学习JS
var foo = ( function CoolModule(){ var something = 'cool'; var another = [1,2,3]; function doSomething() { console.log(something); } function doAnother(){ console.log(another); } return { doSomething: doSomething, doAnother: doAnother } } )();
感觉这个模式很牛逼!记录一下
再抄录一个更完整的,
var foo = (
function CoolModule(id){
function change(){
publicApi.identify = identify2
}
function identify1(){
console.log(id);
}
function identify2(){
console.log(id.toUpperCase());
}
var publicApi= {
change,
identify:identify1
};
return publicApi;
}
)('foo modules')
console.log(foo);
JS从来没有系统学过,就是个小白,见到这个模式,真是惊叹!要多多品味
所有增删查改的功能全部可以放到里面,而且自成一个作用域,不会对外面构成污染,还有,foo是一个单例,也就是说,foo永远指向的都是同一个对象

浙公网安备 33010602011771号