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