1. 根据数字决定调用函数个数,并动态给对象赋属性
{ flash.display.Sprite; Sample9261Sprite { Sample9261() { arr:Array = Array(); arr.push(aFunc); arr.push(bFunc); arr.push(cFunc); obj:Object = Object(); (i:uint=0;i<arr.length;i++) { arr[i](obj); } (j:String obj) { (j++obj[j]); } (k:String obj) { (k); } } aFunc(obj:Object): { obj.attribute1 = ; (); } bFunc(obj:Object): { obj.attribute2 = ; (); } cFunc(obj:Object): { obj.attribute3 = ; (); } } }
2. 动态改变函数调用顺序
{ flash.display.Sprite; Sample9261Sprite { Sample9261() { arrIndex:Array = [2,0,1]; arrFunc:Array = Array(); arrFunc.push(aFunc); arrFunc.push(bFunc); arrFunc.push(cFunc); (i:uint=0;i<arrIndex.length;i++) { arrFunc[arrIndex[i]](); } } aFunc(): { (); } bFunc(): { (); } cFunc(): { (); } } }
3. 利用函数返回函数
{ flash.display.Sprite; Sample9261Sprite { Sample9261() { chooseFuncBy()(); chooseFuncBy()(,); chooseFuncBy()(); withObj:Object = Object(); myObj:Object = {name:,blog:,hobby:}; chooseFuncBy(withObj)(myObj); } chooseFuncBy(input:*):Function { (!(input String)) { objectFunc; } (input) { : aFunc; : bFunc; : kingdaFunc; } } aFunc(str:String): { (+str); } bFunc(str1:String,str2:String): { (+str1++str2); } kingdaFunc(...rest): { (); } objectFunc(kingdaObj:Object): { (); (i:String kingdaObj) { (i++kingdaObj[i]); } } } }
4. 函数动态添加属性(可以得到该函数被调用次数)
{ flash.display.Sprite; Sample9261Sprite { Sample9261() { shot:Function = (): { shot[]++; (+shot[]); } shot[] = 0; shot(); shot(); shot(); } } }
5. 函数对象动态添加实例方法
{ flash.display.Sprite; Sample9261Sprite { Sample9261() { shot:Function = (): { shot[]++; (+shot[]); shot[](); } shot[] = 0; shot[] = (): { (+this[]); (this[]>3) { this[] = 0; } } shot[](); shot(); shot(); shot(); shot(); shot(); shot(); } } }
6. 动态改变实例里面的方法实现
MyClass.as
Model { MyClass { MyClass() { } AlertFunc:Function = (): { (); } } }
Sample9271.as
{ flash.display.Sprite; Model.MyClass; Sample9271Sprite { Sample9271() { model:MyClass = MyClass(); model.AlertFunc(); model.AlertFunc = AMFunc; model.AlertFunc(); model.AlertFunc = PMFunc; model.AlertFunc(); } AMFunc(): { (); } PMFunc(): { (); } } }