In js, eval () function and trim () are removed from the left and right spaces of strings.

Source: Internet
Author: User
The eval () function in js is understood and the trim () function is written to remove spaces between the left and right strings. The eval () function in js is not necessarily correct, for more information about eval () functions, you may be able to understand eval () functions in js and write a trim () function to remove spaces around the string.
Trim () is a reference to the jquery source code, you can rest assured to use.

My understanding of eval () functions in js is not necessarily correct.

The Code is as follows:





New Document


The trim function for processing spaces before and after string filtering in js does not exist,

Jquery adds this very common function. The source code is as follows:

Function trim (t ){

Return (t | ""). replace (/^ \ s + | \ s + $/g ,"");

}

Sometimes we don't need jquery. We don't need to add the entire jquery library to a function,

Now we can copy the source code to write a trim function.



Script
Var f = 'hello ';
// Alert (f );
/*
By the way, the eval () function can use the content in the brackets as js script computing,
It can also be used to calculate mathematical operations or strings.
In short, it is not a simple String concatenation function.
You can use it as a js script in js.
This is similar to jsp. jsp is the java code embedded in html,
The content in eval () is the js Code embedded in js.
*/
// Eval ("alert ('" + f + "')"); // calculate js scripts, which have the same effect as alert (f.
Eval ("var gg = 'hahaha '");
Alert (eval ("gg"); // The js Code embedded in eval is equivalent to var gg = 'hahaha', alert (gg );
// Alert (eval ('3 + 4'); // calculates the mathematical operation. Result 7
// Alert (eval ('3' + '4'); // calculate the string, result 34
Alert ("start" + trim ('abc def ') + "end ");
// Trim function in jquery to filter out the first space.
Function trim (t ){
Return (t | ""). replace (/^ \ s + | \ s + $/g ,"");
}
Script

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.