Node.js 全局 eval,抛出 ReferenceError [英] Node.js Global eval, throwing ReferenceError
问题描述
我正在尝试从 Rhino 书中学习 JavaScript.我试图执行书中关于 eval()
的以下代码.我正在使用 node.js (v0.10.29) 来执行示例.
I am trying to learn JavaScript from the Rhino book. I was trying to execute the following code from the book with regards to eval()
. I am using node.js (v0.10.29) to execute the examples.
var geval = eval; // aliasing eval to geval
var x = 'global'; // two global variables
var y = 'global';
function f () {
var x = 'local'; // define a local variable
eval('x += "changed";'); // direct eval sets the local variable
return x;
}
function g () {
var y = 'local'; // define a local variable
geval('y += "changed";'); // indirect eval sets global variable
return y;
}
console.log(f(), x); // => expected 'localchanged global'
console.log(g(), y); // => expected 'local globalchanged'
但是,当尝试使用 geval()
别名时,我在 g()
函数中得到一个 ReferenceError
:
However, I get an ReferenceError
inside the g()
function when there is an attempt to use the geval()
alias:
undefined:1
y += "changed";
^
ReferenceError: y is not defined
at eval (eval at g (/Users/codematix/Learning/learnjs/expressions.js:148:3), <anonymous>:1:1)
at eval (native)
at g (/Users/codematix/Learning/learnjs/expressions.js:148:3)
at Object.<anonymous> (/Users/codematix/Learning/learnjs/expressions.js:153:3)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
据我所知,当我将 eval()
别名为 geval()
时,传递的字符串中的代码将按照 ES5 在全局范围内进行评估.但是,我遇到了 ReferenceError
并且无法理解为什么.
From what I understand, when I alias eval()
as geval()
, the code in string passed is evaluated in global-scope as per ES5. However, I am running into the ReferenceError
and unable to understand why.
虽然我不认为 eval()
是一个关键特性,但我绝对想了解为什么我会遇到这种行为.
Although I would not consider eval()
to be a critical feature, I definitely would like to understand why I am encountering this behavior.
附言当我尝试在 Google Chrome 中执行相同的代码时,它似乎很有魅力!奇怪!
P.S. When I attempt to execute the same code in Google Chrome, it seems to work like a charm! Strange!
推荐答案
问题是你从一个模块运行这段代码,其中 var y = global;
实际上定义了 y
在模块作用域,而不是全局作用域.
The issue is that you are running this code from a module, wherein var y = global;
actually defines y
in module scope, not global scope.
在浏览器中,顶级作用域是全局作用域.这意味着在浏览器中,如果您在全局范围内 var 将定义一个全局变量.在 Node 中这是不同的.顶级范围不是全球范围;节点模块中的 var 某些内容将是本地的那个模块.
In browsers, the top-level scope is the global scope. That means that in browsers if you're in the global scope var something will define a global variable. In Node this is different. The top-level scope is not the global scope; var something inside a Node module will be local to that module.
http://nodejs.org/api/globals.html#globals_global
因此,在 Node 中实现此功能的两种可能方法是:
So, two possible ways to get this to work in Node are:
- 在节点 REPL 中按原样运行
- 在模块中运行它,但将
var y = global;
替换为y = global;
这篇关于Node.js 全局 eval,抛出 ReferenceError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!