更具体:
module是文件内的全局范围变量。
因此,如果您致电
require("foo"):// foo.jsconsole.log(this === module); // true
它的行为与
window在浏览器中的行为相同。
还有一个称为的全局对象
global,您可以在所需的任何文件中进行读写,但这涉及到更改全局范围,这就是 EVIL
exports是存在的变量
module.exports。基本上,这是您在需要文件时 导出 的内容。
// foo.jsmodule.exports = 42;// main.jsconsole.log(require("foo") === 42); // true单独存在一个小问题
exports。在_global范围上下文+和
module是 不
一样的。(在浏览器中,全局范围上下文与之
window相同)。
// foo.jsvar exports = {}; // creates a new local variable called exports, and conflicts with// living on module.exportsexports = {}; // does the same as abovemodule.exports = {}; // just works because its the "correct" exports// bar.jsexports.foo = 42; // this does not create a new exports variable so it just works


