-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
50、模块化发展历程、IIFE、AMD、CMD、CommonJS、UMD、ES Modules #51
Comments
模块化主要是用来抽离公共代码,隔离作用域,避免变量冲突等。 IIFE: 使用自执行函数来编写模块化,特点:在一个单独的函数作用域中执行代码,避免变量冲突。 (function(){
return {
data:[]
}
})() AMD: 使用requireJS 来编写模块化,特点:依赖必须提前声明好。 define('./index.js',function(code){
// code 就是index.js 返回的内容
}) CMD: 使用seaJS 来编写模块化,特点:支持动态引入依赖文件。 define(function(require, exports, module) {
var indexCode = require('./index.js');
}); CommonJS: nodejs 中自带的模块化。 var fs = require('fs'); UMD:兼容AMD,CommonJS 模块化语法。 webpack(require.ensure):webpack 2.x 版本中的代码分割。 ES Modules: ES6 引入的模块化,支持import 来引入另一个 js 。 import a from 'a'; ES Modules与common.js区别
AMD,CMD用的不多,主要讲一下CommonJS和ESModule 模块的特性
CommonJS特点: exports.add = function add () {/* 方法 */}
// 等同于
module.exports.add = function add () {/* 方法 */} 但注意,不能给 ES6 Module特点: <script type="module" src="./module.js"></script> ESModule的优势:
二者的差异CommonJS模块引用后是一个值的拷贝,而ESModule引用后是一个值的动态映射,并且这个映射是只读的。
CommonJS运行时加载,ESModule编译阶段引用。
Originally posted by @Reaper622 in Advanced-Frontend/Daily-Interview-Question#28 (comment) |
No description provided.
The text was updated successfully, but these errors were encountered: