Skip to content

Instantly share code, notes, and snippets.

@graemeworthy
Last active August 29, 2015 14:11
Show Gist options
  • Save graemeworthy/5eed14d1c565805a55bc to your computer and use it in GitHub Desktop.
Save graemeworthy/5eed14d1c565805a55bc to your computer and use it in GitHub Desktop.
How to namespace your Javascript to be load order independent
// The problem: Load order independence.
// Sometimes your js files are loaded out of order,
// How can you make sure all your modules are properly assembled.
var myApp = (function(existing){
var self = existing || {};
self.a = 'one';
return self;
}(myApp));
// Each module needs to acknowldge it's ancestors
var myApp = myApp || {};
myApp.SecondLevel = (function(existing){
var self = existing || {};
self.a = 'two';
return self;
}(myApp.SecondLevel));
// Each module needs to acknowldge it's ancestors
// the deeper you go, the more ancestors to acknowledge.
var myApp = myApp || {};
myApp.SecondLevel = myApp.SecondLevel || {};
myApp.SecondLevel.ThirdLevel = (function(existing){
var self = existing || {};
self.a = 'three';
return self;
}(myApp.SecondLevel.ThirdLevel));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment