当前位置:
首页
文章
前端
详情

AMD、CDM和UMD区别与规范

一、AMD

全称:Common Module Definition 通用模块定义

典型类库:require.js

写法规范

define(['jquery', 'lodash'], function($, _) {
    function a() {}
    function b() {}
    function c() {} // 私有
    return {a, b}
});

二、 CDM

全称:Asynchronous Module Definition 异步模块定义

典型类库:Sea.js

写法规范

var $ = require('jquery');
var _ = require('lodash');

function a() {}
function b() {}
function c() {} // 私有

module.exports = {a, b}

三、UMD

全称:Universal Module Definition 通用模块定义

兼容AMD和CMD

写法规范

(function (root, factory) {
    if(typeof define === 'function' && define.amd) {
        // AMD
        define(['jquery', 'lodash'], factory);
    } else if(typeof exports === 'object') {
        // Node、CommonJS
        module.exports = factory(require('jquery'), require('lodash'));
    } else {
        root.returnExport = factory(root.jQuery, root._); // root即window
    }
})(this, function($, _) {
    function a() {}
    function b() {}
    function c() {} // 私有

    return {a, b}
});

免责申明:本站发布的内容(图片、视频和文字)以转载和分享为主,文章观点不代表本站立场,如涉及侵权请联系站长邮箱:xbc-online@qq.com进行反馈,一经查实,将立刻删除涉嫌侵权内容。