Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save Uvacoder/f63c104d479606614d8d745e9ffbc190 to your computer and use it in GitHub Desktop.

Select an option

Save Uvacoder/f63c104d479606614d8d745e9ffbc190 to your computer and use it in GitHub Desktop.
Debugging utility for enumerating function methods on a class instance in Javascript
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