数组求和forEach方法

 var arr = [1, 2, 3, 4, 5, 6,7,8,9,10];

var calc = {
sum: 0
};
function getSum(item, index, array) {
this.sum += item;
console.log(this.sum);
}
arr.forEach(getSum,calc);
console.log('use forEach and change this:sum=' + calc.sum); 

原文地址:https://www.cnblogs.com/yuzhengbo/p/6408480.html