Analysis of the for in usage example in js

Source: Internet
Author: User

For (var I = 0; I <len; I ++) can be replaced by for in.
For example:
Copy codeThe Code is as follows:
Var a = ["a", "B", "c"];
For (var el in ){
Alert (a [el]);
}

This is to cite all the elements in a. Of course, the above example can be used.
Copy codeThe Code is as follows:
For (var I = 0, len = a. length; I <len; I ++ ){
Alert (a [I]);
}

This method is cyclically listed, but sometimes this method does not necessarily work.
For example:
Copy codeThe Code is as follows:
Var a = {"first": 1, "second": 2, "third": 3 };

At this time, we can only use for in to make effort.

Whether an object can be used for in is exhaustive. We can use the propertyIsEnumerable attribute to determine whether an object can be used for in, which is described as follows:
PropertyIsEnumerable attributes
Returns a Boolean value indicating whether the specified attribute is a part of an object and whether the attribute is enumerable.
Copy codeThe Code is as follows:
Object. propertyIsEnumerable (proName)

Parameters
Object
Required. An object.

ProName
Required. The string value of an attribute name.

Description
If the proName exists in the object and you can use a... If the In loop is exhausted, the propertyIsEnumerable attribute returns true. If the object does not have a specified attribute or the specified attribute is not enumerable, The propertyIsEnumerable attribute returns false. Typically, predefined attributes are not configurable, and user-defined attributes are always configurable.
The propertyIsEnumerable attribute does not consider objects in the prototype chain.

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.