I faced with strange behaviour when using eval
in JS.
var f = function () {
var x = 10;
return function () {
eval('console.log(x);');
window['eval']('console.log(x);');
}
};
f()();
OUTPUT:
10
undefined:1
console.log(x);
^
ReferenceError: x is not defined
Why using eval
explicitly captures the x
but global['eval']
doesn't?
And even though global['eval']
doesn't capture x
, why it's unable to see after eval
, which already captured x
?