Object.getOwnPropertyNames()
returns an array containing all the names of the own properties of the object passed as argument, including non-enumerable properties. It does not consider inherited properties.
Object.getOwnPropertyNames()
返回一个数组,其中包含作为参数传递的对象自身属性的所有名称,包括不可枚举的属性。 它不考虑继承的属性。
Non enumerable properties are not iterated upon. Not listed in for..of loops, for example.
不可迭代的属性不会被迭代。 例如,未在for..of循环中列出。
To get only a list of the enumerable properties you can use Object.keys()
instead.
要仅获取可枚举属性的列表,可以使用Object.keys()
代替。
Example:
例:
const dog = {}
dog.breed = 'Siberian Husky'
dog.name = 'Roger'
Object.getOwnPropertyNames(dog) //[ 'breed', 'name' ]
翻译自: https://flaviocopes.com/javascript-object-getownpropertynames/