views:

105

answers:

1

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?

+2  A: 

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.

(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.)

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.

It could also improve the possibilities for optimisation, if JS engines can be sure whether a function contains magic.

bobince
Indeed, when we were working on JScript.NET we often wished that there was a way to absolutely positively guarantee that (1) invoking something called "eval" was invoking eval, and (2) invoking something not called "eval" was not invoking eval. The design of ECMAScript makes it extremely difficult to determine that statically, and therefore makes it hard to write an optimizing compiler. I have not been on the technical committee for over ten years and so cannot speak definitively about their motivations, but were I on that committee again I would surely want just such a restriction.
Eric Lippert