-
-
Save saas786/2383717 to your computer and use it in GitHub Desktop.
JavaScript: namespacing (depends on jQuery)
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
(function (global, $) { | |
global.namespace = function (nsName, obj) { | |
var level, names, name; | |
level = global; | |
names = nsName.split('.'); | |
while(name = names.shift()) { | |
level[name] = level[name] || {}; | |
level = level[name]; | |
} | |
$.extend(level, obj); | |
} | |
})(this, jQuery); | |
// Usage: | |
namespace('one.two.three', { | |
four: (function () {}) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment