After using babel,class inheritance, the new.target of the parent class is equal to undefined. Is there a solution?

problem description

after using babel,class inheritance, the new.target of the parent class is equal to undefined. Is there a solution?

the environmental background of the problems and what methods you have tried

https://github.com/babel/babe...

related codes

var A = class A {
  constructor() {
    console.log(new.target, "a");
  }
}

var B = class B extends A {
  constructor() {
    super();
    console.log(new.target, "b");
  }
}

var obj = new B("b"); //  false

running directly on node returns

[Function: B] "a"
[Function: B] "b"

but if babel is used, it returns

undefined "a"
[Function: B] "b"
Jun.22,2022

because after babel translation, super () in subclass B is replaced by A.constructor.call () , so new.target is of course undefined


cannot be solved. Not all features can be compiled consistent with the new version. You have to find a way around


to temporarily replace new.target with this.constructor.

Menu