Home > Web Front-end > JS Tutorial > Analysis of usage examples of for in in js_javascript skills

Analysis of usage examples of for in in js_javascript skills

WBOY
Release: 2016-05-16 17:07:07
Original
1393 people have browsed it

Usage like for(var i=0;iFor example:

Copy code The code is as follows:

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

This is the exhaustive list All elements in a. Of course, in the above example, you can use
to copy the code . The code is as follows:

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

Loop in this way listed, 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, we can judge it through the propertyIsEnumerable attribute. The description is as follows:
propertyIsEnumerable attribute
returns a Boolean value to indicate whether the specified property is part of an object. and whether the property is enumerable.
Copy code The code is as follows:

object.propertyIsEnumerable(proName)

Parameter
object
Required. an object.

proName
Required. A string value for the property name.

Explanation
If proName exists in object and can be enumerated using a For...In loop, then the propertyIsEnumerable property returns true. The propertyIsEnumerable property returns false if the object does not have the specified property or if the specified property is not enumerable. Typically, predefined properties are not enumerable, whereas user-defined properties are always enumerable.
propertyIsEnumerable property does not consider objects in the prototype chain.
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