Node: module.exports、exports的区别
-
"exports is assigned the value of module.exports before the module is evaluated."
exports
是module.exports
的一个引用,exports
指向的是module.exports
mdoule.exports
初始化为一个空对象require()
返回的时**module.exports
而不是exports
**
知道以上三点后我们可以知道:
当给
exports
对象添加属性时,我们同时也操作了module.exports
,是可行的;exports.a = function(){};
当给
exports
对象赋值时,断开了exports
与module.exports
的连接,moudle.exports
仍然是空对象,这时就无效了exports = { a: a, b: b }
因此建议,无论何时都使用
module.exports
的形式导出模块