Possible Duplicate:
How to get a JavaScript Object's Class?
In Ruby I could do this to check the class of an instance:
'this is an string'.class
=>String
Is there any similar thing in js?
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.
You probably mean type or constructor, not class. Class has a different meaning in JavaScript.
To get the class:
See this related MDN article.
To get the constructor or prototype there are several ways, depending on what you need.
To discover what type of primitive you have, use
typeof. This is the best thing to use with strings, booleans, numbers, etc:Just beware that
nullacts weird in this case, astypeof nullwill give you"object", not"null".You can also get the prototype, the backbone JavaScript inheritance, with
Object.getPrototypeOf(myObject)(ormyObject.__proto__ormyObject.constructor.prototypein some browsers whengetPrototypeOfisn’t supported).You can test the constructor with
instanceof:You can also reasonably get the constructor with
myObject.constructor, although be aware that this can be changed. To get the name of the constructor, usemyObject.constructor.name.