I have written a wrapper for my code that allows basic namespacing and I'd like some feedback on how it can be improved.
It is designed so that a module can define it's namespace and get access to all of the parent namespaces. These are spread in reverse order across the arguments of the module (i.e. first.second.third
is passed as (third, second, first)
).
Here is the code itself:
(function(name, globals, factory) {
var namespaces = [];
name.split('.').reduce(function(object, key) {
if (typeof(object[key]) !== 'object') {
object[key] = {};
}
namespaces.push(object[key]);
return object[key];
}, globals);
factory.apply(null, namespaces.reverse());
})(/* namespace */, this, function(util, app) {
});
It is designed for use in the browser, hence this
being passed as the globals
argument (this
is window
in the browser).
Here's an example of how I use it:
(function(name, globals, factory) {
var namespaces = [];
name.split('.').reduce(function(object, key) {
if (typeof(object[key]) !== 'object') {
object[key] = {};
}
namespaces.push(object[key]);
return object[key];
}, globals);
factory.apply(null, namespaces.reverse());
})('app.util', this, function(util, app) {
util.doSomething = function() {};
// I expose all "levels" of the namespace so that it can be used like this:
util.doSomethingElse = function() {
app.core.performAction();
};
});
One or two questions/thoughts I've had on it:
Is there a real reason I should use
this
instead ofwindow
? I'm not really sure why I started using it, I think I saw someone else using it and just picked it up.Is there any way to shorten it? I know it's only ~400 bytes but it would still be nice to simplify it further. I could use
map
instead ofreduce
but I tried it and it only saved on one line and partially sacrificed readability and simplicity.