AS函数的一些特殊应用

本文介绍了使用ActionScript进行动态编程的六种实用技巧,包括根据数字动态调用函数并为对象赋属性、动态改变函数调用顺序、利用函数返回函数、函数动态添加属性以追踪调用次数、为函数对象动态添加实例方法以及动态改变实例内部方法实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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(): 
        { 
            (); 
        } 
    } 
}

 

转载于:https://www.cnblogs.com/CoderWayne/archive/2010/07/15/1778023.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值