Javascript: Extend Classes With Undefined Class
Consider the following two files: /* file A.js */ class A extends B { } /* file B.js */ class B { } If I accidentaly load these two files into the browser in the inverse order
Solution 1:
I'd resolve it by using modules. Modules declare their dependencies explicitly and you only have to reference the top-level module in the page; the rest will be loaded according to the dependency tree. So for instance, B.js
would be:
exportclassB {
// ...
}
Then A.js
would be:
import { B } from"./B.js";
exportclassAextendsB {
// ...
}
Perhaps your main entry point (main.js
or whatever) would have:
import { A } from"./A.js";
// ...
Then your page has
<scripttype="module"src="./main.js"></script>
...and the other dependencies are loaded as needed.
This works with native module support in browsers, and/or if you use a bundler like Rollup.js, Webpack, ...
Post a Comment for "Javascript: Extend Classes With Undefined Class"