我正在阅读Closures,它具有以下关闭示例。
var makeCounter = function() {
var privateCounter = 0;
function changeBy(val)
{
privateCounter += val;
}
return
{
increment: function()
{
changeBy(1);
},
decrement: function()
{
changeBy(-1);
},
value: function()
{
return privateCounter;
}
}
};
var counter1 = makeCounter();
var counter2 = makeCounter();
alert(counter1.value()); /* Alerts 0 */
counter1.increment();
counter1.increment();
alert(counter1.value()); /* Alerts 2 */
counter1.decrement();
alert(counter1.value()); /* Alerts 1 */
alert(counter2.value()); /* Alerts 0 */
我想知道此闭包与以下产生相同结果的代码之间的区别和优点/缺点是什么。
var makeCounter = function() {
var privateCounter = 0;
function changeBy(val)
{
privateCounter += val;
};
this.increment= function()
{
changeBy(1);
};
this.decrement= function()
{
changeBy(-1);
};
this.value= function()
{
return privateCounter;
};
};
var counter1 = new makeCounter();
var counter2 = new makeCounter();
alert(counter1.value()); /* Alerts 0 */
counter1.increment();
counter1.increment();
alert(counter1.value()); /* Alerts 2 */
counter1.decrement();
alert(counter1.value()); /* Alerts 1 */
alert(counter2.value()); /* Alerts 0 */
最佳答案
您的第二个版本也确实使用了闭包,在这方面几乎没有区别。
重要的区别在于,第一个makeCounter
是一个返回对象文字(从Object.prototype
继承的)的工厂,而第二个makeCounter
是一个构造函数(需要用new
调用),该函数确实返回从makeCounter.prototype
继承的实例。您可以通过在原型上移动方法来gain some advantages:
function Counter() {
var privateCounter = 0;
this.changeBy = function changeBy(val) {
privateCounter += val;
};
this.value = function() {
return privateCounter;
};
}
Counter.prototype.increment = function() {
this.changeBy(1);
};
Counter.prototype.decrement = function() {
this.changeBy(-1);
};
您可以避免使用闭包(特权方法),而仅使用原型方法:
function Counter() {
this.counter = 0; // no more private
}
Counter.prototype.valueOf = function() {
return this.ounter;
};
Counter.prototype.increment = function() {
this.counter += 1;
};
Counter.prototype.decrement = function() {
this.counter -= 1;
};