Nest all content into snippets

This commit is contained in:
Angelos Chalaris
2023-05-07 16:07:29 +03:00
parent 2ecadbada9
commit 6a45d2ec07
1240 changed files with 0 additions and 0 deletions

View File

@ -0,0 +1,33 @@
---
title: Function property names
type: snippet
language: javascript
tags: [object,function]
cover: palm-tree-house
dateModified: 2020-10-20T11:21:07+03:00
---
Gets an array of function property names from own (and optionally inherited) enumerable properties of an object.
- Use `Object.keys()` to iterate over the object's own properties.
- If `inherited` is `true`, use `Object.getPrototypeOf()` to also get the object's inherited properties.
- Use `Array.prototype.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, inherited = false) =>
(inherited
? [...Object.keys(obj), ...Object.keys(Object.getPrototypeOf(obj))]
: Object.keys(obj)
).filter(key => typeof obj[key] === 'function');
```
```js
function Foo() {
this.a = () => 1;
this.b = () => 2;
}
Foo.prototype.c = () => 3;
functions(new Foo()); // ['a', 'b']
functions(new Foo(), true); // ['a', 'b', 'c']
```