Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
242 views
in Technique[技术] by (71.8m points)

javascript - Node.js Global eval, throwing ReferenceError

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'

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) 

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.

Although I would not consider eval() to be a critical feature, I definitely would like to understand why I am encountering this behavior.

P.S. When I attempt to execute the same code in Google Chrome, it seems to work like a charm! Strange!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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

So, two possible ways to get this to work in Node are:

  1. Run it as is in the node REPL
  2. Run it in a module, but replace var y = global; with just y = global;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...