[AS3]AS3利用函数返回函数的用法示例
[AS3]AS3利用函数返回函数的用法示例
[AS3]AS3利用函数返回函数的用法示例
- 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("cuplayer.com提示:Welcome to Kingda.org! My blog");
- }
- public function objectFunc(kingdaObj:Object):void
- {
- trace("objectFunc():");
- for(var i:String in kingdaObj)
- {
- trace(i+":"+kingdaObj[i]);
- }
- }
- }
- }
热门文章推荐
- [HLS]做自己的m3u8点播系统使用HTTP Live Streaming(HLS技术)
- [FMS]FMS流媒体服务器配置与使用相关的介绍
- [AS3]什么是M3U8,与HTML5的区别是什么
- AS2.0 让flash自适应全屏,并且不自动缩放
- [AS3]as3.0的sound类常用技巧整理
- [AS3]as3与ByteArray详解、ByteArray介绍、ByteArray用法
- 关于RTMP,RTMPT,RTMPS,RTMPE,RTMPTE协议的介绍
- [JS]分享浏览器弹出窗口不被拦截JS示例
请稍候...