Skip to content

Instantly share code, notes, and snippets.

@jasonayre
Created May 19, 2019 01:09
Show Gist options
  • Save jasonayre/5d9ebd64299bf69c8637a9e03e33a3fb to your computer and use it in GitHub Desktop.
Save jasonayre/5d9ebd64299bf69c8637a9e03e33a3fb to your computer and use it in GitHub Desktop.
getInstanceMethods
function isGetter (obj, prop) {
return !!obj.__lookupGetter__(prop)
}
export const getInstanceMethods = (obj) => {
let keys = []
let topObject = obj
const onlyOriginalMethods = (p, i, arr) => {
return !isGetter(topObject, p) &&
typeof topObject[p] === 'function' &&
p !== 'constructor' &&
(i === 0 || p !== arr[i - 1]) &&
keys.indexOf(p) === -1
}
do {
const l = Object.getOwnPropertyNames(obj)
.sort()
.filter(onlyOriginalMethods)
keys = keys.concat(l)
// walk-up the prototype chain
obj = Object.getPrototypeOf(obj)
} while (
// not the the Object prototype methods (hasOwnProperty, etc...)
obj && Object.getPrototypeOf(obj)
)
return keys
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment