node.js 是否相当于浏览器中的 window 对象

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

Does node.js have equivalent to window object in browser

javascriptnode.js

提问by IGRACH

What I mean is does node.js have object that are global function methods of. Like this in browser:

我的意思是 node.js 是否具有作为全局函数方法的对象。在浏览器中是这样的:

function myGlobalFunction() {
    console.log(this === window);
}
myGlobalFunction();

=> true

采纳答案by EmptyArsenal

The closest equivalent in node is global. I'm not sure if it translates in all of the same ways, but if you open a REPL and type in this === global, it will return true.

node 中最接近的等价物是global。我不确定它是否以所有相同的方式翻译,但是如果您打开 REPL 并输入this === global,它将返回 true。

Here's a discussion on the global object, though some it the information may be deprecated as it's pretty old: 'Global' object in node.js

这是关于全局对象的讨论,尽管有些信息可能已被弃用,因为它已经很旧了:node.js 中的“全局”对象

回答by plalx

Yes, the globalvariable is the global object in Node.js

是的,global变量是 Node.js 中的全局对象

From the docs:

文档

global# {Object} The global namespace object. In browsers, the top-level scope is the global scope. That means that in browsers if you're in the global scope var something will define a global variable. In Node this is different. The top-level scope is not the global scope; var something inside a Node module will be local to that module.

global# {Object} 全局命名空间对象。在浏览器中,顶级作用域是全局作用域。这意味着在浏览器中,如果您在全局范围 var 中,则会定义一个全局变量。在 Node 中这是不同的。顶级作用域不是全局作用域;节点模块中的 var 某些内容将是该模块的本地内容。