模块基于闭包实现,对外暴露公共API使用,隐藏内部数据,外部只能使用不能修改。
个人理解有点像java类的封装
function module() {
let something = 'something';
let another = 'another';
function doSomething() {
console.log(something);
}
function doAnother() {
console.log(another);
}
return {
doSomething,
doAnother
}
}
let module1 = module();
module1.doAnother();
模块特点
1、必须有外部的封装函数,该函数必须至少被调用一次(每次调用都会创建一个新的模块实例)**
2、封闭函数必须返回一个至少一个内部函数,这样内部函数才能在私有作用域中形成闭包,并且可以访问或者修改私有的状态。