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