In JavaScript, you can traverse the property of an object in three ways:
1.for/in. You can use the For/in statement to traverse the property of the object itself and the property that inherits from the prototype object, and only the property of the enumerable is traversed to the Own.
2.object.keys (). You can pass an object as a parameter to Object.keys (), and the Object.keys () statement returns an array of all the property name strings. The Object.keys () statement returns only the property of the object itself (Own property) and the enumerable property. The statement is valid only in the ECMAScript 5 standard.
3.object.getownpropertynames (). You can pass an object as a parameter to Object.getownpropertynames (), and, like Object.keys (), the statement returns an array of all the property name strings. Unlike Object.keys (), the Object.getownpropertynames () statement returns the property (the Own property) of all objects, regardless of whether they are enumerable. The statement is valid only in the ECMAScript 5 standard.
Combined with the above information, summarized in the following diagram:
Experiment:
Copy Code code as follows:
var o = {x:1, y:2};
var a = Object.create (o);
A.Z = 3;
For (p in a) {
Console.log (P);
}//z x y
Console.log (Object.keys (a));//["Z"]
Console.log (Object.getownpropertynames (a));//["Z"]