1、IIFE
使用自执行函数来编写模块化,特点是在一个单独的函数作用域中执行代码,避免变量冲突
(function () {
return ...})()
2、AMD
使用requireJS来编写模块化,特点是依赖必须提前声明好
defined('./index.js', function(code){
// code就是index.js返回的内容
})
3、CMD
使用seaJS来编写模块化,特点是支持动态引入依赖文件。
defined(function(require, exports, module) {
let moduleCode = require('./index.js')
})
4、CommonJS
nodejs中自带的模块化
let fs = require('fs')
5、UMD
兼容AMD,特点CommonJS模块化语法
webpack(require.ensure):webpack 2.x 版本中的代码分割
6、ES Modules
ES6引入的模块化,,支持import来引入另一个js
import a from 'a'