是否可以从基构造函数获取派生类名称?
class Entity {
constructor() {
// how to log here a class?
console.log('a')
}
}
class a extends Entity {}
new a()
最佳答案
您可以使用 Function.name
在 JavaScript/TypeScript 中输出函数名称,参见this answer :
class Entity {
constructor() {
console.log(this.constructor.name)
}
}
class A extends Entity {}
const a = new A();
console.log(a.constructor.name);
关于javascript - 如何从基类获取派生类名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54855932/