你可能会知道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不记录?是否已弃用?