Created
April 30, 2020 03:14
-
-
Save RienNeVaPlus/fee2ee6b3eadf61b79245896357d7624 to your computer and use it in GitHub Desktop.
getAllPropertyNames(obj) collects property names of the entire prototype chain until it reaches `Object` by using `Object.getOwnPropertyNames()`
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
/** | |
* Similar to Object.getOwnPropertyNames(obj) but including the properties of the entire prototype chain | |
* @param obj | |
* @param maxChainLength | |
*/ | |
export function getAllPropertyNames( | |
obj: { new(): any }, | |
maxChainLength: number = 10 | |
): string[] { | |
let set: Set<string> = new Set(), i: number = 0; | |
do { i++; | |
Object.getOwnPropertyNames(obj).forEach(n => set.add(n)); | |
obj = Object.getPrototypeOf(obj); | |
} while(obj.constructor !== Object && i < maxChainLength); | |
return [...set]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment