为Javasciprt 的 Array 类型添加 remove 删除元素方法

 1 Array.prototype.removeItem = function (target) {
 2 var elIndex;
 3 for(elIndex in this){
 4 if(this[elIndex] == target) break;
 5 }
 6 if (!this.hasOwnProperty(elIndex)) return;
 7 if (isNaN(parseInt(elIndex)) || !(this instanceof Array)) delete this[elIndex];
 8 else this.splice(elIndex, 1);
 9 };
10 
11 var arr = ['BB333','BB444','BB555','AA888'];
12 arr.removeItem('BB555');
13 //注意必须使用这种循环方式输出数组中的数据项,如果使用 in 关键字进行循环,则会把数组视为对象处理,则,循环输出的是对象中的所有属性,即 所有的*.prototype均会被输出,因此,最后的输出会出现自定义的remove方法体. 
14 for(var index=0;index<arr.length;index++){
15 alert(arr.length+" : "+arr[index]);
16 }
posted @ 2012-09-21 04:12  EKO.KL  阅读(304)  评论(0)    收藏  举报