闭包 应用之 特权方法

 内容整理自《javascript高级程序设计(第三版)》

特权方法 举例1 举例2 缺点 备注
自定义类型的特权方法
----构造函数
function MyObject(){
    //私有变量和私有函数
    var privateVariable = 10;
    function privateFunction(){
        return false;
    }
    //特权方法
    this.publicMethod = function (){
       privateVariable++;
       return privateFunction();
    };
}
function Person(name){
    this.getName = function(){
        return name;
    };
    this.setName = function (value) {
       name = value;
    };
}
var person = new Person(“Nicholas”);
alert(person.getName()); //”Nicholas”
person.setName(“Greg”);
alert(person.getName()); //”Greg”
必须使用构造函数模式,针对每个实例都会创建同样一组新方法  
自定义类型的特权方法
----原型模式
(function(){
    //私有变量和私有函数
    var privateVariable = 10;
    function privateFunction(){
        return false;
    }

    //构造函数
    MyObject = function(){}; //函数表达式,全局变量
    //公有方法和特权方法
    MyObject.prototype.publicMethod = function(){
        privateVariable++;
    return privateFunction();
    };
})();
(function(){
    var name = “”;
    Person = function(value){
        name = value;
    };
    Person.prototype.getName = function(){
        return name;
    };
    Person.prototype.setName = function (value){
        name = value;
    };
})();
var person1 = new Person(“Nicholas”);
alert(person1.getName()); //”Nicholas”
person1.setName(“Greg”);
alert(person1.getName()); //”Greg”
var person2 = new Person(“Michael”);//引用的同一函数和对
alert(person1.getName()); //”Michael”
alert(person2.getName()); //”Michael”
每个实例都没有自己的私有变量 1.在私有作用域中定义私有变量货函数;
2.初始化未经声明的变量,总会创建一个全局变量;但在严格模式下会导致错误。
3.与上区别:私有变量和函数 实例共享。
4.因为使用原型而增进代码复用,但每个实例都没有自己的私有变量
单例的特权方法
----模块模式
var singleton = function(){
    //私有属性和私有方法
    var privateVariable = 10;
    function privateFunction(){
        return false;
    }
    //特权/公有方法和属性
    return { //返回对象字面量
        publicProperty: true,
        publicMethod : function(){
            privateVariable++;
            return privateFunction();
        }
      };
}();
var application = function(){
    //private variables and functions
    var components = new Array();
    //initialization
    components.push(new BaseComponent());
    //public interface
    return {
        getComponentCount : function(){
             return components.length;
        },
        registerComponent : function(component){
            if (typeof component == “object”){
                  components.push(component);
            }
        }
    };
}();
  1.单例:只有一个实例对象;惯例使用对象字面量方式创建;
2.返回的对象字面量是单例的公共接口;
3.用于:对单例进行某些初始化,同时维护其私有变量
4.举例:Web应用程序使用单例管理应用程序级的信息。
单例的特权方法
----增强的模块模式
var singleton = function(){
    //私有变量和私有函数
    var privateVariable = 10;
    function privateFunction(){
        return false;
    }
    //创建对象
    var object = new CustomType();
    //添加特权/共有属性和方法
    object.publicProperty = true;
    object.publicMethod = function(){
        privateVariable++;
     return privateFunction();
    };
    //返回这个对象
    return object;
}();
//如果前面演示模块模式的例子中的application对象必须是
//BaseComponent的实例,使用如下代码
var application = function(){ //私有变量和私有函数 var components = new Array(); //初始化 components.push(new BaseComponent()); //创建application的局部副本 var app = new BaseComponent(); //公共接口 app.getComponentCount = function(){ return components.length; }; app.registerComponent = function(component){ if (typeof component == “object”){ components.push(component); } }; //返回这个副本 return app; }();
  1.适于:单例必须是某种类型的实例,同时必须添加某些属性或方法对其加以增强的情况;
原文地址:https://www.cnblogs.com/zldream1106/p/3090934.html