Example of for-in traversal in JavaScript _ javascript tips-js tutorial

Source: Internet
Author: User
In the for-in traversal mode, the cyclic counter is of the string type. When the object is traversed, the object property method name is used. When the array is traversed, the array element subscript index is used, which is different from the common for loop, for more information, see the following summary: the for-in traversal cycle counter is a string type. When traversing an object, it is the object property/method name, And when traversing an array, It is the array element subscript index, unlike a common for loop, for-in lists the inherited attributes/methods.

In addition to the traditional for loop, JavaScript defines the for-in method for traversal operations. There are differences in use based on different data sources.
(1) traverse objects:

The Code is as follows:


Var fish = {
Head: 1,
Tail: 1,
}
For (var prop in fish ){
Console. log (fish [prop]);
}


During debugging, observe that the prop is 'head' and 'tail' in sequence. That is, when traversing object attributes, the property name of the object exists in the string type and the cyclic counter is.
(2) traverse the Array

The Code is as follows:


Var arr = ['one', 'two', 'three '];
For (var prop in arr ){
Console. log (prop );
}


During debugging, observe that the prop values are '0' and '1' in sequence, that is, the traversal array still exists in the string type. The difference is that the loop counter is the subscript of the array element. (In this case, you can try to use the for loop output. The result is consistent with that of for-in)
If the following code is added:

The Code is as follows:


If (Object. prototype. clone === 'undefined ')
Object. prototype. clone = function (){};


The output result is: 0, 1, clone
If the for loop is used for output, it is still 0, 1; that is, the for-in loop will traverse the attributes of the type of the data source to which the current operation belongs (when the for-in object is also used for fish, will also output clone), so you need to pull a string using the for-in time: if you only operate on the attributes of the object, you need to remove the inherited attributes, for example, use the hasOwnProperty () method.

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.