Find out the class of an object in prototypejs

I am using the prototypejs class API for an OOP class.

Is there a way to get the class name for an object?

EG:

var myDog = new Dog(); myDog.getClassName() //Should return "Dog" 
+4
source share
1 answer

If you use Prototypejs create() to create a class, you need to save an additional property to keep the class name, since the only reference to the class named Dog is the name of the variable to which you assign the result of create() :

 var Dog = Class.create({ className: "Dog", initialize: function() { } }); var myDog = new Dog(); console.log(myDog.className); // "Dog" 

On the other hand, if you define your class with something like this:

 function Dog() { } 

Then you can just use Object#constructor :

 myDog.constructor.name; // "Dog" 
+5
source

Source: https://habr.com/ru/post/1432591/


All Articles