Last active
August 29, 2015 14:11
-
-
Save graemeworthy/5eed14d1c565805a55bc to your computer and use it in GitHub Desktop.
How to namespace your Javascript to be load order independent
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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