function removeRepeat(arrs){ var newArr = []; var hash = {}; for(var i=0;i<arrs.length;i++){ var key = typeof(arrs[i])+arrs[i]; if(hash[key] !==1){ newArr.push(arrs[i]); hash[key] =1; }; }; return newArr; }
十二、prototype 属性,直接对数组的原型进行更改或者添加功能:
//比如我们给数组添加一个求和的方法 Array.prototype.sum= function(){ var n = 0; this.forEach(function(i){ n+=i; }); return n; } var arr = [1,2,3,4] alert(arr.sum()) //10