Note the differences in browser versions here.
Copy codeThe 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 ("traversing attributes ");
For (var I in tmp ){
Alert (tmp [I]);
}
Alert ("traversing arrays ");
For (var I = 0; I <tmp. length; I ++ ){
Alert (tmp [I]);
}
For non-IE8 browsers, such as IE7, IE9, Chrome, and FF, test only these browsers)
The result is the same for retrieving attributes and arrays.
However, for IE8, there will be a slight difference in the results. In IE8, the prototype chain extension method will be used as an attribute output. You can test it.
This difference was also found because it solved a strange bug. Some colleagues used for (var I in tmp) in the code to traverse every element of the array, if the prototype chain method is defined for Array, this problem occurs.
Therefore, we recommend that you use the standard syntax for (var I = 0; I <tmp. length; I ++) to traverse array elements ).