On the in syntax of JavaScript

Source: Internet
Author: User

I believe everyone has used a For loop in JavaScript, which is mainly used to iterate over the array objects, and it is easy to perform repetitive operations and reflect the reusability of the code. However, the index of an array should generally be a number of integers, and when a JSON object or object is encountered, it cannot be traversed with a for loop, and the object should be traversed with the for in function, where the individual's understanding of for IS.

First of all, even though the keyword is called for-in syntax, the syntax can also be used to traverse the object, get the object's property name, and then get the object by the object name [property name]. Personally, the key to understanding the nature of this grammar is to understand what each cycle gets.

The implementation of the for in feature is extended from the for loop because the array index of JavaScript can be a string. Here's an example,

After clicking the button one:

0:blue

1:red

2:yellow. Click button after: uname: Zhang San
Age:18 This is the result of the above code output, you can also imagine if there is a method in U1 u1.say=function () {
Alert ("Hello");
}, then click on the output uname: Zhang San
Age:18
Say:function () {alert ("Hello");} Therefore, you can iterate through the JSON object and the Obj object using for in! Purely personal experience, the wrong place, please advise!

On the in syntax of JavaScript

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.