Search code examples
javascriptmodel-view-controllerbackbone.js

how to hide the global name space object in module base application


how to avoid accessing name space object.

window.myApp = window.myApp || {};

myApp.Base.Controller = myApp.extend({});
myApp.Base.Model = myApp.extend({});
myApp.Base.View = myApp.extend({});

here i am attaching everything to the myApp and myApp will be avail in window object.

how can i hide myApp Object accessing from window level.


Solution

  • As @Człowiek Fin Śpiewak already suggested, you can wrap the code in an IIFE:

    (function(){
      var myApp = {};
      myApp.Base.Controller = myApp.extend({});
      myApp.Base.Model = myApp.extend({});
      myApp.Base.View = myApp.extend({});
    })();
    

    I also suggest looking into