Home  >  Article  >  Web Front-end  >  The for in traversal problem caused by the prototype extension of array Array_javascript skills

The for in traversal problem caused by the prototype extension of array Array_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:35:011006browse

Usually in JavaScript, there is no difference in the result of using for and for in to traverse an array. Its loop variable i is an array index starting from 0 (if for in traverses the attribute collection of a non-array object, then this i is the attribute name. , or called key). Another thing to note is: use for in to traverse the array, the loop variable i is of string type. If you prototype Array and then use for in to traverse the array, you need to pay attention to some problems.
Test code:

Copy code The code is as follows:

Array.prototype.max = function()
{
return Math.max.apply({}, this);
};
var company = ['Adobe','Apple','Google','Intel' ,'Microsoft','Oracle','IBM','SUN'];
alert(company.length); //The length output here is 8, which is consistent with the array length defined above
//The following It has been looped 9 times. In the first loop of IE6 and IE7, the i value is not 0 but the method name max of the prototype extension above. However, in the last loop of IE8 and FF browsers, the i value is max
for (var i in company)
{
alert('index is:' i 'nvalue is:' company[i]);
}
//At this time, if you only want to print the above 8 For a company list, it is not possible to use a for in loop. Even if you have to do this, you must make some judgments inside the loop, such as the following:
for (var i in company)
{
if (!isNaN(i))
alert('index is:' i 'nvalue is:' company[i]);
}
//Of course, for safety reasons, don’t be so lazy, just It is most scientific to write an ordinary for loop, like the following:
for (var i=0; i< company.length; i )
{
alert('index is:' i 'nvalue is:' company[i]);
}
//This kind of cyclic problem caused by the array prototype extension attribute usually causes you to not get the expected results, but if you are not careful, it may also cause problems in your code. For hard-to-find problems, take a look at the following example:
var userInfo = [["Tom",21,"020-12345678"],["Mike",23,"020-87654321"]]; // Obviously this is array nesting
for (var i in userInfo)
{
//At this time, you may not get the value you expected through userInfo[i][0] at all, such as in this loop , you will see a result with a value of undefined. This is because when the i value is max, userInfo[i] is a function instead of a subarray like this ["Tom",21,"020-12345678"]
alert('Name:' userInfo[i][0] 'nAge:' userInfo[i][1] 'nPhone:' userInfo[i][2]);
}

Generally speaking, for in is used to traverse object properties, while arrays still need to be traversed using for (of course arrays are also objects). It is understood that the traversal efficiency of for is higher than that of for in. In addition, C#, ActionScript3.0 and foreach loop will be the most efficient loop, but JavaScript does not have such a loop. The most scientific array traversal should look like this: use an ordinary for loop and pre-store the array length. The code is as follows:
Copy code The code is as follows:

var company = ['Adobe', ' Apple', 'Google', 'Intel', 'Microsoft', 'Oracle', 'IBM', 'SUN'];
for (var i = 0, companyNum = company.length; i < companyNum; i )
{
alert('index is:' i 'nvalue is:' company[i]);
}

Author: WebFlash
Source: http: //webflash.cnblogs.com
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