自ActionScript 3开始,eval函数就被取消了,这样就不能像原来那样利用字符串动态的创建对象了,但利用函数flash.utils.getDefinitionByName仍可以根据类型名称动态地创建类对象实例,下面看一个例子:
<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" minWidth="955" minHeight="600" applicationComplete="application1_applicationCompleteHandler(event)"> <mx:Script> <!--[CDATA[ import flash.utils.getDefinitionByName; import mx.events.FlexEvent; import test.DynamicObject; protected function application1_applicationCompleteHandler(event:FlexEvent):void { var typeRef:Class = getDefinitionByName("test.DynamicObject") as Class; new typeRef(); // new DynamicObject(); } ]]--> </mx:Script> </mx:Application>package test { public dynamic class DynamicObject { public function DynamicObject() { trace("DynamicObject is created!"); } } }
输出结果:
DynamicObject is created! DynamicObject is created!
其实这样的输出结果,没什么意外的。接下来将“new DynamicObject();”注释掉继续执行,以下是输出解决:
ReferenceError: Error #1065: Variable DynamicObject is not defined. at global/flash.utils::getDefinitionByName() at TestMain/application1_applicationCompleteHandler()[C:/Documents and Settings/Administrator/My Documents/Workspace/eclipse/ccgc/src/TestMain.mxml:14] at TestMain/___TestMain_Application1_applicationComplete()[C:/Documents and Settings/Administrator/My Documents/Workspace/eclipse/ccgc/src/TestMain.mxml:2] at flash.events::EventDispatcher/dispatchEventFunction() at flash.events::EventDispatcher/dispatchEvent() at mx.core::UIComponent/dispatchEvent() at mx.managers::SystemManager/preloader_preloaderDoneHandler()[C:/autobuild/3.5.0/frameworks/projects/framework/src/mx/managers/SystemManager.as:3140] at flash.events::EventDispatcher/dispatchEventFunction() at flash.events::EventDispatcher/dispatchEvent() at mx.preloaders::Preloader/displayClassCompleteHandler()[C:/autobuild/3.5.0/frameworks/projects/framework/src/mx/preloaders/Preloader.as:435] at flash.events::EventDispatcher/dispatchEventFunction() at flash.events::EventDispatcher/dispatchEvent() at mx.preloaders::DownloadProgressBar/timerHandler()[C:/autobuild/3.5.0/frameworks/projects/framework/src/mx/preloaders/DownloadProgressBar.as:1451] at mx.preloaders::DownloadProgressBar/initCompleteHandler()[C:/autobuild/3.5.0/frameworks/projects/framework/src/mx/preloaders/DownloadProgressBar.as:1503] at flash.events::EventDispatcher/dispatchEventFunction() at flash.events::EventDispatcher/dispatchEvent() at mx.preloaders::Preloader/dispatchAppEndEvent()[C:/autobuild/3.5.0/frameworks/projects/framework/src/mx/preloaders/Preloader.as:291] at mx.preloaders::Preloader/appCreationCompleteHandler()[C:/autobuild/3.5.0/frameworks/projects/framework/src/mx/preloaders/Preloader.as:443] at flash.events::EventDispatcher/dispatchEventFunction() at flash.events::EventDispatcher/dispatchEvent() at mx.core::UIComponent/dispatchEvent() at mx.core::UIComponent/set initialized() at mx.managers::LayoutManager/doPhasedInstantiation() at Function/http://adobe.com/AS3/2006/builtin::apply() at mx.core::UIComponent/callLaterDispatcher2() at mx.core::UIComponent/callLaterDispatcher()
之所以会出错,是因为在Flex编译程序时会自行删除一些未使用的,这时在动态创建对象时就会因缺失对象的类型而创建失败。之前的代码之所以执行成功,是因为类型DynamicObject在代码中显示地被使用,所以类型信息DynamicObject在编译时不会被删除。
其实还有一种方法来保证类型信息的存在,就是利用编译参数,也可以保证程序正常执行:
-includes=info.knightrcom.puppet.PushdownWinGamePinocchio