function Accumulator(){ var average = 0; var length = 0; var arr = []; var sum = 0; var max = Number.NEGATIVE_INFINITY; var min = Number.POSITIVE_INFINITY; this.add = function(val){ length++; average = average + ( val - average ) / length; sum += val; if(val > max){ max = val; } if(val < min){ min = val; } arr.push(val); } this.size = function(){ return length; } this.getAverage = function(){ return average; } this.sum = function(){ return sum; } this.max = function(){ return max; } this.min = function(){ return min; } }
文章来源: JS累加器