Get javascript class name or typeof in parent constructor

前端 未结 2 601
遥遥无期
遥遥无期 2021-01-26 11:49

I have two classes in Javascript like this:

class Parent {
    constructor(){
        console.log(typeof this);
    }
}

class Child extends Parent {
    constru         


        
2条回答
  •  春和景丽
    2021-01-26 12:26

    Since you are using ES6 classes, new.target is what you are looking for. But notice that it's usually an antipattern to let a constructor's behaviour depend on particular child classes.

提交回复
热议问题