Combined functions and functionsIn

Makes more sense that way, following the example of flatten.
This commit is contained in:
Angelos Chalaris
2018-01-12 13:37:33 +02:00
parent ba24e72537
commit 56b631da0d
3 changed files with 9 additions and 26 deletions

View File

@ -1,11 +1,15 @@
### functions
Returns an array of function property names from own enumerable properties of object.
Returns an array of function property names from own (and optionally inherited) enumerable properties of an object.
Use `Object.keys(obj)` to iterate over the object's own properties, `Array.filter()` to keep only those that are functions.
Use `Object.keys(obj)` to iterate over the object's own properties.
If `inherited` is `true`, use `Object.get.PrototypeOf(obj)` to also get the object's inherited properties.
Use `Array.filter()` to keep only those properties that are functions.
Omit the second argument, `inherited`, to not include inherited properties by default.
```js
const functions = obj => Object.keys(obj).filter(key => typeof obj[key] === 'function');
const functions = (obj, inherited = false) =>
(inherited ? [...Object.keys(obj), ...Object.keys(Object.getPrototypeOf(obj))] : Object.keys(obj)).filter(key => typeof obj[key] === 'function');
```
```js
@ -13,5 +17,7 @@ function Foo() {
this.a = () => 1;
this.b = () => 2;
}
Foo.prototype.c = () => 3;
functions(new Foo()); // ['a', 'b']
functions(new Foo(), true); // ['a', 'b', 'c']
```