Introduction to three methods for Traversing Object property in JavaScript, javascriptproperty
In JavaScript, you can use three methods to traverse the property of an object:
1. for/in. You can use the for/in statement to traverse the object's Own property (Own Property) and the property inherited from the prototype object. Only the property of enumerable can be traversed.
2. Object. keys (). You can pass an Object as a parameter to Object. keys (). The Object. keys () Statement returns an array consisting of all property name strings. The Object. keys () statement only returns the Object's Own (Own Property) and enumerable property. This statement is only valid in the ECMAScript 5 standard.
3. Object. getOwnPropertyNames (). You can pass an Object as a parameter to Object. getOwnPropertyNames (). Like Object. keys (), this statement returns an array consisting of all property name strings. Unlike Object. keys (), the Object. getOwnPropertyNames () Statement returns the property (Own Property) of all objects, regardless of whether the Object is enumerable. This statement is only valid in the ECMAScript 5 standard.
The above information is summarized as follows:
Lab:
Copy codeThe Code is as follows:
Var o = {x: 1, y: 2 };
Var a = Object. create (o );
A. z = 3;
For (p in ){
Console. log (p );
} // Z x y
Console. log (Object. keys (a); // ["z"]
Console. log (Object. getOwnPropertyNames (a); // ["z"]