为什么EcmaScript 5严格模式会限制标识符“eval”
根据规范(附件C),严格模式代码几乎不能做任何可能分配名称 eval
的任何标识符的事情。我可以理解,人们可能想要限制使用实际的 eval
函数,但是我没有看到限制使用这个名字?
According to the spec (Annex C), strict-mode code can't do pretty much anything that might assign any identifier with the name eval
. I can understand that one might want to restrict use of the actual eval
function, but I don't see what purpose is served by restricting use of the name?
我只能推测,但在我看来,ES5严格说的是 eval
和参数
应被视为原始语法,而不是标识符。这两个特征应该在语法层面实现是合理的,因为它们具有神奇的Funky Magic行为,这些行为无法通过正常函数再现。
I can only speculate, but it seems to me that ES5-strict is saying that eval
and arguments
should be considered as raw syntax, not identifiers. It is reasonable that these two features should be implemented at the syntactical level, because they have Amazing Funky Magic behaviours that cannot be reproduced by a normal function.
(特别是 eval
可以写入调用它的函数中的局部变量,并写入 arguments
奇怪地改变对应的局部变量的值谢天谢地,虽然这种行为似乎在严格模式下消失了。)
(In particular eval
may write to local variables in the function that calls it, and writing to arguments
bizarrely changes the values of local variables corresponding to the arguments. Though this behaviour seems to be going away in strict mode, thankfully.)
出于兼容性原因,ES5实际上无法生成 eval
和参数
语法。所以他们尽可能地做到最近,也就是说标识符 arguments
总是引用 arguments
magic和标识符 eval
总是专门指 eval
magic。
For compatibility reasons, ES5 can't actually make eval
and arguments
syntactical. So they do the nearest they can, which is to say that the identifier arguments
always refers to arguments
magic and the identifier eval
always exclusively refers to eval
magic.
它如果JS引擎可以确定函数是否包含魔法,那么也可以提高优化的可能性。
It could also improve the possibilities for optimisation, if JS engines can be sure whether a function contains magic.