eval在JS中使用时我遇到了奇怪的行为.
var f = function () {
var x = 10;
return function () {
eval('console.log(x);');
window['eval']('console.log(x);');
}
};
f()();
Run Code Online (Sandbox Code Playgroud)
OUTPUT:
10
undefined:1
console.log(x);
^
ReferenceError: x is not defined
Run Code Online (Sandbox Code Playgroud)
为什么使用eval明确捕获x但global['eval']不是?即使global['eval']没有捕获x,为什么它无法看到eval,已经捕获x?