Array没有indexOf方法,这样在一个数组中查找某个元素的索引时比较麻烦,为了调用方便,于是通过prototype原型扩展了Array.prototype.indexOf(),这样用起来就比较方便了。但是这个自定义的indexOf在对数组进行遍历的时候却出现了问题。 复制代码 代码如下: Array.prototype.indexOf = function(item) { for (var i = 0; i < this.length; i++) { if (this[i] == item) return i; } return -1; }
用的时候直接 复制代码 代码如下: var arr=[1,2,3,4,5]; var index=arr.indexOf(1); //index==0
扩展了以后,用起来很爽很方便,一片和谐景象... 但是某次是遍历数组元素的时候,使用for..in..循环,引发了其他的问题,打破了这个和谐的氛围。 复制代码 代码如下: var a=["张飞","关羽","刘备","吕布"]; for(var p in a){ document.write(p+"="+a[p]+"<br/>"); }
本来想输出这四个人的名字,结果输出的是什么呢? 输出的居然是: 复制代码 代码如下: //0=张飞 //1=关羽 //2=刘备 //3=吕布 //indexOf=function(item) { for (var i = 0; i < this.length; i++) { if (this[i] == item) return i; } return -1; }
除了把名字打出来以外,还额外输出了自己扩展的方法indexOf,但是令人疯狂的是,firefox却是“正常”的,只有四个人的人名,为什么会这样? 输出indexOf,自己扩展的,可以理解,毕竟for..in是遍历一个对象的所有用户定义的属性或者一个数组的所有元素。 那么firefox为什么不会? 后来查了资料才明白, Array在javascript1.6版本已经支持Array.indexOf(),而我用的firefox是3.5版本,已经支持javascript1.8了,indexOf是其Array本身固有的方法了。 而IE,即使我用的是IE8,也才支持到javascript1.3版本。 所以IE8认为indexOf是“用户定义的属性”,而firefox认为是自己原生支持的固有的属性。 真的是这样吗? 做个实验,把indexOf更名为myIndexOf,再试试,结果IE和firefox都输出myIndexOf,证明前面的观点是正确。 那么又来了个问题,我扩展indexOf很久了,现在不少项目的代码都已经在使用这个方法,而现在我非要使用for..in输出数组本身的元素,不要其他我自己扩展到俄方法,怎么办? 好在javascript提供了hasOwnProperty方法。 看一下其描述: 复制代码 代码如下: Every object descended from Object inherits the hasOwnProperty method. This method can be used to determine whether an object has the specified property as a direct property of that object; unlike the in operator, this method does not check down the object"s prototype chain