1
,如果我救下面的線路和運行像./node saved.js的Node.js:運行一個腳本文件或互動產生不同的結果
var that = this;
that.val1 = 109;
var f1 = function(){return this;}
var f2 = function(){return this;}
var that1 = f1();
var that2 = f2();
console.log(that1.val1)//undefined
that1.val2=111;
console.log(that2.val2)//111
我獲得這個結果
undefined
111
但是如果我把它粘貼到已經啓動的shell ./node,我得到
...
...
> console.log(that1.val1)//undefined
109
undefined
> that1.val2=111;
111
> console.log(that2.val2)//111
111
爲什麼第一個console.log的輸出不同?
REPL(交互模式)在全局範圍內執行代碼,其中模塊文件[this === global]在[它們自己的範圍內運行](http://nodejs.org/api/globals.html#globals_global_objects )where this == module.exports'。 –