Created
May 19, 2019 01:09
-
-
Save jasonayre/5d9ebd64299bf69c8637a9e03e33a3fb to your computer and use it in GitHub Desktop.
getInstanceMethods
This file contains 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 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