Function中的apply函数的应用

最近在研究PureMVC源码的过程中发现Function顶级包中的apply函数蛮有意思的,通过这个函数,可以及时的调用不同类中的同一个函数,这是该函数的格式:apply(thisArg:* , argArray:* ):*

指定要在 ActionScript 调用的任何函数内使用的  thisObject   的值。此方法还指定要传递给任何被调用函数的参数。由于  apply()   是 Function 类的方法,所以它也是 ActionScript 中每个 Function 对象的方法。

  Function.call()   (它将参数指定为用逗号分隔的列表)不同,该方法将参数指定为一个 Array 对象。如果在脚本实际执行前,无法知道要传递的参数的数量,那么这种方法通常很有用。

返回被调用函数指定为返回值的值。

下面例子:

首先定义好接口:

package com.interfaces
{
    public interface ITest
    {
        function testApply(showText:String):void;
    }
}

下面是分别定义的四个类:

T1:

package com.custom
{
    import com.interfaces.ITest;
   
    public class T1 implements ITest
    {
        public function T1()
        {
        }
       
        public function testApply(showText:String):void
        {
            trace("T1 : "+showText);
        }
    }
}

T2:

package com.custom
{
    import com.interfaces.ITest;
   
    public class T2 implements ITest
    {
        public function T2()
        {
        }
       
        public function testApply(showText:String):void
        {
            trace("T2 : "+showText);
        }
    }
}

T3:

package com.custom
{
    import com.interfaces.ITest;
   
    public class T3 implements ITest
    {
        public function T3()
        {
        }
       
        public function testApply(showText:String):void
        {
            trace("T3 : "+showText);
        }
    }
}

T4:

package com.custom
{
    import com.interfaces.ITest;
   
    public class T4 implements ITest
    {
        public function T4()
        {
        }
       
        public function testApply(showText:String):void
        {
            trace("T4 : "+showText);
        }
    }
}

在主类中测试:

package
{
    import com.custom.T1;
    import com.custom.T2;
    import com.custom.T3;
    import com.custom.T4;
    import com.interfaces.ITest;
   
    import flash.display.Sprite;
   
    public class TFunctionApply extends Sprite
    {
        private var testArr:Array = new Array();
        public function TFunctionApply()
        {
            this.showTest();
        }
       
        private function showTest():void
        {
            testArr.push(new T1());
            testArr.push(new T2());
            testArr.push(new T3());
            testArr.push(new T4());
           
            for(var i:int=0;i<this.testArr.length;i++)
            {
                this.getFun(i).apply(this.getClass(i),["success"]);
            }
        }
       
        public function getFun(index:int):Function
        {
            return (this.testArr[index] as ITest).testApply;               
        }
       
        public function getClass(index:int):ITest
        {
            return this.testArr[index];
        }
       
        private function testFA():void
        {
            t2.apply(this,["success"]);
        }
       
        private function t2(showText:String):void
        {
            trace("t2 : "+showText);
        }
    }
}

输出结果为:

T1 : success
T2 : success
T3 : success
T4 : success

是不是很方便,在对接口的编程中很好用。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值