Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reflection in JavaScript

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.

like image 846
Matthew Layton Avatar asked Dec 17 '12 08:12

Matthew Layton


Video Answer


2 Answers

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.

like image 96
skalee Avatar answered Oct 14 '22 11:10

skalee


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.

like image 38
thSoft Avatar answered Oct 14 '22 11:10

thSoft