1 Object.keys(), Object.values(), Object.entries() series of traversable Object’s own enumerable properties (excluding Symbol key properties) Symbol attribute), suitable for array traversal methods such as forEach, for of

Eg1: traverses its own attributes, but not the attributes on the prototype chain, non-enumerated attributes, and symbol attributes

let obj = Object.create({ inheritProp: 1 }, { noneEnumbleProp: { value: 2 }});
obj.selfProp = 3;
const symbol = Symbol('prop');
obj[symbol] = 4;
Object.keys(obj); // ["selfProp"]
Copy the code

Eg2: used with array traversal methods

let obj = { a: 1, b: 2, c: 3 }; Object.keys(obj).forEach(key => { obj[key] = obj[key] + 1; }); const newObj = {}; for (let [key, value] of Object.entries(obj)) { if (value % 2 === 0) { newObj[key] = value; }}Copy the code

2 Object. GetOwnPropertyNames () can traverse the property itself, not including symbol attribute Usage, and Object. The keys (), the difference is than Object. The keys () more traversal itself not enumerated attribute

Eg: Can traverse its own attributes (enumeration + non-enumeration), cannot traverse the attributes on the prototype chain, cannot traverse the symbol attribute

let obj = Object.create({ inheritProp: 1 }, { noneEnumbleProp: { value: 2 }});
obj.selfProp = 3;
const symbol = Symbol('prop');
obj[symbol] = 4;
Object.getOwnPropertyNames(obj); // ["noneEnumbleProp", "selfProp"]
Copy the code

3 For in can iterate through the prototype chain and its own enumerable properties, excluding the symbol property, cannot iterate through non-enumerable properties

Eg: The symbol attribute cannot be traversed. Non-enumerated attributes cannot be traversed

let obj = Object.create({ inheritProp: 1 }, { noneEnumbleProp: { value: 2 }});
obj.selfProp = 3;
const symbol = Symbol('prop');
obj[symbol] = 4;
for (let prop in obj) {
    console.log(prop);
}
// selfProp
// inheritProp
Copy the code

4 Object. GetOwnPropertySymbols () can traverse the symbol itself attribute (enumeration + not enumerated)

eg:

let obj = Object.create({ inheritProp: 1 }, { noneEnumbleProp: { value: 2 }});
obj.selfProp = 3;
const symbol1 = Symbol('prop1');
const symbol2 = Symbol('prop2');
obj[symbol1] = 4;
Object.defineProperty(obj, symbol2, { value:5 });
Object.getOwnPropertySymbols(obj); // [Symbol(prop1), Symbol(prop2)]
Copy the code

5. Illustrate the differences between the above traversal methods