【发布时间】:2016-04-13 16:30:46
【问题描述】:
我一直在为求职面试而学习,并开始深入研究 JavaScript。想出了这个。
所以:
"use strict";
var x = 0;
var y = 0;
eval("x=3;y=11;"); //direct call to eval in global scope
console.log("x: " + x); // outputs 3
console.log("y: " + y); // outputs 11
但是:
"use strict";
var x = 0;
(0, eval)("x=3;y=11;"); //indirect call to eval in global scope
console.log("x: " + x); // outputs 0 because the strict mode won't allow the reassignment?
console.log("y: " + y); // outputs 11
我不知道/理解执行 eval 时 x 会发生什么。我知道在严格模式下,分配没有问题。有人愿意向我解释这个吗?谢谢!
【问题讨论】:
-
它在我的控制台中为我打印 3。
-
^ 相同。你在什么环境下运行这段代码?
-
两个代码部分对我来说都是一样的
-
我是用node来执行文件的。
-
In what scope are module variables stored in node.js? 的可能重复项使用全局变量重试。
标签: javascript scope eval strict