美文网首页
js模块化

js模块化

作者: 骑驴的帅小伙 | 来源:发表于2017-11-23 16:22 被阅读0次
    • 命名空间
      库名.类名.方法
    NameSpace = {};
    NameSpace.type = NameSpace.type || {};
    NameSpace.type.method = function() {};
    
    • commonJS
      一个文件为一个模块
    modeule.exports // 暴露模块接口
    require // 引用模块 同步执行
    
    • AMD (RequireJS)
      推崇依赖前置,预先加载所有的依赖,直到使用的时候才执行
    define(['./a', './b'], function(a, b) {  // 依赖一开始就写好
        a.doSomething()
        b.doSomething()
    })
    
    • CMD (SeaJS)
      推崇依赖就近,直到使用的时候才定义依赖
    define(function(require, exports, module) {   
        var a = require('./a');
        a.doSomething();
        var b = require('./b'); // 依赖就近书写
        b.doSomething();
    })
    
    • ES6 Module
      一个文件为一个模块
    define(function(require, exports, module) {   
        var a = require('./a');
        a.doSomething();
        var b = require('./b'); // 依赖就近书写
        b.doSomething();
    })
    

    相关文章

      网友评论

          本文标题:js模块化

          本文链接:https://www.haomeiwen.com/subject/iywzvxtx.html