title: Reflection in JavaScript
body: <p>I have been looking into the possibility of reflection in JavaScript. I already have a simple reflector which can list the members of an object/function, like so:</p>
  
  <pre><code>window["Foo"] = {
      "Bar": {
          "Test": function () {
              this.x = 32;
              this._hello = "Hello World";
              this._item = 123.345;
  
              this.hello = function() {
                  alert("hello");
              };
  
              this.goodbye = function() {
                  alert("goodbye");
              }
          }
      }
  }
  
  $(document).ready(function () {
      var x = new Foo.Bar.Test();
      Reflect(new Foo.Bar.Test());
  });
  
  function Reflect(obj) {
      for (var item in obj) {
          $("body").append(item + "(" + typeof obj[item] + ") = " + obj[item] + "&lt;br /&gt;");
      }
  }
  </code></pre>
  
  <p><strong>Results:</strong></p>
  
  <blockquote>
    <p>x(number) = 32</p>
    
    <p>_hello(string) = Hello World</p>
    
    <p>_item(number) = 123.345</p>
    
    <p>hello(function) = function () { alert("hello"); }</p>
    
    <p>goodbye(function) = function () { alert("goodbye"); }</p>
  </blockquote>
  
  <p>The next part of my challenge is to build something which can reflect back (if possible) an objects name, and the path to the object.</p>
  
  <p>using this example:...</p>
  
  <pre><code>var x = new Foo.Bar.Test();
  </code></pre>
  
  <p>How can I reflect back "Test", from x? For example:</p>
  
  <pre><code>ReflectName(x); //returns "Test";
  </code></pre>
  
  <p>Also how can I reflect back the path to x? For example:</p>
  
  <pre><code>ReflectPath(x) //returns Foo.Bar.Test
  </code></pre>
  
  <p>Is is possible to do these two things using JavaScript? I have researched this, and so far have not managed to come up with any viable solutions.</p>
  
  <p>I do not want a solution that requires hard coding the name and path of the object/function, as this would defeat the point of using reflection.</p>