Accept multiple arguments in an AS3 method

試著忘記壹切 提交于 2019-12-06 04:44:31

问题


How do I accept multiple arguments in a custom method? Like:

Proxy(101, 2.02, "303");

function Proxy(args:Arguments){
    Task(args);
}

function Task(var1:int, var2:Number, var3:String){ 
    // work with vars
}

回答1:


You wouldn't be able to just pass the args array through like you have in your question. You'd have to pass each element of the args array seperately.

function Proxy(... args)
{
   // Simple with no error checking.
   Task(args[0], args[1], args[2]);
}

Udate

After reading one of the comments, it looks like you can get away with:

function Proxy(... args)
{
    // Simple with no error checking.
    Task.apply(null, args);

    // Call could also be Task.apply(this, args);
}

Just be careful. Performance of apply() is significantly slower than calling the function with the traditional method.




回答2:


You can also use apply(thisArg:*, argArray:*):* method from the Function object.

example:

package{

    public class Test{
          public function Test(){
              var a:Array=[1,"a"];
              callMe.apply(this,a);
          }       
          public function callMe(a:Number,b:String):void{
                trace(a,b);
          }
    }
}


来源:https://stackoverflow.com/questions/1914648/accept-multiple-arguments-in-an-as3-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!