-
-
Save Uvacoder/f63c104d479606614d8d745e9ffbc190 to your computer and use it in GitHub Desktop.
Debugging utility for enumerating function methods on a class instance in Javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function getMethods(obj) { | |
| const excludeBuiltIns = Object.getOwnPropertyNames(Object.prototype) | |
| let properties = new Set() | |
| let currentObj = obj | |
| do { | |
| Object.getOwnPropertyNames(currentObj).map(item => properties.add(item)) | |
| } while ((currentObj = Object.getPrototypeOf(currentObj))) | |
| return [ | |
| ...properties.keys() | |
| ].filter((item) => { | |
| return !excludeBuiltIns.includes(item) && typeof obj[item] === 'function' | |
| }) | |
| } | |
| class Thing { | |
| foo() {} | |
| } | |
| var thing = new Thing() | |
| console.log(thing) | |
| // Thing {} | |
| console.log(getMethods(thing)) | |
| // ["foo"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment