CommonJs
CommonJs应用在NodeJS,是一种同步的模块机制,大致写法如下:
var firstModule = require("firstModule");
//the codes of yourself...
module.export = anotherModule
AMD
AMD的应用场景则是浏览器,异步加载的模块机制,大致写法如下:
define(['firstModule'],function(module){
//the codes of yourself...
})
CommonJs与AMD的比较
- 从写法上比较,CommonJS是更为优秀的,它是一种同步的写法,对Human很友好,代码不会繁琐臃肿
- 随着npm成为主流的JavaScript组件发布平台,越来越多的前端项目也依赖于npm上的项目,或者自身就会发布到npm平台
ES2015(ES6)
ES2015里也有了自己的模块机制,也就是说ES6的模块机制是官方规定的。
import {someModule} from "someModule";
// your codes...
export anotherModule;