Converting Javascript 'namespaces' over multiple ES modules

大憨熊 提交于 2021-02-08 10:36:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!