Node.js中的模块范围
我很难弄清楚以下模块范围如何在node.js中工作。
main.js
module.exports = App = { add: function(a, b) { return a + b; } } var getNumber = require('./module'); var result = App.add(100, getNumber());
module.js
var number = 200; module.exports = function () { console.log(App); // App is visible here - how come? return number; };
我不知道为什么应用程序在模块中可见,因为它不是必需的。如果我不再导出应用程序,它是不可见的。
由于你没有声明var App
,所以App
成为一个全局variables。 即使您根本没有module.exports
,也会发生这种情况。
App
在全球范围内:
foo = {} foo.bar = baz = 5 console.log(baz) // baz is available on the global scope