//普通函数封装写法,也就是闭包写法
var arr1 = [1,2,3,4,5,6,7,8,9,10];
var arr2 = [2,4,6,8,10,12,14,16]
function AddResult(arr){
arr.sum = function(){
var result = 0;
for(var i = 0;i<this.length;i++){ // 这里也可以换成this.length
result += this[i];
}
return result; // 返回结果
}
return arr.sum();
}
console.log("数组arr1和为",AddResult(arr1)); // 数组arr1和为55
console.log("数组arr2和为",AddResult(arr2)); // 数组arr2和为72