码迷,mamicode.com
首页 > Web开发 > 详细

如何让你的插件兼容CommonJS, AMD, CMD 和 原生 JS

时间:2017-08-30 17:37:40      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:==   style   code   common   bsp   params   oba   接口   var   

我们除了提供 AMD 模块接口,CMD 模块接口,还得提供原生的 JS 接口。
由于 CMDAMD 都可以使用 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

标签:==   style   code   common   bsp   params   oba   接口   var   

原文地址:http://www.cnblogs.com/Alan2016/p/7454198.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!