如何从基类获取派生类名称?

yan*_*rab 1 javascript typescript

是否可以从基构造函数中获取派生类名称?

class Entity {
    constructor() {
         // how to log here a class?
         console.log('a')
    }
}
class a extends Entity {}
new a()
Run Code Online (Sandbox Code Playgroud)

jo_*_*_va 7

您可以使用 JavaScript/TypeScript 输出函数的名称Function.name,请参阅此答案

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

class A extends Entity {}
const a = new A();
console.log(a.constructor.name);
Run Code Online (Sandbox Code Playgroud)