ActionScript - 在switch语句中使用“is”比较?

时间:2010-09-24 16:05:14

标签: actionscript-3 switch-statement

我有许多相同自定义类的对象,以及另一个自定义类的另一个对象。我想创建一个switch语句来确定该对象属于哪个类。以下代码无法编译,所以我不确定这是否可行。是使用if语句的唯一选择吗?

function mouseClickEventHandler(evt:MouseEvent):void
     {
     switch (evt.currentTarget)
            {
            case (is customClassA):  trace("is instance of customClassA");  break
            case (is customClassB):  trace("is instance of customClassB");
            }
     }

2 个答案:

答案 0 :(得分:12)

这应该有效:

function mouseClickEventHandler ( evt:MouseEvent ):void
{
    switch ( evt.currentTarget.constructor )
    {
        case CustomClassA:
            trace("is instance of customClassA");
            break;

        case CustomClassB:
            trace("is instance of customClassB");
            break;
    }
}

请参阅Object.constructor

答案 1 :(得分:3)

function clickHandler (event:MouseEvent):void
{
    var target:Object = event.currentTarget;
    switch (true)
    {
        case (target is CustomClassA):
            trace("is instance of customClassA");
            break;

        case (target is CustomClassB):
            trace("is instance of customClassB");
            break;
    }
}

不确定是否需要大括号