标签:style blog io ar color sp on div log
var singleton = function(){ //私有 var priVar = 10; function priFun(){ return false; } //公有 return { publicProperty: true, publicMethod: function(){ priVar++; return priFun(); } }; }(); //初始化函数 document.writeln(singleton.publicProperty);
通过变量 singleton 只能访问公有的属性和方法,私有的属性和方法会在初始化时调用
某种类型的单利
function BaseComponet(){ } var application = function(){ // 私有 var privateVar = 10; var privateMethod = function(){ return false; } var components = new Array(); var app = new BaseComponet(); //自定义的 //公共接口 app.getComponent = function(){ return components.length; } app.registerComponent = function(component){ if(typeof component == "object"){ components.push(component); } } return app; // 初始化时将app的引用赋给application }(); // 初始化
初始化的方式:表达式();
标签:style blog io ar color sp on div log
原文地址:http://www.cnblogs.com/lcw5945/p/4146171.html