Is it possible to get the non-enumerable inherited property names of an object?

后端 未结 9 1890
温柔的废话
温柔的废话 2020-11-22 12:46

In JavaScript we have a few ways of getting the properties of an object, depending on what we want to get.

1) Object.keys(), which returns all own, enu

9条回答
  •  北恋
    北恋 (楼主)
    2020-11-22 13:35

    An implementation in my personal preferences :)

    function getAllProperties(In, Out = {}) {
        const keys = Object.getOwnPropertyNames(In);
        keys.forEach(key => Object.defineProperty(In, key, {
            enumerable: true
        }));
        Out = { ...In, ...Out };
    
        const Prototype = Object.getPrototypeOf(In);
        return Prototype === Object.prototype ? Out : getAllProperties(Proto, Out);
    }
    

提交回复
热议问题