lundi 19 novembre 2018

Accessing submodules methods from main module?

What is the best practise in Javascript, if any, to access submodules methods from the main module?

We need to enlarge the project and want to hierarchize the modules right from the beginning, and thus be sure that things get done nice and neat right from the start. Our project was large, and as we are not pros we didn't use modules nor singletons, we put all the functions into global scope (I know now, it's a mess), and now we are facing a huge burden to reorganize everything. So, apologies if it's newbie question, but before allocating many men-hours, we want to be sure everything follows the best practises.

Is this approach correct and within best practises?

//in file Module.js
var Module = (function() {

    //shall I put something here, to inform others about a submodule?
    this.Utils = (function(){})();

    function B() {
        console.log("Module: B");
        
        // accessing submodule public methods 
        // by name or this?
        Module.Utils.C(); 
        this.Utils.C();
    };

    return {
        B: B
    };

})();


//in file Utils.js
Module.Utils = (function() {

    function C() {
        console.log("Module.Utils: C");
    };

    return {
        C: C
    }

})();

(function(){
    Module.B();
})();

Aucun commentaire:

Enregistrer un commentaire