In JavaScript, you can use the eval() function to parse JavaScript code in a string and return the corresponding code execution result:
The code copy is as follows:
console.log(eval("42 * 2"));//84
Eval() is essentially a function of a JavaScript global object. For example, the above code is equivalent to:
The code copy is as follows:
console.log(this.eval("42 * 2"));//84
However, when using the eval() statement, the first method mentioned above is generally adopted, that is, ignore the global object and use eval() directly.
Use of eval()
For the following two reasons, unless you really need it, you should try to avoid using the eval() statement in your code:
1. Logically speaking, strings should be used to store content and information during the program operation, rather than to store specific calculation logic.
2. Since the eval() parameter is a string, and it cannot be lexical for a piece of string, the JavaScript interpreter cannot optimize for the eval() call statement.
return value of eval()
The return value of eval() follows the following rules:
1. If the parameter of eval() is not a string, eval() will directly return the parameter.
2. If the parameter of eval() is a string, then eval() parses the string into code and executes it, and returns the result of the last line of code execution.
3. If the string cannot be parsed into legitimate code, eval() will throw a SyntaxError error.
4. If a string can be parsed into legal code, but an error is reported during execution of this code, then the error will be reported to the eval() statement and thrown by eval().
The code copy is as follows:
console.log(eval([1,2,3]));//[1, 2, 3]
console.log(typeof eval([1,2,3]));//object
console.log(eval("42 */ 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 will be propagated outside eval().
variable environment
Eval() in JavaScript has an important feature: the code in the eval() parameter string can access variables in the external code, and can also expose the newly created variables in the parameter string code to the external code. That is, if the eval() parameter string can be legally parsed, JS will replace the parsed code with the line where eval() is located:
The code copy is 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 worth noting that the prerequisite for implementing the above feature is that the code in the eval() parameter string can be legally parsed. In addition to correct code syntax, JS also requires that the code in the eval() parameter string must be "self-organized": the code must make sense only in terms of the code in the parameter string. For example, it is impossible to pass a string such as "return;" to the eval() function:
The code copy is as follows:
function test(){
var s = "test";
eval("return s;");
}
test();//SyntaxError: return not in function
If you use the eval() function directly, the variables accessed by the code in the eval() parameter string are those variables in the function where the eval() statement is located, that is, the variable environment used by the eval() function is the "local variable environment". If you do not use the eval() function directly, but use a new variable that also points to the eval() function, then the variables accessed by the code in the corresponding parameter string are global variables, that is, the variable environment used by the eval() function is "global variable environment":
The code copy is 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 worth noting that the behavior in IE6, 7, and 8 is different. In IE6, 7, and 8, even if the eval() function is renamed, the "local variable environment" is still used.