Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

How to get class object's name as a string in Javascript?

user-image
Question added by Zaid Rabab'a , Software Development Team Leader , Al-Safa Co. Ltd.
Date Posted: 2014/02/02
Mahmoud Elmahdi
by Mahmoud Elmahdi , UX/UI Designer, Web Developer , ikantam.com

Here's couple of answers Answer1 , Answer2 .

Hope that can help.

mohammed Ali Ali
by mohammed Ali Ali , Software Engineer , Elaw

PLease have a look into this link .hope you can find the answer 

http://stackoverflow.com/questions/1249531/how-to-get-a-javascript-objects-class

Hatim Laxmidhar
by Hatim Laxmidhar , Self Employed , The Way It Works

I assume that you are looking to get class name from an object variable.

e.g.

function Foo() {... ... ... ... ... ... ... }

var myFoo = new Foo();

 

Now you have myFoo and what you want is "Foo".

The simple thing that will work here is:

var strClassname = myFoo.constructor.name;

//here strClassname will hold value "Foo" as string

 

There is a very good disussion done for this@

http://stackoverflow.com/questions/1249531/how-to-get-a-javascript-objects-class

 

 

Noor Khan
by Noor Khan , Technical Team Leader , Insightz360

In JavaScript, every object has a constructor property, which contains a Function object, which has a name property, which contains a string

 

obj.constructor.name

More Questions Like This

Do you need help in adding the right keywords to your CV? Let our CV writing experts help you.