The example below exports a class Example
. The class has several static properties, defined on multiple levels of the prototype chain but they're unfortunately not enummarable.
const symbol1 = Symbol('test1');
const symbol2 = Symbol('test2');
class Base {
static test1 = '...';
static test2() {
// ...
}
}
Base[symbol1] = '...'
class Example extends Base {
static test3 = '...';
static test4() {
// ...
}
}
Example[symbol2] = '...'
Thankfully Object.getOwnPropertyNames
and Object.getOwnPropertySymbols
is able to list these properties however it does not support recursive operations.
Object.getOwnPropertyNames(Example); // 5 Array ["length", "name", "prototype", "test4", "test3"]
Object.getOwnPropertySymbols(Example); // 1 Array [Symbol(test2)]
Hence, object-props
was created to simply get ALL or OWN (explicitly defined and not inherited from the Object.prototype
)
Returns an Array
of names for the object and all its inherited properties.
const { getAllProps } from 'object-props';
getAllProps(Example); // 24 Array ["test4", "test3", "test2", "test1", ...all]
Returns an Array
of names and Symbols
for the object and all its inherited properties.
const { getAllPropsAndSymbols } from 'object-props';
getAllPropsAndSymbols(Example); // 27 Array ["test4", "test3", Symbol(test2), "test2", "test1", Symbol(test1), ...all]
Returns an Array
of names for the object and its inherited properties excluding Object
prototype.
const { getOwnProps } from 'object-props';
getOwnProps(Example); // 7 Array ["length", "name", "prototype", "test4", "test3", "test2", "test1"]
Returns an Array
of names and Symbols
for the object and all its inherited properties excluding Object
prototype.
const { getOwnPropsAndSymbols } from 'object-props';
getOwnPropsAndSymbols(Example); // 9 Array ["length", "name", "prototype", "test4", "test3", Symbol(test2), "test2", "test1", Symbol(test1)]