Node.js 变量声明和作用域
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19850234/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Node.js variable declaration and scope
提问by IGRACH
When I type this in node.js, I get undefined.
当我在 node.js 中输入这个时,我得到undefined.
var testContext = 15;
function testFunction() {
console.log(this.testContext);
}
testFunction();
=>undefined
Without varkeyword, it passes (=>15). It's working in the Chrome console (with and without varkeyword).
没有var关键字,它通过 (=>15)。它在 Chrome 控制台中工作(带和不带var关键字)。
回答by gustavohenke
It doesn't work in Node when using varbecause testContextis a local of the current module. You should reference it directly: console.log(testContext);.
使用时它在 Node 中不起作用,var因为它testContext是当前模块的本地。您应该直接引用它:console.log(testContext);.
When you don't type var, what happens is that testContextis now a global var in the entire Node process.
当你不输入时var,发生的事情是testContext现在整个 Node 进程中的一个全局变量。
In Chrome (or any other browser - well, I'm unsure about oldIE...), it doesn't matter if you use varor not in your example, testContextwill go to the global context, which is window.
在 Chrome(或任何其他浏览器 - 好吧,我不确定 oldIE ...)中,无论您var在示例中是否使用,testContext都将转到全局上下文,即window.
By the way, the "global context" is the default thisof function calls in JS.
顺便说一句,“全局上下文”是thisJS 中函数调用的默认值。
回答by Jonathan Lonowski
The key difference is that all modules (script files) in Node.js are executed in their own closurewhile Chrome and other browsers execute all script files directly within the global scope.
关键的区别在于 Node.js 中的所有模块(脚本文件)都在它们自己的闭包中执行,而 Chrome 和其他浏览器直接在全局范围内执行所有脚本文件。
This is mentioned in the Globals documentation:
这在Globals 文档中提到:
Some of these objects aren't actually in the global scope but in the module scope - this will be noted.
其中一些对象实际上并不在全局范围内,而是在模块范围内 - 这将被指出。
The vars you declare in a Node module will be isolated to one of these closures, which is why you have to export membersfor other modules to reach them.
var您在 Node 模块中声明的s 将与这些闭包之一隔离,这就是为什么您必须导出其他模块的成员才能访问它们。
Though, when calling a functionwithout a specific context, it will normally be defaulted to the global object-- which is conveniently called globalin Node.
但是,在function没有特定上下文的情况下调用 a 时,它通常会默认为全局对象——global在 Node.js 中很方便地调用它。
function testFunction() {
return this;
}
console.log(testFunction() === global); // true
And, without the varto declare it, testContextwill default to being defined as a global.
而且,如果没有var声明它,testContext将默认定义为全局.
testContext = 15;
console.log(global.testContext); // 15
回答by prasun
As mentioned in the document
如文档中所述
var something inside an Node.js module will be local to that module.
Node.js 模块中的 var 某些内容将是该模块的本地内容。
So, it is going to be different as the var testContextis in module context and the context of this is global.
因此,它会有所不同,因为var testContextis 在模块上下文中,而 this 的上下文是global。
You can alternatively, use:
您也可以使用:
global.testContext = 15;
function testFunction() {
console.log(this.testContext);
}
testFunction();
回答by Jason
I believe the problem has to do with the thiskey word. If you do console.log(this)you will see that testContext is not defined. You may want to try:
我认为问题与this关键字有关。如果你这样做,console.log(this)你会看到 testContext 没有定义。您可能想尝试:
this.testContext = 15;
function testFunction() {
console.log(this.testContext);
}
testFunction();

