Created
October 30, 2013 12:57
-
-
Save jefsnare/7232239 to your computer and use it in GitHub Desktop.
JS pattern
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
// top-level namespace | |
var myApp = myApp || {}; | |
// directly assign a nested namespace | |
myApp.library = { | |
foo:function(){ | |
//... | |
} | |
}; | |
// deep extend/merge this namespace with another | |
// to make things interesting, let's say it's a namespace | |
// with the same name but with a different function | |
// signature: $.extend( deep, target, object1, object2 ) | |
$.extend( true, myApp, { | |
library:{ | |
bar:function(){ | |
//... | |
} | |
} | |
}); | |
console.log("test", myApp); | |
// myApp now contains both library.foo() and library.bar() methods | |
// nothing has been overwritten which is what we're hoping for. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment