Array.prototype.sum= function(l,r){
l=l==undefined ? 0 : l;
r=r==undefined ? this.length - 1 : r;
var s = 0;
for(var i = l;i <= r;i++){s+=this[i];}
return s;
}


使用:

var ar = new Array(1,3,4,-1,-2,3,4,-2,4);
console.log(ar.sum(0,2))