Object property traversal method
for... in
Object.keys/Object.values/Object.entries
Object.getOwmPropertyName
Traversal methods | Its properties | Inherit the attributes | The enumeration of | Symbol type |
---|---|---|---|---|
for… in | Their own | inheritance | Can be enumerated | Does not contain |
Object.keys() | Their own | Can be enumerated | Does not contain | |
Object.getOwnPropertyNames() | Their own | Enumerable and not enumerable | Does not contain | |
Object.getOwnPropertySymbols() | Their own | All Symbol attributes | ||
Reflect.ownKeys() | Their own | Enumerable and not enumerable | contains |
Only for… In is to be able to access to the property on the prototype, in addition to Reflect the Ownkeys () and Object getOwnpropertySymbols outside can access to the Symbol properties As long as the belt ‘Own’ are ignored an enumeration, that is to say an enumeration attributes can have access to
How can I tell if an object is an empty object
function ObjisEmpty(obj) {
returnobj ! = =null
&& typeof obj === 'object'&&!Array.isArray(obj)
&& (Object.getOwnPropertyNames(obj).length === 0)
&& (Object.getOwnPropertySymbols(obj).length === 0)}// or
function ObjisEmpty(obj) {
return (Object.prototype.toString.call(obj) === '[object Object]')
&& (Object.getOwnPropertyNames(obj).length === 0)
&& (Object.getOwnPropertySymbols(obj).length === 0)}// or
function ObjisEmpty(obj) {
return (String(obj) === '[object Object]') && (Reflect.ownKeys(obj).length === 0)}Copy the code