Last active
October 2, 2015 00:32
-
-
Save javidjamae/2e83ef1162f7f53fdfe6 to your computer and use it in GitHub Desktop.
Javascript Dependency Injector
This file contains hidden or 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
(function() { | |
window.JDI = window.JDI || {}; | |
/* | |
JDI.register - register a module on the "JDI" global | |
Usage: | |
with a namespace: JDI.register("foo", {bar: myFunc}) => JDI.foo.bar = myFunc | |
without a namespace: JDI.register({bar: myFunc}) => JDI.bar = myFunc | |
*/ | |
JDI.register = function(){ | |
var root = window.JDI, | |
mod = arguments[0]; | |
// if there is an optional namespace defined | |
if(arguments.length === 2){ | |
var namespace = arguments[0]; | |
root[namespace] = root[namespace] || {}; | |
root = root[namespace]; | |
mod = arguments[1]; | |
} | |
for (var prop in mod) { | |
if (mod.hasOwnProperty(prop)) { | |
root[prop] = mod[prop]; | |
} | |
} | |
}; | |
})(); | |
// Examples: | |
JDI.register( { | |
blah: "blah!", | |
foo: function() {} | |
}); | |
console.log(JDI.blah); | |
JDI.foo(); | |
JDI.register("myModule", { | |
bar: "bar!", | |
shoo: function() {} | |
}); | |
console.log(JDI.myModule.bar); | |
JDI.myModule.shoo(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment