eval function
The Eval function receives a string consisting of a JavaScript statement and returns the return value of the last statement in the string, and if the last statement does not return a value, the Eval function returns undefined. If the pass to the Eval function is not a string, then what is passed and eval returns.
Three scenarios for calling the Eval function
When the Eval function is called, JavaScript creates a new execution environment with a total of three scenarios:
1 Direct call
When called directly, the Eval function-related execution Environment property thisbinding,lexicalenvironment,variableenvironment values are as follows:
A) thisbinding is the thisbinding of the caller execution environment when the Eval function is called
b) Lexicalenvironment is the lexicalenvironment of the caller execution environment when the Eval function is called
c) Variableenvironemnt is the variableenvironment of the caller execution environment when the Eval function is called
Suppose you have the following JavaScript code (f is called in the Global Environment):
function f () { var i = 1; Eval ("var y = 2; i = 3 ");
alert (y);
}f ();
When the Eval function is called, the execution environment stack is as follows:
It is important to note that when creating a new execution environment, the identifier is bound and the identifier of the binding is placed in the lexical environment that the execution environment variableenvironment points to. Because the variableenvironment of the Eval function and the caller (that is, the function f that called Eval) point to the same lexical environment, the variableenvironment The local variable y declared in Eval is bound to the caller's lexical environment, which causes the eval-related execution environment to be ejected from the top of the stack when the Eval function call ends, while the local variable y declared in eval is still accessible in function f. Therefore, alert in the above code will show 2 instead of an error.
2 Indirect calls
Called an indirect call, the eval will be assigned to another variable after the call, as shown in the following code:
var g = eval;g ("var y = 1;");
The indirect call also creates a new execution environment, except that the value of the thisbinding,lexicalenvironment,variableenvironment for the new execution environment is different:
A) thisbinding as a global object
b) lexicalenvironment for the Global execution Environment lexicalenvironment
c) variableenvironment for the Global execution Environment variableenvironment
If you have the following code:
function f () { var i = 1; var geval = eval; Geval ("var y = 2; i = 3 "); alert (y);} f ();
When Geval is called, the execution environment stack looks like this:
As you can see, the variables declared in Geval are bound to the global execution environment, and it is important to note that the variable i in Geval is not the variable I of the referenced function F, because the local variable I of function f is not accessible from the scope chain of the geval. At this point, the variable i in Geval is equivalent to declaring a global variable without using the keyword var. The alert statement for function f still shows 2, except that the y that is accessed at this time is the variable y in the Global environment.
3 eval in strict mode
In strict mode, the lexicalenvironment,variableenvironment of eval points to the lexcial environment that belong to eval itself, not lexical environment of the caller, But thisbinding is still the caller's thisbinding. Also, in strict mode if Eval is called directly, then Eval's lexical environment outer pointer points to the caller's lexical environment, otherwise, if it is an indirect call, then the eval lexical The environment outer pointer points to the lexical environment of the global environment.
If you have the following code:
// Use Strict mode function f () { var i = 1; Eval ("var y = 2; i = 3 "); alert (y);} f ();
When you call eval, the execution environment stack looks like this:
As can be seen from the graph, the local variable y declared in Eval is bound to its own lexical environment, and I in eval accesses the local variable I declared by function F. Since the variable y is bound to Eval's own lexical environment, the function f is not accessible to the variable y when the eval run is finished and the associated execution environment is ejected from the top of the stack, so alert has an error.
Eval in IE
Before IE9, Eval is treated as a direct call, regardless of whether Eval is a direct call or an indirect call, and you can use the Execscript function provided by IE if you need to have the effect of an indirect call.
Resources:
JavaScript authoritative Guide
ECMA-262
The Eval function in JavaScript