Eval () function usage in JavaScript introduction _javascript Tips

Source: Internet
Author: User

In JavaScript, you can use the Eval () function to parse the JavaScript code in a string and return the corresponding code execution result:


Copy Code code as follows:

Console.log (eval ("42 * 2"));//84


In essence, eval () is a function of the JavaScript global object. For example, the above code is equivalent to:


Copy Code code as follows:

Console.log (This.eval ("42 * 2"));//84


However, when using the eval () statement, the first of these approaches is generally followed by ignoring the global object directly using eval ().

Use of eval ()

For the following two reasons, you should avoid using the eval () statement in your code unless you really need to:

1. Logically, strings should be used to store content and information during the running of a program, and should not be used to store specific computational logic.
2. The JavaScript interpreter cannot be optimized for eval () invocation statements because the eval () parameter is a string and cannot be lexical for a segment of the string.

return value of eval ()

The return value of the eval () follows the following rules:

1. If the eval () parameter is not a string, then eval () returns the argument directly.
2. If the eval () parameter is a string, eval () parses the string into code to execute and returns the result of the last line of code execution.
3. If the string cannot be parsed into legitimate code, eval () throws a syntaxerror error.
4. If the string can be parsed into legitimate code, but the error is reported in the execution of this code, the fault is escalated to the Eval () statement and thrown by Eval ().

Copy Code code as follows:

Console.log (eval ([1,2,3]));//[1, 2, 3]
Console.log (typeof eval ([1,2,3]);//object

Console.log (eval ("2"));//syntaxerror
Console.log (eval ("42 * 2; 22 * 3; ")); /66. Eval returns the result of last expression/statement
Console.log (eval ("null.tostring ()"));//typeerror, exception in eval-ed code would be propagated outside Eval ().

Variable environment (variable environment)

An important feature of Eval () in JavaScript is that the code in the eval () parameter string can access the variables in the external code, and you can expose the new variable in the parameter string code to the external code. That is, if the eval () parameter string can be legitimately parsed, then JS replaces the parsed code with the same line as the eval ():

Copy Code code as follows:

Variable environment
var a = 108;
Console.log (eval ("function double (x) {return x*2;} A = double (a)));
Console.log (a);//216
Console.log (double (33));//66


It is important to note that the above features are implemented on the assumption that the code in the eval () parameter string can be legitimately parsed. In addition to the correct code syntax, JS requires that the code in the eval () parameter string must be "self-contained": The code must be meaningful only for the code in the parameter string. For example, the "return;" Such strings are passed to the eval () function:


Copy Code code as follows:

function Test () {
var s = "Test";
Eval ("return s;");
}
Test ();//syntaxerror:return not in function


If you use the Eval () function directly, the variable accessed by the code in the eval () parameter string is those of the function of the eval () statement, and the variable environment used by the eval () function is the local variable environment. If you do not use the Eval () function directly, but instead use a new variable that also points to the Eval () function, the code in the corresponding parameter string accesses a global variable and the variable environment used by the eval () function is the global variable environment:


Copy Code code as follows:

Local variable environment and global variable environment
var renamed = eval;
var x = "origin", y = "origin";
function f () {
var x = "new";
Eval ("x + + ' Changed ';");
return x;
}
function g () {
var y = "new";
Renamed ("Y + = ' Changed ';");
return y;
}
Console.log (f (), x);//newchanged origin
Console.log (g (), y);//new originchanged


However, it is noteworthy that the behavior in IE6, 7, and 8 differs from this. In IE6, 7, and 8, even if the eval () function is renamed, the "local variable environment" is still used.

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.