1

Is there a way to get class name that extends the class?

class Foo {
  constructor(){
    console.log(extendedBy.name);//extendedBy.name to be replaced with "Bar"
  }
}

class Bar extends Foo {
  constructor(){
    super();
  }
}

I tried using Google and Stack but only found how to get the parent (I.E. Foo) name.

Thanks in advance.

Dan B.
  • 93
  • 1
  • 1
  • 12
  • Are you looking to do something like this? https://stackoverflow.com/questions/31618212/find-all-classes-in-a-javascript-application-that-extend-a-base-class – sbeliv01 Oct 02 '19 at 15:41

1 Answers1

0

Check the this.constructor.name :

class Foo {
  constructor(){
    console.log('Foo extendedBy : ', this.constructor.name);
  }
}

class Bar extends Foo {
  constructor(){
    super();
  }
}

class OtherBar extends Foo {
  constructor(){
    super();
  }
}

class ByBar extends Bar {
  constructor(){
    super();
  }
}

const MyBar = new Bar();
const MyOtherBar = new OtherBar();
const FromBar = new ByBar();
Taki
  • 17,320
  • 4
  • 26
  • 47