getting the name of a class through an instance of that class
I have a function that takes as parameters 2 instances of a (custom) class. But they can each be one of several classes, and I ne开发者_开发问答ed to then call another function based on what type they are. I'd 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 'classname' in each of my classes, or is there a better way to get the class name of an instance? I don't know how to get typeof() to return anything other than 'object' for a custom class, maybe it's possible?
EDIT: It would be inconvenient to use the instanceof operator, as each class can be 1 of 6 (currently).You can use instanceof, or the 'is' operator, or the getQualifiedClassName method
You can use instanceof
var a:Number;
if (a instanceof Number)
{
trace("a is a number");
}
another way to get the class of an instance is using
var c:Class = instance["constructor"];
then you can do something like this:
switch(c)
{
case Circle:
whatever();
}
精彩评论