NodeJS 中的“全局"对象是什么 [英] What is the 'global' object in NodeJS

查看:24
本文介绍了NodeJS 中的“全局"对象是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚在 NodeJS 环境中看到 this 关键字的奇怪行为.我用代码列出它们.我已经用 NodeJS v6.x 运行了这些代码,只有一个 JavaScript 文件.

I've just seen a weird behaviour of the this keyword in NodeJS environment. I'm listing them with code. I've run this codes with NodeJS v6.x, with a single JavaScript file.

在使用如下一行代码进行测试时,无论是否使用 'use strict' 语句,这都指向一个空对象 {}.

While testing with one line of code as follows, whether with or without the 'use strict' statement, this points to an empty object {}.

console.log(this)

但是,当我在像这样的自执行函数中运行语句时,

But, when I'm running the statement within a self executing function like,

(function(){
  console.log(this);
}());

它正在打印一个非常大的对象.在我看来,NodeJS 环境创建的全局执行上下文对象.

It's printing a really big object. Seems to me the Global execution context object created by NodeJS environment.

并且在使用 'use strict' 语句执行上述函数时,预计它会打印 undefined

And while executing the above function with a 'use strict' statement, expectedly it's printing undefined

(function(){
  'use strict';

  console.log(this);
}());

但是,在使用浏览器时(我只用 Chrome 测试过),前三个示例产生 window 对象,最后一个给出 undefined 符合预期.

But, while working with browser (I've tested only with Chrome), the first three examples yield the window object and the last one gave undefined as expected.

浏览器的行为是可以理解的.但是,在 NodeJS 的情况下,它是否不会创建执行上下文,直到我包装在函数中?

The behaviour of the browser is quite understandable. But, in case of NodeJS, does it not create the execution context, until I'm wrapping inside a function?

那么,NodeJS 中的大部分代码在一个空的 global object 下运行吗?

So, most of the code in NodeJS runs with an empty global object?

推荐答案

虽然在浏览器中全局作用域是 window 对象,但在 nodeJS 中模块的全局作用域是模块本身,所以当您在 nodeJS 模块的全局范围内定义了一个变量,它将是该模块的本地变量.

While in browsers the global scope is the window object, in nodeJS the global scope of a module is the module itself, so when you define a variable in the global scope of your nodeJS module, it will be local to this module.

您可以在 NodeJS 文档 它说:

You can read more about it in the NodeJS documentation where it says:

全球

<代码><对象>全局命名空间对象.

在浏览器中,顶级作用域是全局作用域.这意味着在浏览器中,如果您在全局范围内 var 将定义一个全局变量.在 Node.js 中这是不同的.顶级范围是不是全局范围;在 Node.js 模块中 var 的东西将是该模块的本地.

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.js this is different. The top-level scope is not the global scope; var something inside an Node.js module will be local to that module.

在您编写代码时:

  • console.log(this) 在一个空的 js 文件(模块)中,它将打印一个空对象 {} 引用你的空模块.
  • console.log(this); 在自调用函数中,this 将指向全局 nodeJS 作用域对象,该对象包含所有 NodeJS 公共属性和方法,例如 require()moduleexportsconsole...
  • console.log(this) 带有 严格模式 在自调用函数中它会打印 undefined 因为自调用函数在 严格模式.
  • console.log(this) in an empty js file(module) it will print an empty object {} referring to your empty module.
  • console.log(this); inside a self invoking function, this will point to the global nodeJS scope object which contains all NodeJS common properties and methods such as require(), module, exports, console...
  • console.log(this) with strict mode inside a self invoking function it will print undefined as a self invoked function doesn't have a default local scope object in Strict mode.

这篇关于NodeJS 中的“全局"对象是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆