Series of links:
Js traversal object -for… And in the Object. The keys
Js traversal object (3) -Iterator
Enumerable
Common attributes added through assignment operations are enumerable and are enumerated to (for… In or object.keys etc.). The Default Enumerable value in the Object.defineProperty() method is false, and it only appears in the Object’s enumerable property if and only if it is true.
const obj = {
name: 'lsc'.age: 21
}
// defineProperty Add attributes that are not enumerable by default
// Object.defineProperty(obj, 'height', {
// value: 187
// })
// console.log(Object.entries(obj)); // [ [ 'name', 'lsc' ], [ 'age', 21 ] ]
Object.defineProperty(obj, 'height', {
enumerable: true./ / can be enumerated
value: 187
})
console.log(Object.entries(obj)); // [ [ 'name', 'lsc' ], [ 'age', 21 ], [ 'height', 187 ] ]
Copy the code
PropertyIsEnumerable Checks whether it is enumerable
Every object has a propertyIsEnumerable method. This method determines whether a property specified in an object can be for… The in loop enumerates, except for properties inherited through the stereotype chain.
let arr = [1.2.3]
console.log(arr.propertyIsEnumerable(0)); // true
console.log(arr.propertyIsEnumerable('length')); // false
// js built-in methods and properties are almost non-enumerable
Copy the code
Symbol
Symbol is the attribute name. When traversing the object, the attribute does not appear in the for… In, for… Of loop, will not be the Object. The keys (), Object, getOwnPropertyNames (), JSON. The stringify () returns.
/ / write 1
let obj = {};
obj[s1] = "aaa";
console.log(obj); // { [Symbol(s)]: 'sss' }
/ / write 2
let obj2 = {
[s2]: "bbb"
};
console.log(obj2);
/ / writing 3
let obj3 = {};
Object.defineProperty(obj3, s3, {
value: "ccc".enumerable: true
});
console.log(obj3); // { [Symbol(s333)]: 'ccc' }
Copy the code
The traversal method is introduced
1. Object.getOwnPropertyNames()
Object. GetOwnPropertyNames () method returns a specified by the Object of all its attributes of the attribute name (including not enumerated attribute but does not include Symbol value as the name of the attribute) consisting of an array. .
Features: Access to all its own properties, including non-enumerable properties
// Only the non-enumerable properties that can be obtained are validated
const obj = {
name: 'lsc'.age: 21
}
// defineProperty Add attributes that are not enumerable by default
Object.defineProperty(obj, 'height', {
value: 187
})
console.log(Object.getOwnPropertyNames(obj)); // [ 'name', 'age', 'height' ]
Copy the code
2. Object.getOwnPropertySymbols()
An array of all Symbol properties found on the given object itself
Features: all Symbol attributes can be obtained, including the non enumerable Symbol attributes
let s1 = Symbol('sss111')
let s2 = Symbol('sss222')
const obj = {
name: 'lsc',
[s1]: 'I'm s1'
}
Object.defineProperty(obj, s2, {
value: 'I'm s2' // s2 is an unenumerable symbol attribute
})
console.log(Object.getOwnPropertySymbols(obj)); // [ Symbol(sss111), Symbol(sss222) ]
console.log(obj[s2]); / / I'm s2
Copy the code
3.Reflect.ownKeys()
The reflect.ownkeys () method returns an array of the target object’s own property keys. Its return value equal to the Object. GetOwnPropertyNames (target). The concat (Object. GetOwnPropertySymbols (target))
Features: Access to all of its properties, including non-enumerable properties and Symbol properties
const obj = {
name: 'lsc'.age: 21
}
// defineProperty Adds attributes ordinary attributes that are not enumerable by default
Object.defineProperty(obj, 'height', {
value: 187
})
// defineProperty adds attribute default non-enumerable Symbol attribute
let weight = Symbol('weight')
Object.defineProperty(obj, weight, {
value: 170
})
console.log(Reflect.ownKeys(obj)); // [ 'name', 'age', 'height', Symbol(weight) ]
Copy the code
But none of the three methods can get the stereotype chain attribute