js

闭包和面向对象设计

2017-02-23  本文已影响7人  u14e

闭包:

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

extent.call();  // 1
extent.call();  // 2
extent.call();  // 3

面向对象:

var extent = {
    value: 0,
    call: function() {
        this.value++;
        console.log(this.value);
    }
}
var Extent = function() {
    this.value = 0;
}
Extent.prototype.call = function() {
    this.value++;
    console.log(this.value);
}

var extent = new Extent();
上一篇 下一篇

猜你喜欢

热点阅读