The eval () function and trim () in JS to remove the left and right spaces of the string apply _javascript tips

Source: Internet
Author: User
Tags eval jquery library
For the function of the eval () in JS and write a function trim () Remove the left and right spaces of the string.
Trim () is referred to the jquery source code, you can be assured to use.

For JS in the eval () function of understanding is my experience is not necessarily correct.
Copy Code code as follows:

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >
<title> New Document </title>
<body>
JS in the processing of string filter before and after the space of the trim function is not,<br>
And jquery added this very common function, its source code is as follows <br>
function Trim (t) {<br>
Return (t| | ""). Replace (/^\s+|\s+$/g, "");<br>
}<br>
Sometimes we don't need jquery to join the entire jquery library for a function,<br>
Then we can put the source code to write a trim function on the line. <br>
</body>
<script>
var f= ' Hello ';
alert (f);
/*
Here, by the way, the eval () function, which computes the contents of the parentheses as a JS script,
You can also compute a mathematical operation, or you can compute a string.
In short, it is not a simple string concatenation function.
You can take it as the JS script in JS.
This is similar to JSP, JSP is embedded in the HTML Java code,
The content in the eval () bracket is the JS code embedded in JS.
*/
Eval ("Alert (' +f+ ')");//Compute JS script, same as alert (f) effect.
Eval ("var gg= ' haha '");
Alert ("GG");//eval is embedded in the JS code, equivalent to the Var gg= ' haha ', alert (GG);
Alert (eval (' 3+4 '));//computational mathematical operation, result 7
Alert (eval (' 3 ' + ' 4 '));//compute string, result 34
Alert ("Start" +trim (' abc def ') + "End");
The Trim function in jquery filters 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.