I try to write a class A
that instantiate a class B
that inherit from A
. In JS, this is technically possible as shown on this JSFiddle and with this previous question: In JavaScript, can a class A instantiate an object B that inherit from A
With WebPack, I wrote the following files:
index.js
import A from "./a.js";
let a = new A();
a.fct();
a.js
import B from "./b.js"
export default class A {
constructor() {
console.log ("a constructor")
}
fct() {
this.b = new B();
}
}
b.js
import A from "./a.js"
export default class B extends A {
constructor() {
console.log ("b constructor")
}
}
When I try to run the above code, I’ve got the following error at runtime:
js:3 Uncaught ReferenceError: Cannot access 'A' before initialization
at Module.default (a.js:3:56)
at eval (b.js:3:32)
at ./src/js/b.js (bundle.425598cf4c8e29cd2b4d.js:723:1)
at __webpack_require__ (bundle.425598cf4c8e29cd2b4d.js:1278:41)
at eval (a.js:5:63)
at ./src/js/a.js (bundle.425598cf4c8e29cd2b4d.js:712:1)
at __webpack_require__ (bundle.425598cf4c8e29cd2b4d.js:1278:41)
at eval (index.js:2:63)
at ./src/js/index.js (bundle.425598cf4c8e29cd2b4d.js:821:1)
at __webpack_require__ (bundle.425598cf4c8e29cd2b4d.js:1278:41)
Am I doing something wrong or is it an issue with Webpack?
Note that since I’m not sure I also reported an issue on GitHub