1

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.)

8
  • Can you post a simplified example of what it looks like? Commented Apr 9, 2013 at 15:40
  • what is mynamespace? Commented Apr 9, 2013 at 15:41
  • If you only have a reference to the object 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. Commented Apr 9, 2013 at 15:41
  • @Blender: Of what looks like... the namespace itself? Each part is just a javascript module, with tons of functions, variables, "private" stuff, closures, etc. Commented Apr 9, 2013 at 15:41
  • @Jamiec: var mynamespace = Foo.Bar.Baz; Commented Apr 9, 2013 at 15:42

1 Answer 1

2

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).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.