tags:

views:

29

answers:

2

I have a variable containing a string "Y.Plugin.abcd" and I would like to access an object with the same name...I'm using YUI 3 and attempted the Y.namespace method with no luck.

var nm = "Y.Plugin.abcd";

Y.log(Y.Plugin.abcd); //outputs the correct object

Y.log(Y.namespace(nm)); //outputs an empty object

I'm pretty much looking for any method, YUI or not, to be able to do what is a rather simple task in PHP.

+1  A: 

Check this out

Aaron Hathaway
Thanks this started getting me on the right track but it was Daniels answer that really drove the idea home.
+2  A: 

In plain JavaScript, you could probably split your string and then use the subscript notation, as follows:

var nm = "Y.Plugin.abcd";
var nm_array = nm.split('.');    // split the nm string
var obj = window;                // set our reference to the global object

for (var i = 0; i < nm_array.length; i++) {
   obj = obj[nm_array[i]];       // walk through all the namespaces
}

Y.log(obj);                      // obj will hold a reference to Y.Plugin.abcd
Daniel Vassallo
Thanks, what I ended up doing was Y.log(Y.Plugin[nm]); It ended up getting me the reference that I needed.