1

I'd like to walk up the super type chain all the way to Object, so given:

abstract class Foo {}

abstract class Bar extends Foo {}

abstract class Baz extends Bar {}

class Yolo extends Baz {}

let yolo = new Baz()

How can I get all the extended classes Baz, Bar, Foo, and Object with an instance of yolo?

I saw How to get the parent class at runtime and I don't see how that would help me, I can only go up one level.

Lev Kuznetsov
  • 3,520
  • 5
  • 20
  • 33

1 Answers1

2

You can use getPrototypeOf successively to walk up the super chain:

var ctor = yolo.constructor;
while(ctor !== null){
    console.log(ctor);
    ctor = Object.getPrototypeOf(ctor);    
}

Will output:

function Yolo() { … }
function Baz() { … }
function Bar() { … }
function Foo() { … }
function () { … }
Object { … }
Titian Cernicova-Dragomir
  • 230,986
  • 31
  • 415
  • 357