AS函数的一些特殊应用
1. 根据数字决定调用函数个数,并动态给对象赋属性
package { import flash.display.Sprite; public class Sample9261 extends Sprite { public function Sample9261() { var arr:Array = new Array(); arr.push(aFunc); arr.push(bFunc); arr.push(cFunc); var obj:Object = new Object(); for(var i:uint=0;i<arr.length;i++) { arr[i](obj); } for(var j:String in obj) { trace(j+":"+obj[j]); } for each(var k:String in obj) { trace(k); } } private function aFunc(obj:Object):void { obj.attribute1 = true; trace("执行了aFunc方法"); } private function bFunc(obj:Object):void { obj.attribute2 = true; trace("执行了bFunc方法"); } private function cFunc(obj:Object):void { obj.attribute3 = true; trace("执行了cFunc方法"); } } }
2. 动态改变函数调用顺序
package { import flash.display.Sprite; public class Sample9261 extends Sprite { public function Sample9261() { var arrIndex:Array = [2,0,1]; var arrFunc:Array = new Array(); arrFunc.push(aFunc); arrFunc.push(bFunc); arrFunc.push(cFunc); for(var i:uint=0;i<arrIndex.length;i++) { arrFunc[arrIndex[i]](); } } public function aFunc():void { trace("this is a function"); } public function bFunc():void { trace("this is b function"); } public function cFunc():void { trace("this is c function"); } } }
3. 利用函数返回函数
package { import flash.display.Sprite; public class Sample9261 extends Sprite { public function Sample9261() { //通过A调用只有一个参数的aFunc(): chooseFuncBy("A")("A func has only one parameter."); //输出:aFunc():A func has only one parameter. //通过B调用有两个参数的bFunc(): chooseFuncBy("B")("B func has two parameters.","No.2 parameter"); //输出:bFunc():B func has two parameters. one more Parameter:No.2 parameter //字符串不符,默认函数 chooseFuncBy("wu lala")("I choose A function"); //输出:Welcome to kingda.org! My blog var withObj:Object = new Object(); var myObj:Object = {name:"黑羽",blog:"http://www.kingda.org",hobby:"WowCraft"}; chooseFuncBy(withObj)(myObj); /*输出: objectFunc(): name:黑羽 blog:http://www.kingda.org hobby:WowCraft */ } public function chooseFuncBy(input:*):Function { //运用一:利用参数的种类来确定返回的函数 if(!(input is String)) { return objectFunc; } //运用二:根据参数内容来返回函数 switch(input) { case "A": return aFunc; case "B": return bFunc; default: return kingdaFunc; } //更多延伸运用:利用参数个数、 is 确定不同Class的实例来选择函数,等等 } public function aFunc(str:String):void { trace("aFunc():"+str); } public function bFunc(str1:String,str2:String):void { trace("bFunc():"+str1+" one more Parameter:"+str2); } public function kingdaFunc(...rest):void { trace("Welcome to Kingda.org! My blog"); } public function objectFunc(kingdaObj:Object):void { trace("objectFunc():"); for(var i:String in kingdaObj) { trace(i+":"+kingdaObj[i]); } } } }
4. 函数动态添加属性(可以得到该函数被调用次数)
package { import flash.display.Sprite; public class Sample9261 extends Sprite { public function Sample9261() { var shot:Function = function():void { shot["times"]++; trace("Shot():time:"+shot["times"]); } shot["times"] = 0; //初始化times shot(); //输出:Shot():times:1 shot(); //输出:Shot():times:2 shot(); //输出:Shot():times:3 } } }
5. 函数对象动态添加实例方法
package { import flash.display.Sprite; public class Sample9261 extends Sprite { public function Sample9261() { var shot:Function = function():void { shot["times"]++; trace("Shot():times:"+shot["times"]); shot["reload"](); //其它代码 } shot["times"] = 0;//初始化times shot["reload"] = function():void { trace("reload:"+this["times"]); if(this["times"]>3) { this["times"] = 0; } } shot["reload"](); shot(); shot(); shot(); shot(); shot(); shot(); } } }
6. 动态改变实例里面的方法实现
MyClass.as
package Model { public class MyClass { public function MyClass() { } public var AlertFunc:Function = function():void { trace("what time is it now?"); } } }
Sample9271.as
package { import flash.display.Sprite; import Model.MyClass; public class Sample9271 extends Sprite { public function Sample9271() { var model:MyClass = new MyClass(); model.AlertFunc(); model.AlertFunc = AMFunc; model.AlertFunc(); model.AlertFunc = PMFunc; model.AlertFunc(); } internal function AMFunc():void { trace("Good Morning"); } internal function PMFunc():void { trace("Good Afternoon"); } } }