Getting the class name through an instance of this class

I have a function that takes 2 instances of a (custom) class as parameters. But each of them can be one of several classes, and I then need to call another function based on what type they are. I would like to do something like this:

function any_any(inst1, inst2) {
    this[inst1.classname + "_" + inst2.classname] (inst1, inst2);
}
function Circle_Line(circle:Circle, line:Line) {
    //treat this case
}

Should I go and define a "classname" in each of my classes, or is there a better way to get the instance class name? I don't know how to get typeof () to return anything other than an "object" for a custom class, maybe this is possible?

EDIT: It would be inconvenient to use the instanceof operator, since each class can be 1 out of 6 (currently).

+3
source share
3 answers

you can use instanceof

   var a:Number;

   if (a instanceof Number)
   {
       trace("a is a number");
   }
0
source

Another way to get an instance class is to use

var c:Class = instance["constructor"];

then you can do something like this:

switch(c)
{
    case Circle:
        whatever();
}
-1
source

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


All Articles