javascript for in 循环时,会取到Array.prototype

/**
 *删除数组指定下标或指定对象
 */

if(!Array.prototype.remove){

    Array.prototype.remove = function(obj){
        for(var i =0;i < this.length;i++){
            var temp = this[i];
            if(!isNaN(obj)){
                temp=i;
            }
            if(temp == obj){
                for(var j = i;j <this.length;j++){
                    this[j]=this[j+1];
                }
                this.length = this.length - 1;
            }
        }
    }
}


//get array size
if(!Array.prototype.countArray){

    Array.prototype.countArray=function(){
        if(this == null){
            return 0;
        }
        var size = 0;
        for(var key in this){
            if(this[key] != undefined && typeof this[key] != 'function'){
                size++;
            }
        }
        return size;
    }
}

在JS中,扩展了array的两个方法,在for in 会输出CountArray和remove这个元素,

目前使用类似

for(var key in array){
  if(typeof array[key] == 'function'){
   continue;
  }
  // do somthing
}
原文地址:https://www.cnblogs.com/fsong/p/5135511.html