Node.js中的根对象是什么 [英] What is the root object in Node.js

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

问题描述

您可能知道Node中的 全局对象 .js:

You may know the global object in Node.js:


{Object}全局命名空间对象。

{Object} The global namespace object.

在浏览器中,顶级范围
是全局范围。这意味着在浏览器中,如果你在
全局范围内,var会定义一个全局变量。在Node中,
是不同的。顶级范围不是全球范围; var
Node模块中的内容将是该模块的本地内容。

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.

现在我偶然发现 root 似乎无处记录的对象。

Now I stumbled over the root object which seems to be documented nowhere.

虽然我似乎可以使用 root 全球相同:

Though it seems that I can use root the same way as global:

test1.js

foo = 'bar'; // foo is defined in the global scope (no var in front of foo)

test2 .js

require('./test1.js');
console.log(root.foo);

在shell中:

$ node test2.js
bar

当我检查全局 root 在shell中它们看起来是一样的。尝试:

When I inspect global and root in the shell they look the same. Try:

$ node
> global
...
> root
...

所以似乎 root global 相同。但为什么冗员呢?为什么 root 没有记录?它是否已被弃用?

So it seems that root is the same as global. But why the redundancy? Why is root not documented? Is it deprecated?

推荐答案

它与 global 完全相同。

这样有一些未记录的属性。它们可以从节点的早期开始,但是保留了它们以保持向后兼容性,并且没有必要删除它们。

There are a few undocumented properties like this. They date from early days of node but were left in to maintain backwards-compatibility and there is no pressing need to remove them.

你不应该在任何新的中使用它们代码,因为它们可能在将来被删除。

You shouldn't use them in any new code, as they could be removed at any future time.

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

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