Object. The keys () and the Object. GetOwnPropertyNames () the difference between the additional symbol (knowledge)
1.Object.keys()
The object.keys () method returns an array of the self-enumerable (True) properties of a given Object, the names of which are listed in the same order as they would be returned by a normal loop through the Object.
const myobject={
name:'Saku'.age:'21'.say(){
console.log("Hello,world")}}Object.defineProperty(myobject,'address', {// Definitions are not enumerable
enumerable:false.value:'It's a secret.',})console.log(Object.keys(myobject))
// Array ["name", "age", "say"]
Copy the code
2.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.
const myobject={
name:'Saku'.age:'21'.say(){
console.log("Hello,world")}}Object.defineProperty(myobject,'address', {// Definitions are not enumerable
enumerable:false.value:'It's a secret.',})console.log(Object.getOwnPropertyNames(myobject));
// Array ["name", "age", "say", "address"]
Copy the code
3. The Chinese translation is a Symbol.
Object. GetOwnPropertyNames () referred to in the explanation but does not include the Symbol value as the name of the attribute, is can’t get the Symbol value as the name of the attribute.
The data type “symbol” is a basic data type whose value can be used to create anonymous object properties. Symbol is used to create anonymous object attributes.
const _item=Symbol('item')
const myobject={
name:'Saku'.age:'21',
[_item]:'mysymbol'.say(){
console.log("Hello,world")}}Object.defineProperty(myobject,'address', {// Definitions are not enumerable
enumerable:false.value:'It's a secret.',})console.log(Object.getOwnPropertyNames(myobject));
//Array ["name", "age", "say", "address"]
Copy the code