The usage of the for in statement in js, And the js statement

Source: Internet
Author: User

The usage of the for in statement in js, And the js statement

Copy codeThe Code is as follows:
For (variable in object)
Statement

Variable is a var statement that declares a variable, an element of an array or an attribute of an object.
Within the loop body, an attribute name of an object is assigned to the variable as a string.

Note: Some attributes of an object are marked as read-only, permanent (undeletable), or unenumerable in the same way. These attributes cannot be enumerated using the for/in loop. Although all user-defined attributes can be enumerated, many internal attributes, including all internal methods, cannot be enumerated. In addition, the object can inherit the attributes of other objects. The inherited user-defined attributes can be enumerated cyclically using for/in.

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:

Whether object. propertyIsEnumerable (propname) can be seen through the for/in Loop
Propname: a string containing the name of the object attribute.
If the object has a non-inherited property named propname and the property is enumerable (that is, it can be enumerated using the for/in loop), true is returned.

Description:

The for/in statement can be used to traverse the "enumeration" attribute of an object, but not all attributes of an object are enumerable, the attributes added to an object through JavaScript code can be enumerated, while the predefined attributes (such as methods) of an internal object cannot be enumerated.

The propertyIsEnumerable () method does not detect the prototype chain. This means that it only applies to partial attributes of objects and does not detect the enumerable properties of inherited attributes.
Copy codeThe Code is as follows:
Var o = new Object ();
O. x = 3.14;
O. propertyIsEnumerable ("x"); // true
O. propertyIsEnumerable ("y"); // false have not the property
O. propertyIsEnumerable ("toString"); // false inherited
Object. prototype. propertyIsEnumerable ("toString"); // false nonenumerable

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.