<script>
var module = {
id: 0,
say: function () {}
}
</script>
<!-- 也是全局变量对象, 且成员属性会被修改 -->
自执行函数(函数作用域实现私有成员)
<script>
var module = (function() {
var id = 0; // 私有变量
var hello = function () {};
var world = function () {};
return {
hello: hello,
world: world
}
})();
</script>
<!-- 全局变量, 但实现了部分成员私有, 其他成员共享 -->
<script>
module.hello()
module.world()
</script>
默认导出:export default Person(当导入时可指定模块任意名称,无需知晓内部真实名称)
单独导出:export const name = "huhua"
按需导出:export { age, name, sex }(推荐)
改名导出:export { name as newName }
默认导入:import Person from "person"
整体导入:import * as Person from "person" // Module对象
按需导入:import { age, name, sex } from "person"
改名导入:import { name as newName } from "person"
自执导入:import "./person"
复合导入:import Person, { name } from "person"
默认导入导出:export { default } from "person"
整体导入导出:export * from "person"
按需导入导出:export { age, name, sex } from "person"
改名导入导出:export { name as newName } from "person"
具名改默认导入导出:export { name as default } from "person"
默认改具名导入导出:export { default as name } from "person"
传统模式下的模块
2.模块是同步加载的,即只有加载完成,才能执行后面的操作
3.模块在首次执行后就会缓存,再次加载只返回缓存结果,如果想要再次执行,可清除缓存
4.模块加载的是值的拷贝, 同时 require('./moudle.js') 多次都只是一个对象的拷贝; CommonJS 模块遇到循环加载时,返回的是当前已经执行的部分的值,而不是代码全部执行后的值
默认导出:export default Person(当导入时可指定模块任意名称,无需知晓内部真实名称) 单独导出:export const name = "huhua" 按需导出:export { age, name, sex }(推荐) 改名导出:export { name as newName }
默认导入:import Person from "person" 整体导入:import * as Person from "person" // Module对象 按需导入:import { age, name, sex } from "person" 改名导入:import { name as newName } from "person" 自执导入:import "./person" 复合导入:import Person, { name } from "person"
默认导入导出:export { default } from "person" 整体导入导出:export * from "person" 按需导入导出:export { age, name, sex } from "person" 改名导入导出:export { name as newName } from "person" 具名改默认导入导出:export { name as default } from "person" 默认改具名导入导出:export { default as name } from "person"
将各个模块维护到一个list中 / module code / 样板代码解析 replace
同步加载: 直接加载文件 异步加载: jsonp + promise