I use the revealing module pattern, and have a namespace Foo.Bar.Baz.
How do I convert that to a string "Foo.Bar.Baz"?
(If I do mynamespace.toString() it yields "[object Object]" rather than the desired string.)
Using a very simplified verion of the Revealing module pattern:
var revealed = function(){
var a = [1,2,3];
function abc(){
return (a[0]*a[1])+a[2];
}
return {
name: 'revealed',
abcfn: abc
}
}();
You could include an override of toString in the revealed object:
var revealed = function(){
var a = [1,2,3];
function abc(){
return (a[0]*a[1])+a[2];
}
return {
name: 'revealed',
abcfn: abc,
toString = function(){ return "revealed"; }
}
}();
This can then be called using revealed.toString() as you first attempted. If your revealed object contains sub objects (namespaces) then each individual toString implementation can call the children's toString and concatenate with a dot (or whatever is appropriate).
mynamespace?Foo.Bar.Baz, then there is no way to do it. If you have a reference to the root, you could iterate recursively over the namespace to find the object (and keep track of the property names), but this does not sound very efficient.var mynamespace = Foo.Bar.Baz;