Skip to content

Instantly share code, notes, and snippets.

@RienNeVaPlus
Created April 30, 2020 03:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RienNeVaPlus/fee2ee6b3eadf61b79245896357d7624 to your computer and use it in GitHub Desktop.
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()`
/**
* 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