Created
June 26, 2012 08:47
-
-
Save AMekss/2994473 to your computer and use it in GitHub Desktop.
Helper for global javascript namespace definition
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 MIT License (MIT) => http://www.opensource.org/licenses/mit-license | |
// Copyright (c) 2012 Artūrs Mekšs | |
// | |
// Runtime depndency jQuery 1.4 or above | |
(function($, rootspace){ | |
window[rootspace] = function(namespace, object){ | |
var setNamespace = function(namespace){ | |
var root = window[rootspace]; | |
$(namespace.split(".")).each(function(idx, name){ | |
root = root[name] = root[name] || {}; | |
}); | |
return root; | |
}; | |
return $.extend(true, setNamespace(namespace), object); | |
}; | |
})(jQuery, "APP"); |
Usage example:
APP("some.name.space", {
init: function() {
// function body
}
);
// this would create name-spaced function available under:
APP.some.name.space.init();
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
fcuk php - the link works without it as-well http://www.opensource.org/licenses/mit-license ;)