ActionScript - Using "is" comparative in switch statement?
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
精彩评论