1、js模块化历史
? js本身简单的页面设计:页面动画+表单提交
? 无模块化和命名空间的概念
? 幼年期:无模块化
? 1、开始需要在页面中增加一些不同js:动画、表单、格式化等
? 2、多种js文件被分在不同的文件中
? 3、不同文件被一个模板引用
? 出现了jqury.js main.js depl.js
? 文件分离是基础的模块化第一步,但出现了这些问题
? * 污染全局作用域 =>不利于大型项目开发、多人团队的共建
? 成长期:模块化雏形 -IIFE(语法优化)
? 具体而言就是作用域的把控
?
let count = 0;
const increase = ()=>++count;
const reset = ()=>{
count = 0;
}
(()=>{
let count = 0;
})
increase();
reset();
(()=>{
let count = 0;
})();
const lifeModule = (()=>{
let count = 0;
return {
increase:()=> ++count;
reset:()=>{
count = 0;
}
}
})();
lifeModule.increase();
lifeModule.reset();
const lifeModule = ((dependencyModule1,dependencyModule2)=>{
let count = 0;
return {
increase:()=> ++count;
reset:()=>{
count = 0;
}
}
})(dependencyModule1,dependencyModule2);
lifeModule.increase();
lifeModule.reset();
const lifeModule = ((dependencyModule1,dependencyModule2)=>{
let count = 0;
return {
increase:()=> ++count;
reset:()=>{
count = 0;
}
return {
ioncrease,reset
}
}
})(dependencyModule1,dependencyModule2);
lifeModule.increase();
lifeModule.reset();
const dependcyModule1 = require(./dependcyModule1);
const dependcyModule2 = require(./dependcyModule2);
let count = 0;
const increase = ()=>count++;
const reset = ()=>{
count = 0;
}
exports.increase = increase;
exports.reset = reset;
module.exports = {
increase,reset;
}
const {increase,reset} = require('./main.js');
increase();
reset();
(function(thisValue,exports,require,module){
const dependcyModule1 = require(./dependcyModule1);
const dependcyModule2 = require(./dependcyModule2);
}).call(thisValue,exports,require,module);
define(id,[depends],callback);
require([module],callback);
define('amdModule',['dependencyModule1,dependencyModule2'],(dependencyModule1,dependencyModule2)=>{
let count = 0;
const increase = ()=>count++;
const reset = ()=>{
count = 0;
}
return {
increase,reset
}
});
reuqire(['amdModule'],amdModule=>{
amdModule.increase();
})
define('amdModule',[],(require,export,module)=>{
let count = 0;
const increase = ()=>count++;
const reset = ()=>{
count = 0;
}
export.increase = increase();
export.reset = reset();
})
define(require=>{
const otherMoudle = require('amdModule');
otherModule.increase();
otherModule.reset();
})
(define('amdModule',[],(require,export,module)=>{
const dependcyModule1 = require(./dependcyModule1);
const dependcyModule2 = require(./dependcyModule2);
let count = 0;
const increase = ()=>count++;
const reset = ()=>{
count = 0;
}
export.increase = increase();
export.reset = reset();
}))(
typeof module === "object"&&module.exports&&typeof define !== "function"
?
factory=>module.exports = factory(require,exports,module)
://是AMD
define
)
define('amdModule',[],(require,exports,module)=>{
let $ = require('jquery');
let dependencyModule1 = require('./dependcyModule1');
})
import dependencyModule1 from './dependencyMoudule1.js';
import dependencyModule2 from './dependencyMoudule2.js';
const dependcyModule1 = require(./dependcyModule1);
const dependcyModule2 = require(./dependcyModule2);
let count = 0;
export const increase = ()=>count++;
export const reset = ()=>{
count = 0;
}
export default {
increase,reset
}
<script type="module" src="esModule.js"></script>
import {increase,reset} from './esModule,mjs';
esModule.increase()
esModule.reset()
import('./esModule.js').then(dynamicEsModule=>{
dynamicEsModule.increase();
})
?
|