//模块模式
var box = function(){
var user = 'hkk'; //私有化变量,对外不可访问
function run(){ //私有化方法
return "runing ...";
}
var obj;
obj = {
publicGo:function(){ return user+run();},
}
return obj;
}();
box.publicGo();
//增强模块模式,适用于自定义对象
var box = function(){
var user = 'hkk';
function run(){
return "runing ...";
}
function Desk(){}
var desk = new Desk();
desk.publicGo = function(){ return user+run();}
return desk;
}();
box.publicGo();
JavaScript中的单例模式
最新推荐文章于 2023-04-30 11:15:57 发布