Javascript eval alias

Does javascript provide an alias of eval? The first part of the following code behaves unexpectedly (displays 1, 1), but the second part is not displayed (displays 1, 2). Link to docs of ECMA script or mozilla would be useful, I could not find it.

<html> <script type="application/javascript;version=1.8"> (function(){ eval('var testVar=1'); alert(testVar); var eval2=eval; eval2('var testVar=2'); alert(testVar); })(); (function(){ eval('var testVar=1'); alert(testVar); eval('var testVar=2'); alert(testVar); })(); </script> </html> 
+6
source share
2 answers

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.
+7
source

In the first case, when you use eval , it uses the scope in which it is executed. When you assign eval to eval2 and then execute the same statement, it seems to use the window context (global scope), not the function context. This is why you see the same value 1 in the first case, because testVar inside the function is 1 and outside window.testVar is 2. You can prove it by doing the below snippet

 <script> (function(){ eval('var testVar=1'); alert(window.testVar); var eval2=eval; eval2('var testVar=2'); alert(window.testVar); })(); (function(){ eval('var testVar=1'); alert(testVar); eval('var testVar=2'); alert(testVar); })(); </script> 

In fact, according to the Mozilla Developer Network , you cannot use the alias eval.

+2
source

Source: https://habr.com/ru/post/949920/


All Articles