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

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

问题描述

我刚刚在NodeJS环境中看到了这个关键字的奇怪行为。我用代码列出它们。我使用 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 ),前三个示例产生窗口对象,最后一个给出了 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 中的大部分代码都以空全局 <$运行c $ c> 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:


全球

< Object>全局命名空间对象。

在浏览器中,顶级范围是全局范围。这意味着如果您在全局范围内,浏览器中的
会定义
全局变量。在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); 在自调用函数中,将指向全局nodeJS范围对象,该对象包含所有NodeJS公共属性和方法,例如 require() module exports console ...

  • 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天全站免登陆