闭包和面向对象设计

1.用闭包来实现面向对象设计

var extent=(function(){
    var value=0;
    return {
        call:function(){
            value++;
            console.log(value);
        }
    };
})();

extent.call()  //1

extent.call()  //2

extent.call()  //3

2.对象字面量法

var extent={
    value:0,
    call:function(){
        this.value++;
        console.log(this.value);
    }
}
extent.call()  //1
extent.call()   //2
extent.call()   //3

3.组合使用构造函数模式和原型模式

var Extent=function(){
    this.value=0;
};

Extent.prototype.call=function(){
    this.value++;
    console.log(this.value);
};

var extent=new Extent();

extent.call(); //1
extent.call(); //2
extent.call();//3
原文地址:https://www.cnblogs.com/t1amo/p/6767634.html