exports仅仅是module.exports的一个引用。在factory内部给exports重新赋值时,并不会改变module.exports的值。因此给exports赋值是无效的,不能用来更改模
exports
仅仅是 module.exports
的一个引用。在 factory
内部给 exports
重新赋值时,并不会改变 module.exports
的值。因此给 exports
赋值是无效的,不能用来更改模块接口。
个人理解,exports相当于是暴露属性或者方法,而module.exports相当于是暴露了整个模块,模块中存在属性与方法。所以当给exporst直接赋值,并不能改变模块中的字段。
错误写法:
define(function(require, exports) { // 错误用法!!! exports = { foo: 'bar', doSomething: function() {} };});
正确写法:
define(function(require, exports, module) { // 正确写法 module.exports = { foo: 'bar', doSomething: function() {} };});