1

Is is possible to get derived class name from base constructor?

class Entity {
    constructor() {
         // how to log here a class?
         console.log('a')
    }
}
class a extends Entity {}
new a()
jo_va
  • 13,504
  • 3
  • 23
  • 47
yantrab
  • 2,482
  • 4
  • 31
  • 52
  • 1
    At the JavaScript level, `console.log(this.constructor.name)`. I don’t know if TypeScript provides something more static. – Ry- Feb 24 '19 at 19:59

1 Answers1

7

You can output the name of a function in JavaScript/TypeScript using Function.name, see this answer:

class Entity {
  constructor() {
    console.log(this.constructor.name)
  }
}

class A extends Entity {}
const a = new A();
console.log(a.constructor.name);
jo_va
  • 13,504
  • 3
  • 23
  • 47