Marc
Marc

Reputation: 4477

How to retrieve the constructor's name in JavaScript?

Assume the following program:

var SomeConstructor = function() { };
var instance = new SomeConstructor ();

The expression instance instanceof SomeConstructor yields True, so instance has to know somehow that it was constructed by the function SomeConstructor . Is there way to retrieve the name SomeConstructor directly from instance ?

(In my problem at hand, I have a hierarchy of prototypes implementing possible signals in my application. I have to access the type of a signal which shall be equivalent to the constructor used to create that signal.)

Upvotes: 37

Views: 50228

Answers (6)

sje397
sje397

Reputation: 41862

On Chrome (7.0.544.0 dev), if I do:

function SomeConstructor() { }

var instance = new SomeConstructor();

console.log(instance.constructor.name);

it prints 'SomeConstructor'...but if SomeConstructor is defined as an unnamed function as you have it, it will print an empty string instead.

If I print instance.constructor it prints the same thing as it does if I print SomeConstructor in the code you have. The instanceof operator need only compare these two values to see that they are equal to be able to return true.

Upvotes: 45

Wilt
Wilt

Reputation: 44422

You can also declare your class this:

var C = function C() { };

Now your C class constructor is a named function (no longer an anonymous function) meaning you can do:

x = new C();    

console.log(x.constructor.name); // output: C

Upvotes: 2

Thomas Hagström
Thomas Hagström

Reputation: 4528

At least in React it might be

this.default.name

Upvotes: -6

phatmann
phatmann

Reputation: 18513

This code will get the name of the constructor, as long as it is not an anonymous function:

obj.constructor.toString().match(/function (\w*)/)[1];

Why would you need the class name? Let's say you want to save and restore class instances via JSON. You could store the class name in a "type" property, and then use a resolver function in JSON.parse to restore the objects. (See the sample code on this page).

So, in theory you could use the code above to make a generalized serializer that could handle any class instance, but parsing function strings is very inefficient. This overhead can be avoided by requiring all the classes you are going to store to provide the type explicitly:

function Foo() {}
Foo.prototype.type = 'Foo';

This seems silly and redundant, which is why I started on the quest to obtain the class name implicitly. But in the end I have to give in: there is no acceptable solution in JS :-(

Upvotes: 10

RichN
RichN

Reputation: 6241

This code of yours that needs to know the constructor, can it access the constructor function itself? If so, you can just do that instanceof thing.

It seems that you need to know it by name. This sounds dangerous. Someone can create another constructor that happen to have the same name and it will pass (unless that's what you want, of course).

Upvotes: 0

Andy E
Andy E

Reputation: 344803

No. You can use x.constructor to get a direct reference to C, but it's an anonymous function so there's no way of getting its name.

If it were defined like so:

function C() { };
x = new C();

Then it would be possible to use x.constructor.toString() and parse out the name of the function from the returned string. Some browsers would also support x.constructor.name[1].

[1] https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/name

Upvotes: 4

Related Questions