最近在学习seajs,不知道有没有知道如下两段代码的使用区别,分别在什么场景下使用?define(function(require, exports) { // 对外提供 foo 属性
exports.foo = 'bar'; // 对外提供 doSomething 方法
exports.doSomething = function() {};});define(function(require, exports, module) { // 对外提供接口
module.exports = {
name: 'a',
doSomething: function() {};
};});seajsjavascript
exports.foo = 'bar'; // 对外提供 doSomething 方法
exports.doSomething = function() {};});define(function(require, exports, module) { // 对外提供接口
module.exports = {
name: 'a',
doSomething: function() {};
};});seajsjavascript
exports.doSomething = function() {};
exports.doOthers = function() {};但有时,我们想提供整个接口的时候,exports 就不行了
var interface = {doSomething:function(){},doOthers:function(){}}
此时书写起来会非常麻烦,需要这样写:
exports.doSomething = interface.doSomething;
exports.doOthers = interface.doOthers;所以,这个就可以改变 module.exports 的引用来替换掉整个接口
module.exports = interface;
至于其余的,不理会;对外提供接口什么的,哪用说的这么高级?
自己预先定义一个全局对象就行了,要外部调用的函数放全局对象上就ok了