我们除了提供 AMD
模块接口,CMD
模块接口,还得提供原生的 JS 接口。
由于 CMD
和 AMD
都可以使用 return
来定义对外接口,故可以合并成一句代码。
一个直接可以用的代码如下:
;(function(){ ???function MyModule() { ???????// ... ???} ???????var moduleName = MyModule; ???if (typeof module !== ‘undefined‘ && typeof exports === ‘object‘) { ???????module.exports = moduleName; ???} else if (typeof define === ‘function‘ && (define.amd || define.cmd)) { ???????define(function() { return moduleName; }); ???} else { ???????this.moduleName = moduleName; ???}}).call(function() { ???return this || (typeof window !== ‘undefined‘ ? window : global);});
如何让你的插件兼容CommonJS, AMD, CMD 和 原生 JS
原文地址:http://www.cnblogs.com/Alan2016/p/7454198.html