Is there a JavaScript equivalent of Java‘s class.getName()?
Is there a JavaScript equivalent of Java ‘s class.getName() ?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
No.
ES2015 Update: the name of
class Foo {}isFoo.name. The name ofthing‘s class, regardless ofthing‘s type, isthing.constructor.name. Builtin constructors in an ES2015 environment have the correctnameproperty; for instance(2).constructor.nameis"Number".But here are various hacks that all fall down in one way or another:
Here is a hack that will do what you need – be aware that it modifies the Object’s prototype, something people frown upon (usually for good reason)
Now, all of your objects will have the function,
getName(), that will return the name of the constructor as a string. I have tested this inFF3andIE7, I can’t speak for other implementations.If you don’t want to do that, here is a discussion on the various ways of determining types in JavaScript…
I recently updated this to be a bit more exhaustive, though it is hardly that. Corrections welcome…
Using the
constructorproperty…Every
objecthas a value for itsconstructorproperty, but depending on how thatobjectwas constructed as well as what you want to do with that value, it may or may not be useful.Generally speaking, you can use the
constructorproperty to test the type of the object like so:So, that works well enough for most needs. That said…
Caveats
Will not work AT ALL in many cases
This pattern, though broken, is quite common:
Objectsconstructed vianew Thingywill have aconstructorproperty that points toObject, notThingy. So we fall right at the outset; you simply cannot trustconstructorin a codebase that you don’t control.Multiple Inheritance
An example where it isn’t as obvious is using multiple inheritance:
Things now don’t work as you might expect them to:
So, you might get unexpected results if the
objectyour testing has a differentobjectset as itsprototype. There are ways around this outside the scope of this discussion.There are other uses for the
constructorproperty, some of them interesting, others not so much; for now we will not delve into those uses since it isn’t relevant to this discussion.Will not work cross-frame and cross-window
Using
.constructorfor type checking will break when you want to check the type of objects coming from differentwindowobjects, say that of an iframe or a popup window. This is because there’s a different version of each core typeconstructorin each `window’, i.e.Using the
instanceofoperator…The
instanceofoperator is a clean way of testingobjecttype as well, but has its own potential issues, just like theconstructorproperty.But
instanceoffails to work for literal values (because literals are notObjects)The literals need to be wrapped in an
Objectin order forinstanceofto work, for exampleThe
.constructorcheck works fine for literals because the.method invocation implicitly wraps the literals in their respective object typeWhy two dots for the 3? Because Javascript interprets the first dot as a decimal point 😉
Will not work cross-frame and cross-window
instanceofalso will not work across different windows, for the same reason as theconstructorproperty check.Using the
nameproperty of theconstructorproperty…Does not work AT ALL in many cases
Again, see above; it’s quite common for
constructorto be utterly and completely wrong and useless.Does NOT work in <IE9
Using
myObjectInstance.constructor.namewill give you a string containing the name of theconstructorfunction used, but is subject to the caveats about theconstructorproperty that were mentioned earlier.For IE9 and above, you can monkey-patch in support:
Updated version from the article in question. This was added 3 months after the article was published, this is the recommended version to use by the article’s author Matthew Scharley. This change was inspired by comments pointing out potential pitfalls in the previous code.
Using Object.prototype.toString
It turns out, as this post details, you can use
Object.prototype.toString– the low level and generic implementation oftoString– to get the type for all built-in typesOne could write a short helper function such as
to remove the cruft and get at just the type name
However, it will return
Objectfor all user-defined types.Caveats for all…
All of these are subject to one potential problem, and that is the question of how the object in question was constructed. Here are various ways of building objects and the values that the different methods of type checking will return:
While not all permutations are present in this set of examples, hopefully there are enough to provide you with an idea about how messy things might get depending on your needs. Don’t assume anything, if you don’t understand exactly what you are after, you may end up with code breaking where you don’t expect it to because of a lack of grokking the subtleties.
NOTE:
Discussion of the
typeofoperator may appear to be a glaring omission, but it really isn’t useful in helping to identify whether anobjectis a given type, since it is very simplistic. Understanding wheretypeofis useful is important, but I don’t currently feel that it is terribly relevant to this discussion. My mind is open to change though. 🙂