Skip to content

Instantly share code, notes, and snippets.

@ofca-snippets
Created December 23, 2012 20:58
Show Gist options
  • Save ofca-snippets/4366073 to your computer and use it in GitHub Desktop.
Save ofca-snippets/4366073 to your computer and use it in GitHub Desktop.
Creates namespace.
(function(root) {
/**
* Sets specified namespace on object.
*
* @example
* <code>
* var bar = namespace('foo.bar', window); // `bar` is link to window.foo.bar
* </code>
*
* @param {String} namespace
* @param {Object} object
* @return {Object}
*/
root.ns = function(namespace, object) {
var root = object,
ns = namespace.split('.'),
len = ns.length,
part, i = 0;
for (; i < len; i++) {
part = ns[i];
if ( ! root[part]) {
root[part] = {};
}
root = root[part];
}
return root;
};
})(window);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment