As showed here (or here) we can use external class definition in many browsers and NodeJs... But the most useful way to load the external class is
import('./MyClass.js').then(({default: MyClass}) => {
let x = new MyClass(); // using here!
// ... but it is not global, is AN ISLAND IN A BLOCK
}); // async loading
... but it is not global, is an island in a async block. So, how to do it globally?
TESTING GLOBAL ALTERNATIVES AND ERRORS:
const MyClass = () => import('/MyClass.js'); // ok, but...
let x = new MyClass()
// Uncaught TypeError: MyClass is not a constructor
const MyClass = await import('/MyClass.js');
// Uncaught SyntaxError: await is only valid in async function
The module = await import(moduleFile)
form is suggested here.
For "global class" suppose an external Javascript file MyClass.js
like this:
export default class MyClass {
constructor(x) {
this.val=x? x: "Hello!"
console.log("MyClass ok...")
}
}