The forin traversal problem caused by prototype extension of Array _ javascript skills

Source: Internet
Author: User
Different programming languages have multiple types of loop statements with similar functions. Of course, there are some differences in usage scenarios, such as for, forin, and forin, it does not need to know the object attribute length in advance. Generally, in JavaScript, there is no difference between the for and for in array traversal results, its cyclic variable I is an array index starting from 0 (for in, if it traverses a non-array object attribute set, this I is the attribute name or key ). In addition, you should note that the for in array is traversed, And the loop variable I is of the string type. If you perform prototype extension on the Array and use for in to perform the traversal of the Array, pay attention to some problems.
Test 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 output length here is 8, consistent with the length of the array defined above
// The following loops 9 times. In the first loop of IE6 and IE7, the I value is not 0, but the method name max extended by the prototype above, 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 list of the above eight companies, you will not be able to use the for in loop. Even if you have to do so, you should make some judgments within the loop, for example:
For (var I in company)
{
If (! IsNaN (I ))
Alert ('index is: '+ I +' \ nvalue is: '+ company [I]);
}
// Of course, for the sake of security, do not be so lazy. Writing a common for loop is the most scientific, as shown below:
For (var I = 0; I <company. length; I ++)
{
Alert ('index is: '+ I +' \ nvalue is: '+ company [I]);
}
// This kind of loop problem caused by the extended attributes of the array prototype usually results in unexpected results, but it may cause hard-to-find problems for your code if you do not pay attention to it. Let's take a look at the following example:
Var userInfo = [["Tom", 21, "020-12345678"], ["Mike", 23, "020-87654321"]; // It is obviously an array nested
For (var I in userInfo)
{
// At this time, userInfo [I] [0] may not get the expected value at all. For example, in this loop, you will see the undefined result, this is because when the I value is max, userInfo [I] is a function rather than a subarray like this ["Tom", 21, "020-12345678"]
Alert ('name: '+ userInfo [I] [0] +' \ nAge: '+ userInfo [I] [1] +' \ nPhone: '+ userInfo [I] [2]);
}


In general, for in is used to traverse object attributes, while arrays still use for to traverse (of course, arrays are also objects ). It is understood that for is more efficient than for in traversal, and there are foreach loops like C # And ActionScript3.0, which will be the most efficient loop, but JavaScript does not. The most scientific array traversal should be like this: Use a common for loop, and pre-store the length of the array. The Code is as follows:

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
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.