You cannot βalias" eval and expect it to behave the same. Just like that. What for? eval not a function.
What happens when you call eval2 , you set the cache variable to work with global variables. Therefore, by setting a variable inside it, you set a global variable. However, upon exiting, the cache variable reverts back to the scope function. Therefore, the second alert shows 1 - the global variable is obscured by the function level.
This is noted in Appendix E (p. 239) ECMAScript (emphasis mine)
10.4.2: In Revision 5, indirect calls to the eval function use the global environment, both the environment variable and the lexical environment for the eval code. In version 3, the variable and lexical environment of the indirect indirect eval were used as environments for the eval code.
The full definition of "Enter Eval code" is defined in Β§10.5.2 (p. 58) (emphasis added)
- If there is no call context or if the eval code is not evaluated by a direct call (15.1.2.1.1) for the eval function,
- Initialize the execution context as if it were the global execution context using the eval code as C, as described in 10.4.1.1.
- Otherwise,
- Set ThisBinding to the same value as ThisBinding for the call execution context.
- Set the LexicalEnvironment dictionary value to the LexicalEnvironment value to make the context call.
- Set the VariableEnvironment variable to the VariableEnvironment value to make the context call.
- If eval code is strong code then
- Let strictVarEnv be the result of calling NewDeclarativeEnvironment, passing LexicalEnvironment as an argument.
- Set LexicalEnvironment to strictVarEnv.
- Set the strictVarEnv environment variable.
- Perform binding with binding data as described in 10.5 using the eval code.
source share