【发布时间】:2013-11-19 22:36:41
【问题描述】:
当我在 node.js 中输入这个时,我得到undefined。
var testContext = 15;
function testFunction() {
console.log(this.testContext);
}
testFunction();
=>undefined
没有 var 关键字,它通过 (=>15)。它在 Chrome 控制台中运行(带有和不带有 var 关键字)。
【问题讨论】:
-
您是否尝试在对
console.log()的调用中删除this.? -
thiskeyword 在那里做什么? -
@JustinNiessner 删除
this.有效。为什么?如果在另一个 node.js 文件中调用了testFunction();,它还可以工作吗? (测试是,但为什么呢?) -
发现是javascript的
lexical scope。
标签: javascript node.js scope