This method is quite useful. There are corresponding implementations in many programming languages, and JavaScript is no exception. However, when I run the following code in IE:
var arr = [1,2,3];
alert(arr.indexOf(1));
But I was prompted "The object does not support this property and method". It runs fine in chrome and ff. So I went to ask Google experts and found that the indexOf method of Array in js was only implemented in js1.6 version. IE7 and 8 only implemented js1.3 version, chrome was js1.7 version, and ff was js1.8. Version. (ie still half a beat slower). I have no choice but to extend it for IE:
Array.prototype. _indexOf = function(n){
if("indexOf" in this){
return this["indexOf"](n);
}
for(var i=0;iif(n===this[i]){
return i;
}
}
return -1;
};
is used as follows:
var arr = ["1","2","3"];
alert(arr._indexOf("2"));
Here we have extended the Array prototype. In the extension I added the "_" character to the name of the method. I think this is a good habit. When you extend the prototype, it is necessary to mark your extension.
In the _indexOf method, we first determine whether the current Array implements the "indexOf" method. If so, directly call the system method, otherwise it will be traversed.
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn