0%
好处
立即执行函数
1 2 3 4
| (function(globalVariable){ globalVariable.test = function(){} })(globalVariable)
|
AMD和CMD
1 2 3 4 5 6 7 8 9 10 11 12 13
| define(['./a', './b'], function(a, b){ a.do() b.do() })
define(function(require, exports, module){ var a = require('./a') a.do() })
|
CommonJS
1 2 3 4 5
| const path = require('path')
const path = () => {} module.exports = path
|
特点:
- require()是同步加载模块
- 是基于值的拷贝
- node环境中默认使用CommonJS规范
ES Module
1 2 3 4 5 6
| import path from 'path' import { doSomeThing } from 'path'
export const doSomeThing = () => {} export default path
|
特点:
- import是异步加载模块
- 基于值的引用
- ES Module会编译成 require/exports 来执行