Home > Article > Web Front-end > Explanation on the usage of for in statement in js_javascript skills
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 likefor(var i=0;i For example: 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 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
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
for(var i=0,len=a.length;i
}
This method is used to list in a loop, but sometimes this method may not work.
For example:
var a = {"first":1,"second":2,"third":3};
At this time, you can only use for in to exhaust the list.
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
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