我在nodejs上运行这段代码。我想知道为什么执行闭包时不会打印字符串“Globals”?难道闭包中的this
指向全局作用域吗?
// Running on NodeJS, not in a browser!
this.name = "Globals";
function Person(name) {
this.name = name;
this.namePrinter = function() {
return function() {
console.log(this.name);
}
}
}
var p = new Person("Faiz");
p.namePrinter()(); // prints undefined. Shouldn't it print Globals?
console.log(this.name); // prints Globals
setTimeout
是如何被引入答案的?我已经修复了,但是我很好奇。这实际上是一个重复的问题吗? - T.J. Crowder