模块变量存储在 node.js 中的范围是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15406062/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 13:51:50  来源:igfitidea点击:

In what scope are module variables stored in node.js?

node.js

提问by skerit

When I do this in my node.js module:

当我在 node.js 模块中执行此操作时:

var abc = '123';

Where does it go? And by this I mean: in the browser it goes in window.abc(if not executed in a function or otherwise)

它去哪里?我的意思是:在浏览器中它会进入window.abc(如果不是在函数中执行或以其他方式执行)

If I execute this:

如果我执行这个:

abc = '123';

Then I can find it in global.abc, but that's not how I want it.

然后我可以在 中找到它global.abc,但这不是我想要的。

回答by Hector Correa

Unlike the browser, where variables are by default assigned to the global space (i.e. window), in Node variables are scoped to the module (the file) unless you explicitlyassign them to module.exports.

与浏览器中的变量默认分配给全局空间(即窗口)不同,在 Node 中变量的范围仅限于模块(文件),除非您明确将它们分配给 module.exports。

In fact, when you run node myfile.jsor require('somefile.js')the code in your file is wrapped as follow:

实际上,当您运行node myfile.jsrequire('somefile.js')文件中的代码如下包装时:

(function (exports, require, module, __filename, __dirname) {
     // your code is here
});

回答by Toadfish

All the other answers are 100% correct, but I thought I would add an expanded/definitive list of the scopes within a Node.js application in case anybody comes across this via Google while starting off learning Node.js or JavaScript:

所有其他答案都是 100% 正确的,但我想我会在 Node.js 应用程序中添加范围的扩展/确定列表,以防有人在开始学习 Node.js 或 JavaScript 时通过 Google 遇到这个问题:

Global Scope

全球范围

Anything declared without the varkeyword in any file will be accessible from anywhere running in the same instance of the Node server:

var在任何文件中没有关键字声明的任何内容都可以从运行在 Node 服务器的同一实例中的任何地方访问:

// foo.js
bar = 'baz';


// qux.js
console.log(bar); // prints 'baz'

Note that this is widely considered to be a bad idea, because it makes your app strongly 'coupled'– meaning that you'd have to open foo.js to work out why bar = 'baz'in qux.js

请注意,这被广泛认为是一个坏主意,因为它使您的应用程序强“耦合”——这意味着您必须打开 foo.js 才能bar = 'baz'在 qux.js 中找出原因

Module Scope

模块范围

Anything declared with the varkeyword at the top level (not inside a function or object, or any other block) of a node.js file is in module scope, and will be accessible from anywhere within the same file, but will not exist anywhere else:

var在 node.js 文件的顶层(不在函数或对象或任何其他块内)用关键字声明的任何内容都在模块范围内,并且可以从同一文件中的任何地方访问,但不会存在于其他任何地方:

// foo.js
var bar = 'baz';
console.log(bar); // prints 'baz'


// qux.js
console.log(bar); // prints 'undefined'

Function Scope

功能范围

Anything declared using the varkeyword within a function will only be accessible from within that function, and not from anywhere else:

var在函数内使用关键字声明的任何内容只能从该函数内访问,而不能从其他任何地方访问:

// foo.js
function myFunction() {
  var bar = 'baz';
  console.log(bar); // prints 'baz'
}

function myOtherFunction() {
  console.log(bar); // prints 'undefined'
}



// qux.js
console.log(bar); // prints 'undefined'

JavaScript is function scoped. Unlike other (block scoped) languages, variables declared in a block within a function are accessible from anywhere else in that parent function. For example, this means that if you declare a new variable inside inside a loop, it's accessible outside of that loop as well, as long as you're still inside the parent function:

JavaScript 是函数作用域的。与其他(块作用域)语言不同,在函数内的块中声明的变量可以从该父函数中的任何其他地方访问。例如,这意味着如果您在循环内部声明一个新变量,只要您仍在父函数内部,它也可以在该循环外部访问:

function myFunction() {
  while (thing === true) {
  var bar = 'baz';
  thing = false;
  }
  console.log(bar); // prints 'baz'
}

Shadowing

阴影

If you 'redeclare' an existing variable, e.g. use the varkeyword with a variable name that has been used already, then the value associated with that variable name is overwritten within the scope of the new declaration:

如果您“重新声明”现有变量,例如使用var已使用过的变量名的关键字,则与该变量名关联的值将在新声明的范围内覆盖

var bar = 'foo';
console.log(bar) // prints 'foo'

function myFunction() {
  var bar = 'baz';
  console.log(bar);
}
myFunction(); // prints 'baz'
console.log(bar) // prints 'foo'

回答by robertklep

Node has a module scope, so var abc = '123'in a module will create a variable which is scoped to (and therefore, reachable only for code in) that module.

Node 有一个模块作用域,所以var abc = '123'在一个模块中将创建一个变量,该变量作用域为(因此,只有代码可以访问)该模块。

See also http://nodejs.org/api/globals.html#globals_global

另见http://nodejs.org/api/globals.html#globals_global

回答by loadaverage

Pretty old question, and if anyone is curious about ECMA specs about this question, here is the link

很老的问题,如果有人对这个问题的 ECMA 规范感到好奇,这里是链接

And there is no way for direct access for module variables (except for imported modules):

并且无法直接访问模块变量(导入的模块除外):

Lexical Environments and Environment Record values are purely specification mechanisms and need not correspond to any specific artefact of an ECMAScript implementation. It is impossible for an ECMAScript program to directly access or manipulate such values.

词法环境和环境记录值纯粹是规范机制,不需要对应于 ECMAScript 实现的任何特定人工制品。ECMAScript 程序不可能直接访问或操作这些值。