One argument that's been lodged by many is that MooTools doesn't
encourage the use of namespaces for classes. The Dojo Toolkit does
employ namespaces and provides two helpful functions for working with
them: dojo.getObject and dojo.setObject. These methods allow for
setting and getting of nested objects via strings. I've ported this
functionality MooTools so you can create and retrieve nested objects
quickly!
The MooTools JavaScript
Here goes the magic, slightly modified from Dojo:
(function() {
var objectifier = function(splits, create, context) {
var result = context || window;
for(var i = 0, s; result && (s = splits[i]); i++) {
result = (s in result ? result[s] : (create ? result[s] = {} : undefined));
}
return result;
};
Object.extend("place", function(name, value, context) {
var splits = name.split("."), s = splits.pop(), result = objectifier(splits, true, context);
return result && s ? (result[s] = value) : undefined;
});
Object.extend("retrieve", function(name, create, context) {
return objectifier(name.split("."), create, context);
});
Object.extend("exists", function(name, context) {
return Object.retrieve(name, false, context) !== undefined;
});
})();
Both methods use a root method for parsing the string and finding the
object, if it exists. The place method creates the object, accepting
the object name, value, and context (base object), creating the object
as desired:
Object.place("my.namespace.MyClass", {
name: "David"
});Object.place("my.namespace.MyClass", {
name: "David"
}, some.existing.objecto);
Likewise, the retrieve method searches for the object, and optionally may create it:
Object.retrieve("my.namespace.MyClassToo");
Object.retrieve("my.namespace.MyClassThree", true);
As a bonus, I've also thrown in an exists method to check for the existence of an object:
Object.exists("my.namespace.MyClassToo");
Woohoo! These are some really nice utility methods to have around if
you're looking to work with dynamic objects and namespaces!