Method to obtain the object object property, Reflect.ownKeys , Object.getOwnPropertyNames , Object.getOwnPropertySymbols , Object.keys ,for in

Time:2020-10-22
let triangle={
a:1,
b:2,
c:3
}

 

function coloTriangle(){
this.color=’red’;
}

 

coloTriangle.prototype=triangle;
let color=new coloTriangle();
Object.defineProperty(color,’auto’,{
value:’auto’
})
let symbolProp=Symbol(‘symbol’);
color[symbolProp]=’symbol’
/**
* Reflect.ownKeys:Object.getOwnPropertyNames(target).concat(Object.getOwnPropertySymbols)
*None of them come from inheritance. They are all own
*It can be traversed regardless of enumeration or not
*You can also get the symbol type attribute
*/
Reflect.ownKeys(color) //[“color”,”auto”,Symbol(symbol)]

 

/**
* Object.getOwnPropertyNames
*None from inheritance, all own
*It can be traversed regardless of enumeration or not
*Unable to get symbol type attribute
*/
Object.getOwnPropertyNames(color);//[“color”,”auto”]

 

/**
* Object.getOwnPropertySymbols
*Only own
*Whether or not to enumerate
*Only symbol type properties can be obtained
*/

 

Object.getOwnPropertySymbols(color);//[Symbol(symbol)]

 

/**
* Object.keysIt’s a little pretentious
*Only own
*And to enumerate
*Symbol type attribute not available
*
*/
Object.keys(color);//[“color”]

/**
* for in That is to say, take one of them
*Don’t need own, can come from inherited property
*But it must be enumerated
*Symbol type attribute not available
*/
for(let i in color){
console.log(i);//color a b c
}
 
 
 
 
Ha ha ha, if there is anything else, please add it