Home >Web Front-end >JS Tutorial >IE8 discusses the differences between JS attributes and array traversal parsing_javascript skills

IE8 discusses the differences between JS attributes and array traversal parsing_javascript skills

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-05-16 17:34:391068browse

Please keep in mind the pitfalls of differences in browser versions.

Copy code The code is as follows:

Array.prototype.Contains = function (str) {
if (!str)
return false;
for (var i = 0; i < this.length; i ) {
if (this[i] == str) {
return true;
}
}
return false;
}
var tmp = new Array();
tmp.push("1");
tmp.push ("2");
tmp.push("3");
alert("Traverse attributes");
for ( var i in tmp ) {
alert(tmp[i]) ;
}
alert("Traverse array");
for ( var i=0;ialert(tmp[i]);
}

If it is a non-IE8 browser, such as (IE7, IE9, Chrome, FF, only these are tested)
Traversing through attributes and arrays, the results are the same.
But for IE8, the results will be slightly different. Under IE8, the prototype chain extension method will be output as an attribute. You can test it.

The reason why this difference was discovered was because a weird bug was solved. A colleague used for (var i in tmp) in the code to traverse each element of the array. If Array is defined If the prototype chain method is not used, this problem will occur.
So it is recommended that if you traverse array elements, you should still use the standard writing method for (var i=0;i
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