diff --git a/snippets/functions.md b/snippets/functions.md index fff51096a..3c9550d34 100644 --- a/snippets/functions.md +++ b/snippets/functions.md @@ -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'] ``` diff --git a/snippets/functionsIn.md b/snippets/functionsIn.md deleted file mode 100644 index 09a1d4d34..000000000 --- a/snippets/functionsIn.md +++ /dev/null @@ -1,22 +0,0 @@ -### functionsIn - -Returns an array of function property names from own and inherited enumerable properties of object. - -Use `Object.keys(obj)` and `Object.get.PrototypeOf(obj)` to iterate over the object's own and inherited properties, `Array.filter()` to keep only those that are functions. -Use the spread operator (`...`) to combine all returned property names into one array. - -```js -const functionsIn = obj => - [...Object.keys(obj), ...Object.keys(Object.getPrototypeOf(obj))].filter( - key => typeof obj[key] === 'function' - ); -``` - -```js -function Foo() { - this.a = () => 1; - this.b = () => 2; -} -Foo.prototype.c = () => 3; -functionsIn(new Foo()); // ['a', 'b', 'c'] -``` diff --git a/tag_database b/tag_database index 812a08a5c..d9ad2a94e 100644 --- a/tag_database +++ b/tag_database @@ -52,7 +52,6 @@ formatDuration:date,math,string,utility fromCamelCase:string functionName:function,utility functions:object,function -functionsIn:object,function gcd:math,recursion geometricProgression:math getDaysDiffBetweenDates:date