exports
首先是 exports 的几种形式,我们可以:
define(function(require, exports, module) {
exports.a = 'a';
exports.fn = function() {};
});
也可以:
define(function(require, exports, module) {
module.exports = {
a: 'a',
fn: function() {}
};
});
这两者有什么区别呢?
就最终的效果而言,这两种形式没什么区别:都是使得该模块对外提供 xx 和 fn 两个公共成员。
就代码组织形式而言, exports.xx
= ... 是分散赋值,中间可以穿插其他代码,很灵活。 module.exports
= { ... } 则是集中式......
阅读全文