More Related Content
More from Shanda innovation institute (16)
[Flash开发者交流][2010.05.30]actionscript中的元编程和开发流程解耦(谈熠)
- 8. 反射的应用 classApplicationState { publicstaticconstLOGIN:uint=1; publicstaticconstLOADING:uint=2; publicstaticconstPLAYING:uint=3; privatestaticconststateNames:Object= {}; }
- 9. 反射的应用 classApplicationState { ... publicstaticfunction init():void { for each(varconstantName:XMLin describeType(ApplicationState).constant.@name) { stateNames[ApplicationState[constantName.toString()]]= constantName.toString(); } } } ...
- 12. 概念1:干 干 —— 避免重复 DRY – Don’t Repeat Yourself 一种常见的重复操作: trace(“name:”+this.name+”, data:”+this.data);……
- 13. 反射在调试时的应用 class Inspector /** * 列出一个对象中的所有公开变量名,并把这些变量名放在一个 Vector.<String> * 容器中 * @paramobj object of any kind * @return 列出一个对象中的所有公开变量名,并把这些变量名放在一个 * Vector.<String> 容器中 */ publicstaticfunctionlistInstanceVariables(obj:*):Vector.<String> { returnxmlListToStringList(describeType(obj).variable.@name); } publicstaticfunctiondumpInstanceVariables(obj:*):Vector.<String> { returndumpProperty(listInstanceVariables(obj),obj); }
- 15. 方法缺失 (Method Missing) 工具:flash.utils.Proxy 作用:重载AS对象默认的操作行为 原理: Object extends flash.utils.Proxy Object.methodName(arg0,arg1) Object.callProperty(“methodName”,args)
- 16. 方法缺失 public dynamic class ClassA extends Proxy { flash_proxy override function callProperty(name:*, ...rest):* { trace(“请求调用方法:”+name) } } varinstanceA:ClassA = new ClassA(); instanceA.doSomeThing(); // 请求调用方法:doSomeThing
- 24. 采用代理模式(Proxy Pattern) Server send() ServerProxy@connectionObject send() RealConnectionsend() RealConnection可能是 xml, json, bin socket, restful http … function send(){ connectionObject.send() }