Usage like for(var i=0;i<len;i++) can generally be replaced by for in.
For example:
Copy the code code 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 the code code as follows:
for(var i=0,len=a.length;i<len;i++){
alert(a[i]);
}
This method is used to list in a loop, but sometimes this method may not work.
For example:
Copy the code code 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 through the propertyIsEnumerable attribute. The description is as follows:
propertyIsEnumerable Property
Returns a Boolean value indicating whether the specified property is part of an object and whether the property is enumerable.
Copy the code code as follows:
object.propertyIsEnumerable(proName)
parameter
object
Required. an object.
proName
Required. A string value for the property name.
illustrate
The propertyIsEnumerable property returns true if proName exists in object and can be enumerated using a For...In loop. If the object does not have the specified property or the specified property is not enumerable, the propertyIsEnumerable property returns false. Typically, predefined properties are not enumerable, whereas user-defined properties are always enumerable.
The propertyIsEnumerable property does not consider objects in the prototype chain.