module
is a plain JavaScript object with an exports
property.
(module
是一个带有exports
属性的普通JavaScript对象。)
exports
is a plain JavaScript variable that happens to be set to module.exports
.(exports
是一个普通的JavaScript变量,恰好被设置为module.exports
。)
At the end of your file, node.js will basically 'return' module.exports
to the require
function.(在文件末尾,node.js基本上将“返回” module.exports
到require
函数。)
A simplified way to view a JS file in Node could be this:(在Node中查看JS文件的一种简化方法是:)
var module = { exports: {} };
var exports = module.exports;
// your code
return module.exports;
If you set a property on exports
, like exports.a = 9;
(如果您为exports
设置属性,例如exports.a = 9;
)
, that will set module.exports.a
as well because objects are passed around as references in JavaScript, which means that if you set multiple variables to the same object, they are all the same object;(,这也会设置module.exports.a
,因为对象是作为JavaScript中的引用传递的,这意味着,如果将多个变量设置到同一对象,则它们都是同一对象;)
so then exports
and module.exports
are the same object.(所以后来exports
和module.exports
是同一个对象。)
But if you set exports
to something new, it will no longer be set to module.exports
, so exports
and module.exports
are no longer the same object.(但是,如果你设置exports
新的东西,它将不再被设为module.exports
,因此exports
和module.exports
不再是同一个对象。)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…