对于大多数node初学者而言, module.exports应该都是理解的, 但多出来一个exports获取就有些疑问了javascript
疑问一: 既然有module.exports了为何还要有exports?java
疑问二: 二者有什么区别?node
首先, 官网是这么回答的express
The exports
variable is available within a module's file-level scope, and is assigned the value of module.exports
before the module is evaluated. app
It allows a shortcut, so that module.exports.f = ...
can be written more succinctly as exports.f = ...
. 函数
也就是说, exports至关于一个快捷方式,exports.f = ...
. 确定是比 module.exports.f = ... 写起来方便一些。下面附上一段express源码中的使用你就明白了。ui
exports = module.exports = createApplication; exports.mime = connect.mime; exports.application = proto; exports.request = req; exports.response = res; function createApplication() { var app = connect(); merge(app, proto); app.request = { __proto__: req, app: app }; app.response = { __proto__: res, app: app }; app.init(); return app; }
其实exports是将指针执行module.exports对象, 若是你像exports = function(){}这样至关于改变了exports原来的指向, 也就没法被导出, 为何?先看官网给的相似实现:lua
function require(/* ... */) { const module = { exports: {} }; ((module, exports) => { // 你的模块代码在这。在这个例子中,定义了一个函数。 function someFunc() {} exports = someFunc; // 此时,exports 再也不是一个 module.exports 的快捷方式, // 且这个模块依然导出一个空的默认对象。 module.exports = someFunc; // 此时,该模块导出 someFunc,而不是默认对象。 })(module, module.exports); return module.exports; }
根据上面的代码能够看出exports是模块内部一个形参对象, 若是给exports对象添加属性是能够导出的, 由于指针并未改变, 但若是赋值一个对象就不行了, 由于指针已经改变了,最后导出的是module.exportsspa