[jQuery] javascript namespacer
I wrote a simple namespacer. Do you think this is useful in real world
applications? Any improvements that can be made to the code? Thank
you.
var namespace = function (name, global){
var root = global || '$';
(!eval('window.'+root))? eval('window.' + root + '={}') : '';
var name = name.split(".");
var fullname;
for (var i=0; i<name.length; i++) {
fullname = (!i) ? name[i] : fullname + "." + name[i];
if (!eval (root + "." + fullname))
eval (root + "." + fullname + " = {}");
}
}
namespace('hello.yahoo');
namespace('helloz.yahoo2');
namespace('helloz.yahoo2', 'YAHOO');
$.hello.yahoo.init = function() { alert(1); };
$.helloz.yahoo2.init = function() { alert(2); };
YAHOO.helloz.yahoo2.init = function() {alert(3); };
$.hello.yahoo.init(); //alerts 1
$.helloz.yahoo2.init(); // alerts 2
YAHOO.helloz.yahoo2.init(); // alerts 3