对于这样的写法可以防止编译器报错,但是运行时会因为找不到属性而报错:
var c:Sprite = new Sprite();
c['ddd'] = 0;
运行时访问了不存在的属性或者方法ddd,报错“ReferenceError: Error #1069: Property ddd not found on flash.display.Sprite and there is no default value.”
对于自定义的类也会这样,一般这种写法需要加个try catch。
使用Proxy也可以处理此类错误,前提是必须继承自Proxy类,并覆盖Proxy中的方法。比如取不存在的属性需要覆盖getProperty方法,如果访问到了不存在的属性则会执行getProperty。
flash.utils.Proxy的中英文文档:
http://shouce.jb51.net/actionscript3.0/flash/utils/Proxy.html
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/utils/Proxy.html
判断类的是否有方法或属性可用hasOwnProperty方法,但是无法判断私有变量、方法。
使用this['proxxx']的写法只能避免编译报错,不能避免运行时的报错,运行时需要捕获或者类似于Proxy的做法。