问题
In some older codes I 'namespaced' shared methods like this (assuming mycomp
is a company name):
//File mycomp-common.js
var mycomp = window.mycomp || {};
mycomp.common = {
//shared methods and constants
someMethod: function() {
...
}
};
//File mycomp-myapp-common.js
var mycomp = window.mycomp || {};
mycomp.myapp = mycomp.myapp || {};
mycomp.myapp.common = {
//shared methods and constants for myapp
someOtherMethod: function() {
...
}
};
As you can see both files make sure that the main namespace mycomp
exists. The first file places company wide utilities in the main namespace. The second file creates an application specific sub-namespace and places its shared methods there. These files are currently loaded top-level in my index.html.
The consuming code accesses methods like this
let result = mycomp.myapp.someOtherMethod();
How do I convert these codes to ES modules for better modularization, lazy loading and bundling?
来源:https://stackoverflow.com/questions/62910519/converting-javascript-namespaces-over-multiple-es-modules