ابدأ بالتواصل مع الأشخاص وتبادل معارفك المهنية

أنشئ حسابًا أو سجّل الدخول للانضمام إلى مجتمعك المهني.

متابعة

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

user-image
تم إضافة السؤال من قبل Zaid Rabab'a , Software Development Team Leader , Al-Safa Co. Ltd.
تاريخ النشر: 2014/02/02
Mahmoud Elmahdi
من قبل Mahmoud Elmahdi , UX/UI Designer, Web Developer , ikantam.com

Here's couple of answers Answer1 , Answer2 .

Hope that can help.

Hatim Laxmidhar
من قبل 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
من قبل Noor Khan , Technical Team Lead , Ammrys Studios

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

المزيد من الأسئلة المماثلة