开发者

ActionScript - Using "is" comparative in switch statement?

开发者 https://www.devze.com 2023-01-17 06:21 出处:网络
i have many objects of the same custom class, and another many objects of another custom class.i would like to create a switch statement to determine from which of the classes the object belongs.the f

i have many objects of the same custom class, and another many objects of another custom class. i would like to create a switch statement to determine from which of the classes the object belongs. the following code doesn't compile, so i'm not sure if this is possible. is the only alternative to use if statements?

function mouseClickEventHandler(evt:MouseEvent):void
     {
     switch (evt.currentTarget)
            {
            case (is customCla开发者_StackOverflow中文版ssA):  trace("is instance of customClassA");  break
            case (is customClassB):  trace("is instance of customClassB");
            }
     }


This should work:

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;
    }
}

See Object.constructor.


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;
    }
}

Not sure if braces are needed

0

精彩评论

暂无评论...
验证码 换一张
取 消