3
我在nodejs上运行此代码。我想知道为什么在执行时不关闭字符串'Globals'?封闭体中的this
不是指向全局范围吗?Javascript关闭按预期工作
// 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
^^当然!卫生署。 –
很好地解释了+1 –
'setTimeout'是如何进入答案的?我修好了,但我很好奇。这实际上是一个愚蠢的? –