Skip to content

Instantly share code, notes, and snippets.

@craigmaslowski
Last active December 15, 2015 13:09
Show Gist options
  • Save craigmaslowski/5265794 to your computer and use it in GitHub Desktop.
Save craigmaslowski/5265794 to your computer and use it in GitHub Desktop.
Memoized module function with support for 'dot.notated.namespacing'
var m1 = module('modules.galore.yo');
m1.foo = 'bar';
var m2 = module('modules.galore')
console.log(m2); // { yo: { foo: 'bar' } }
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;
}
}();
@adamyanalunas
Copy link

Missing semicolons on lines 17 & 19. Don't give me none of that ASI jabba!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment