Flex动态创建类对象

自ActionScript 3开始,eval函数就被取消了,这样就不能像原来那样利用字符串动态的创建对象了,但利用函数flash.utils.getDefinitionByName仍可以根据类型名称动态地创建类对象实例,下面看一个例子:

输出结果: 

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在编译时不会被删除。

其实还有一种方法来保证类型信息的存在,就是利用编译参数,也可以保证程序正常执行:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值