Imagine a JavaScript "class" Foo
:
var Foo = function()
{
};
And an instance of that class:
var foo = new Foo();
Can I obtain the string Foo
directly from the instance foo
, or is Foo
just a transitive variable that cannot be associated with the instance foo
after instantiation?
EDIT 1 SLaks suggests using foo.constructor
. This gives:
function ()
{
}
This approach works if the function is defined in the form:
function Foo()
{}
...but this might not always be the case.
EDIT 2 Trying skizeey's approach doesn't work either. It is essentially a more complete attempt as Slack's method, but yields an empty string: ""
EDIT 3 I wonder whether this is actually possible somehow. Notice the following transcript from Chrome's JavaScript console:
> var Foo = function() {}
undefined
> var foo = new Foo();
undefined
> foo
Foo
In the last line, Chrome clearly knows that the object is of type Foo
. However this may just be a Chrome thing, and not standard JavaScript or even inaccessible from the language itself.