JS的各种模块化规范

时间:2020-08-17 14:07:14   收藏:0   阅读:64

AMD CMD CommonJs UMD ES6 一路演进

AMD

// moudle-a.js
define(‘moudleA‘, function() { 
    return {
        a: 1
    }
});

// moudle-b.js
define([‘moudleA‘], function(ma) {
    var b = ma.a + 2;

    return {
        b: b
    }
});

CMD

// moudle-a.js
define(function(require, exports, module) {

    module.exports = { 
        a: 1 
    };

});

// moudle-b.js
define(function(require, exports, module) {

    var ma = require(‘./moudle-a‘);
    var b = ma.a + 2;
    module.exports = { 
        b: b 
    };
});

CommonJs

//moudle-a.js
moudle.exports = {
    a: 1
};

//moudle-b.js
var ma = require(‘./moudle-a‘);
var b = ma.a + 2;
module.exports ={
    b: b
};

UMD

function(e, t) {
    "object" == typeof exports && "object" == typeof module 
    ? module.exports = t() 
    : "function" == typeof define && define.amd 
        ? define([], t) 
        : "object" == typeof exports 
            ? exports.xmh = t() 
            : e.xmh = t()
} (window, (function() {
    // todo
}))

ES6 模块

//module-a.js
export default {
    a: 1
}

//module-b.js
import { a } from ‘./module-a‘
export const b = a + 2

原文:https://www.cnblogs.com/babywhale/p/13516855.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!