4 ms·
It's getting to the limit of my knowledge here, so please don't take this as fact, however: I believe it is because of a weird distinction between "direct" and
by Klathmon 7y ago
It's getting to the limit of my knowledge here, so please don't take this as fact, however:
I believe it is because of a weird distinction between "direct" and "indirect" calls in javascript. The spec says that an indirect call is guaranteed to execute in the global context, but a direct call will execute in whatever context it's in.
The `(0, eval)` makes it an indirect call by evaluating an expression that returns the function.
I'm not quite sure about the details of why indirect calls are treated that way though...
- mratzloff 7y agoI believe it is because of a weird distinction between "direct" and "indirect" calls in javascript. The spec says that an indirect call is guaranteed to execute in the global context, but a direct call will execute in whatever context it's in. The `(0, eval)` makes it an indirect call by evaluating an expression that returns the function. This is really timely. I just ran into an issue this weekend where I tried to generalize a function call like this (unaware of the direct vs. indirect semantics), and TypeScript yelled at me about it. Looks like it was trying to evaluate it in the global context, so the error makes sense now.
- wiktor-k 7y agoThis is correct, for details this is specified as point 15.1.2.1 in ES spec: > (...) any invocation of eval that is not a direct call uses the global environment as its variable environment rather than the caller’s variable environment. Source: https://ecma-international.org/ecma-262/5.1/ https://ecma-international.org/ecma-262/5.1/
- chii 7y agoahh, so this only applies to the 'eval' function, and not just any functions it seems.