JavaScript eval ("{}") returns behavior?

According to the ECMA-262 specification, the following statements return 1 :

 eval("1;;;;;") eval("1;{}") eval("1;var a;") 

Ensuring that:

The StatementList value is the value of the last value that produces the Statement in the Statement list.

Can you explain these different results?

 eval("{}") // undefined eval("var a={}; a;") // {} eval("var a={};") // undefined 

What is the difference between 1; and {}; ?

+4
source share
2 answers

Alone {} interpreted as a block, not an object. It does not contain instructions, so it does not affect the value of, say, eval("1;{}") . To make it be interpreted as an object, you can use parentheses:

 eval("1;({})"); // {} 
+6
source

It seems to me that eval interprets {} as code block delimiters and therefore has no inherent meaning.

+2
source

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


All Articles