function Counter()
{ this.sum = 0;
this.count = 0;
}
Counter.prototype.add = function(array){
array.forEach(function(entry){
this.sum+= entry;
++this.count;
},this);
};
var obj = new Counter();
obj.add([2,5,9]);
console.log(obj.count);
// 3
console.log(obj.sum);
//16
++符号在这里有什么作用? 我知道这是一个人为的例子,它是一个array.forEach(function()) 应用程序。但我似乎并没有明白这一点。请通读此代码。为什么它的输出是3和16我应该如何理解附加代码中的“array.forEach(function)”?
*请通过此代码。为什么它的输出是3和16 * - 最好的一个来帮助你在这里是Chrome/Firefox中的JavaScript调试器。打开开发人员工具/ F12并尝试一下。 – niksofteng
++此处的this.count相当于this.count = this.count + 1 – Avinash