Gets an array of function property names from own (and optionally inherited) enumerable properties of an object.
Object.keys()
to iterate over the object's own properties.inherited
is true
, use Object.getPrototypeOf()
to also get the object's inherited properties.Array.prototype.filter()
to keep only those properties that are functions.inherited
, to not include inherited properties by default.const functions = (obj, inherited = false) =>
(inherited
? [...Object.keys(obj), ...Object.keys(Object.getPrototypeOf(obj))]
: Object.keys(obj)
).filter(key => typeof obj[key] === 'function');
function Foo() {
this.a = () => 1;
this.b = () => 2;
}
Foo.prototype.c = () => 3;
functions(new Foo()); // ['a', 'b']
functions(new Foo(), true); // ['a', 'b', 'c']
JavaScript, Object
Retrieves a set of properties indicated by the given selectors from an object.
JavaScript, Object
Omits the key-value pairs corresponding to the keys of the object for which the given function returns falsy.
JavaScript, Object
Creates an object composed of the properties the given function returns truthy for.