Javascript module template / organization / submodules

  • I would like to know what is the difference (advantages / disadvantages) between the following patterns.
  • How to create sub-modules based on a module template?

My goal is for my js to be organized into several files that are lazy loaded but have the same namespace.

For instance:

SO.global (global.js) SO.global.registration (registration.js) <- load

var SO = function(){

    var CONSTANT = 'Z';

    function createX(){
      alert("create X");
    }

    function getY(){
       alert("get Y");
    }
    return{
      create:createX,
      get:getY
    }
}();

//SO.createX(); 
//SO.getY();

VS.

var SO = (function() {

    var CONSTANT = 'Z';

    function createX(){
      alert("create X");
    }

    function getY(){
       alert("get Y");
    }

    return {
      create:createX,
      get:getY
    }

} ());
+5
source share
2 answers

Did you consider Require.JS ? He is trying to provide the following solution:

  • Some type # include / import / require
  • , ,

Require.JS Module/Asynchronous Definition, Common.JS

+5

All Articles