Home > Web Front-end > JS Tutorial > Explanation on the usage of for in statement in js_javascript skills

Explanation on the usage of for in statement in js_javascript skills

WBOY
Release: 2016-05-16 16:02:35
Original
1541 people have browsed it

Copy code The 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
Inside the loop body, a property name of the object is assigned to the variable as a string.

Note: Some properties of the object are marked as read-only, permanent (non-deletable) or non-enumerable in the same way. These properties cannot be enumerated using for/in loops. Although all user-defined properties are enumerable, many internal properties, including all internal methods, are not enumerable. In addition, objects can inherit properties from other objects, and those inherited user-defined properties can be enumerated using a for/in loop.

Usage like

for(var i=0;i

For example:

Copy code The code is as follows:

var a = ["a","b","c"];
for(var el in a){
alert(a[el]);
}

This is to enumerate all the elements in a. Of course, the above example can be used
Copy code The code is as follows:

for(var i=0,len=a.length;i alert(a[i]);
}

This method is used to list in a loop, but sometimes this method may not work.
For example:
Copy code The code is as follows:

var a = {"first":1,"second":2,"third":3};

At this time, you can only use for in to exhaust the list.

Whether an object can be exhaustively for in can be judged by the propertyIsEnumerable attribute. The description is as follows:

Whether object.propertyIsEnumerable(propname) can see the property through for/in loop
propname is 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 a for/in loop), return true

Description:

You can use the for/in statement to traverse the "enumerable" properties of an object, but not all properties of an object are enumerable. Properties added to the object through JavaScript code are enumerable, and internal Predefined properties of objects (such as methods) are usually not enumerable.

The propertyIsEnumerable() method does not check the prototype chain, which means it only applies to local properties of the object and cannot detect the enumerability of inherited properties

Copy code The 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 labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template