Last active
December 15, 2015 07:09
-
-
Save andrewgunn/5221212 to your computer and use it in GitHub Desktop.
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 (root, undefined) { | |
'use strict'; | |
var toString = Object.prototype.toString; | |
if (toString.call(root.namespace) === '[object Function]') { | |
return; | |
} | |
root.namespace = function (ns, parent) { | |
var parts, | |
index, | |
length; | |
if (toString.call(ns) !== '[object String]') { | |
throw new Error('Invalid namespace: ' + ns); | |
} | |
parts = ns.split('.'); | |
parent = parent || root; | |
for (index = 0, length = parts.length; index < length; index += 1) { | |
if (typeof parent[parts[index]] === 'undefined') { | |
parent[parts[index]] = {}; | |
} | |
parent = parent[parts[index]]; | |
} | |
return parent; | |
}; | |
})(this); | |
// var modules = namespace('app.modules'); -> app.modules = {} | |
// var moduleA = namespace('app.modules.moduleA'); -> app.modules.moduleA = {} | |
// var moduleB = namespace('moduleB', modules); -> app.modules.moduleB = {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment