本地对象Array的原型扩展

该扩展方法目的为删除数组中的重复项,并返回一个包含所有重复条目的新数组;

我的方法
Array.prototype.del=function(){
var b={},c,i=0,l=this.length,j;
for(;i<l;i++){
c
=this.shift();
c
in b ? b[c]++ : b[c]=0;
}
for(j in b){
if(b[j]>0)this.push(+j||j);
}
return this;
}
var a=[1,2,2,3,3,3,'a','b','b'];
alert(a.del());

 

果果的去重方法
Array.prototype.del=function(){
return (function (i, n, j, u, o){
for (; i < j; i++) {
o
= this[i];
o
in u ? 0 : (u[this[n++] = o] = 1)
}
this.length = n;
return this
}).call(
this, 0, 0, this.length, {})
}
var a=[1,2,2,3,3,3,'a','b','b'];
alert(a.del())
posted @ 2010-12-04 14:34  jxin  阅读(329)  评论(3编辑  收藏  举报