You Don't Know JS - Eval
Table of Contents
wonky and fked up interesting language, which tempts me to write a series about it and this is the first one. This is by no means supposed to show how to write JS but just to show some “wacky” stuff.
Eval basically allows you to execute any script within the given context.
So far everything is normal: eval runs inside the current scope.
this is pointed to the instance of A.
Things get interesting when you do this:
Well in this scenario eval cannot get the value of someVar in the global scope.
However ECMA5 says, if you change
eval() call to indirect, in other words, if you use it as a value rather than a function reference, then it will evaluate the input in the global scope.
So this would work:
eval call the exact same function,
geval is a value and thus it becomes an indirect call according to ECMA5.
So what the hell is
(0, eval) then? Well a comma separated expression list evaluates to the last value, so it essentially is a shortcut to
var geval = eval; geval(...);
0 is only a puppet here. It could be any value.