publicfunction TypeTest() { var uiComponent:UIComponent = new UIComponent(); var canvas:Canvas = new Canvas(); trace("uiComponent is UIComponent "+(uiComponent isUIComponent)); trace("uiComponent is Canvas "+(uiComponent isCanvas)); trace("canvas is UIComponent " +(canvas is UIComponent)); }
输出一下内容: uiComponent is UIComponent true uiComponent is Canvas false canvas is UIComponent true 类型检测最常用到的地方是当组件抛出一个事件时。在事件处理函数中检测是什么对象发出动作。
+展开
-ActionScript
privatefunction eventListener(mouseEvent:MouseEvent):void { if (mouseEvent.target is Button) { /* handle button specific actions */ } elseif (mouseEvent.target is ComboBox) { /* handle combobox specific things */ } else { /* handle all other cases */ } }