Skip to content

Instantly share code, notes, and snippets.

@ceving
Last active May 29, 2023 05:05
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ceving/2fa45caa47858ff7c639147542d71f9f to your computer and use it in GitHub Desktop.
Save ceving/2fa45caa47858ff7c639147542d71f9f to your computer and use it in GitHub Desktop.
JavaScript ancestor class list
/**
* Returns the list of ancestor classes.
*
* Example:
* ancestors(HTMLElement) .map (e => e.name || e.constructor.name)
* => [ "HTMLElement", "Element", "Node", "EventTarget", "Function", "Object" ]
*/
function ancestors (anyclass)
{
switch (true) {
case (anyclass === undefined): return;
case (anyclass === null): return [];
default:
return [anyclass, ...(ancestors (Object.getPrototypeOf (anyclass)))];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment