0
看看這個怪異的行爲:爲什麼節點的全局這個對象是空的?
/tmp$ node -v
v0.10.31
/tmp$ cat foo.js
function FooBar() {
this.some_method = function() {
return 42
}
}
var class_name = "FooBar"
console.log((new this[class_name]).some_method())
/tmp$ node < foo.js
42
/tmp$ node foo.js
/tmp/foo.js:7
console.log((new this[class_name]).some_method())
^
TypeError: undefined is not a function
at Object.<anonymous> (/tmp/foo.js:7:14)
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)
at node.js:906:3
爲什麼從一個文件中執行代碼時,節點忘記其全球this
對象的內容,從STDIN反對?