4

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

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

Ensuring that:

The value of a StatementList is the value of the last value producing Statement in the StatementList.

Can you explain these different returns ?

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

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

4

2 回答 2

6

Left alone, {} is interpreted as a block, not an object. It contains no statements, so does not affect the value of, say, eval("1;{}"). To force it to be interpreted as an object, you can use parentheses:

eval("1;({})"); // {}
于 2012-05-28T22:49:45.633 回答
2

在我看来,它eval被解释{}为代码块的分隔符,因此没有内在价值。

于 2012-05-28T22:52:37.483 回答