array没有indexof方法,这样在一个数组中查找某个元素的索引时比较麻烦,为了调用方便,于是通过prototype原型扩展了array.prototype.indexof(),这样用起来就比较方便了。但是这个自定义的indexof在对数组进行遍历的时候却出现了问题。
array没有indexof方法,这样在一个数组中查找某个元素的索引时比较麻烦,为了调用方便,于是通过prototype原型扩展了array.prototype.indexof(),这样用起来就比较方便了。
复制代码 代码如下:
array.prototype.indexof = function(item) {
for (var i = 0; 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]+
);
}
本来想输出这四个人的名字,结果输出的是什么呢?
输出的居然是:
//0=张飞
//1=关羽
//2=刘备
//3=吕布
//indexof=function(item) { for (var i = 0; i 除了把名字打出来以外,还额外输出了自己扩展的方法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
看描述,就是我们想要的东西。
在for...in..里做个 判断就ok了
复制代码 代码如下:
if(a.hasownproperty(p)){
document.write(p+=+a[p]+
);
}
另外,附上hasownproperty用法示例,来源于互联网:
复制代码 代码如下:
function book(title, author) {
this.title = title;
this.author = author;
}
book.prototype.price = 9.99;
object.prototype.copyright = herongyang.com;
var mybook = new book(javascript tutorials, herong yang);
// dumping built-in properties at the base prototype level
document.writeln(/nobject.prototype's built-in properties:);
dumpproperty(object.prototype, constructor);
dumpproperty(object.prototype, hasownproperty);
dumpproperty(object.prototype, isprototypeof);
dumpproperty(object.prototype, tostring);
dumpproperty(object.prototype, valueof);
dumpproperty(object.prototype, copyright);
// dumping built-in properties at the my prototype level
document.writeln(/n==================/nbook.prototype's built-in properties:);
dumpproperty(book.prototype, constructor);
dumpproperty(book.prototype, hasownproperty);
dumpproperty(book.prototype, isprototypeof);
dumpproperty(book.prototype, tostring);
dumpproperty(book.prototype, valueof);
dumpproperty(book.prototype, copyright);
// dumping built-in properties at the object level
document.writeln(/n==================/nmybook's built-in properties:);
dumpproperty(mybook, constructor);
dumpproperty(mybook, hasownproperty);
dumpproperty(mybook, isprototypeof);
dumpproperty(mybook, tostring);
dumpproperty(mybook, valueof);
dumpproperty(mybook, copyright);
function dumpproperty(object, property) {
var inheritance;
if (object.hasownproperty(property))
inheritance = local;
else
inheritance = inherited;
document.writeln(property+: +inheritance+:
+object[property]);
}
查看浏览器支持javascript到哪个版本:
复制代码 代码如下:
浏览器的javascript版本支持测试