javascript – Node.js中的根对象是什么?

前端之家收集整理的这篇文章主要介绍了javascript – Node.js中的根对象是什么?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
你可能会知道Node.js中的 global object

{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.

现在我偶然发现了根目录,似乎没有记录.

虽然看起来我可以使用与全局相同的方式:

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

当我在shell中检查全局和root时,它们看起来一样.尝试:

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

所以看起来根与全球是一样的.但为什么冗余?为什么root不记录?是否已弃用?

解决方法

它与全球完全一样.

有一些这样的无证属性.他们从节点的早期开始,但是保持向后兼容性,并且没有迫切需要去除它们.

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

原文链接:https://www.f2er.com/js/153928.html

猜你在找的JavaScript相关文章