请解释代码段注释的this指向?代码段链接出处:https://developer.mozilla.org...function Counter() { this.sum = 0; this.count = 0;
}
Counter.prototype.add = function(array) {
array.forEach(function(entry) { this.sum += entry;
++this.count;
}, this);//**该行的this是指向什么??**};var obj = new Counter();
obj.add([2, 5, 9]);
obj.count// 3 obj.sum// 16
1 回答

萧十郎
TA贡献1815条经验 获得超13个赞
Counter.prototype.add = function(array) { array.forEach(func, this); };
你把第一个函数参数替换一下,this好像并没有机会指向其他对象吧
添加回答
举报
0/150
提交
取消