Last active
May 29, 2023 05:05
-
-
Save ceving/2fa45caa47858ff7c639147542d71f9f to your computer and use it in GitHub Desktop.
JavaScript ancestor class list
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
/** | |
* 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