Last active
December 15, 2015 13:09
-
-
Save craigmaslowski/5265794 to your computer and use it in GitHub Desktop.
Memoized module function with support for 'dot.notated.namespacing'
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
var m1 = module('modules.galore.yo'); | |
m1.foo = 'bar'; | |
var m2 = module('modules.galore') | |
console.log(m2); // { yo: { foo: 'bar' } } | |
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
var module = function () { | |
var modules = {}; | |
return function (namespace) { | |
var nodes = namespace.split('.'), | |
lastNode; | |
_.each(nodes, function (node) { | |
// handle first iteration | |
if (!lastNode) { | |
modules[node] = modules[node] || {}; | |
lastNode = modules[node]; | |
} else { | |
lastNode[node] = lastNode[node] || {}; | |
lastNode = lastNode[node]; | |
} | |
}) | |
return lastNode; | |
} | |
}(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Missing semicolons on lines 17 & 19. Don't give me none of that ASI jabba!