js模块化
2017-11-23 本文已影响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();
})