【发布时间】:2016-06-19 04:15:34
【问题描述】:
我在 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
【问题讨论】:
标签: javascript node.js scope closures