8

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:

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] + "<br />");
    }
}

Results:

x(number) = 32

_hello(string) = Hello World

_item(number) = 123.345

hello(function) = function () { alert("hello"); }

goodbye(function) = function () { alert("goodbye"); }

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.

using this example:...

var x = new Foo.Bar.Test();

How can I reflect back "Test", from x? For example:

ReflectName(x); //returns "Test";

Also how can I reflect back the path to x? For example:

ReflectPath(x) //returns Foo.Bar.Test

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.

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.

LostMyGlasses
  • 3,074
  • 20
  • 28
Matthew Layton
  • 39,871
  • 52
  • 185
  • 313

3 Answers3

13

There are no classes in JavaScript (although due to code style which for reasons unknown to me imitates Java you could think there are some). Foo.Bar.Test does not mean class Test registered in namespace Foo.Bar, but function which is assigned as attribute Test of some object which is assigned as attribute Bar of some object known as Foo.

You can't do reflection like "give me all variables to which number 7 is assigned", consequently you can't list all the objects which hold Test in one of their attributes.

This is actually good and opens new possibilities, but might be confusing in the beginning.

BTW Since there are no classes in JavaScript, I believe term reflection is not very fortunate. And new Foo() does not mean "create new instance of Foo", but "create a new object and execute function Foo in context of that object, and finally return it. Yeah, the new keyword is very confusing, if you want to do anything more advanced in JavaScript, never trust your Java/C# experience. JavaScript fakes Java (I suppose to not scare newcomers and allow them to do easy things quickly), but it's very different.

skalee
  • 12,331
  • 6
  • 55
  • 57
  • Thanks for this answer. I understand the points made. Like you said, JavaScript does not have classes or namespaces. I am actually looking at it from the TypeScript perspective, which has classes and modules. Understandably this has no real significance as it compiles to JavaScript anyway, and therefore can only comply with how JavaScript formulates Objects/Functions. I am beginning to think that the only way I will achieve what I am trying to achieve, will be to have a Type dictionary, where each type may have a unique identifier pointing to its type information in the dictionary. – Matthew Layton Dec 17 '12 at 11:10
  • @series0ne: I'm not familiar with TypeScript, but since it registers types, it may add reflection to JavaScript too. – skalee Dec 17 '12 at 12:01
  • unfortunately this is not the case. TypeScript is just a superset of JavaScript, and compiles to JavaScript, therefore what I am trying to achieve is no more achievable from TypeScript than it is from JavaScript. The only things TypeScript brings to the table are syntactic sugar, so for example, if I write a class in TypeScript, and then inherit that class elsewhere, "class Foo extends Bar" becomes the "prototypal inheritance" equivalent of the statement. – Matthew Layton Dec 17 '12 at 12:43
  • this answer works and gives the type name!: https://stackoverflow.com/a/47447046/1915920 – Andreas Covidiot May 13 '21 at 08:01
5

This is not possible in JavaScript. (To get a deeper understanding of JavaScript's type system, I recommend reading this.)

The best approximation you can do is querying over a static JSON structure.

klutt
  • 30,332
  • 17
  • 55
  • 95
thSoft
  • 21,755
  • 5
  • 88
  • 103
  • Nice idea! Inspecting `window` object properties which start with capital letter (there are not so many of them), then their properties etc. should give pretty nice results. The only thing to remember is that objects have prototypes and their properties should (?) be omitted. – skalee Dec 17 '12 at 12:09
  • 1
    The first link no longer exists. :( – mgthomas99 Feb 11 '19 at 16:38
2

Answer to your first question

function ReflectName(obj) { return obj.__proto__.constructor.name }

Second question is a bit more difficult and would require more setup with the definitions.

It is answered a bit better here: Javascript objects: get parent

simon
  • 854
  • 1
  • 9
  • 23