在父亲的构造函数中获取子类名


class Grandfather1{
constructor(childClassName){}
}
class Father1 extends Grandfather1{
constructor(){
super(childClassName (=== Child1) )
}
}
class Child1 extends Father1{
constructor(){
super() // without passing `Child1` as parameter
}
}

我有40多个儿童班。我正在努力节省代码和时间。

有什么办法吗?

class Father1{
constructor(){
const childClassName = this.constructor.name
console.log(childClassName)
}
}
class Child1 extends Father1{
constructor(){
super() // without passing `Child1` as parameter
}
}
new Child1();

编辑

class Grandfather1{
constructor(){
console.log(this.constructor.name)
}
}
class Father1 extends Grandfather1{
constructor(){
super()
}
}
class Child1 extends Father1{
constructor(){
super() // without passing `Child1` as parameter
}
}
new Child1

最新更新