views:

459

answers:

2

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
}
+4  A: 

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.

Justin Niessner
Are you sure there's no way to pass an array of arguments which gets translated to the inbuilt arguments by AS3? Or is there a cleaner way of writing the Task function so I don't have to use `arg[0]` and `arg[1]` ?
Jenko
This method seems most correct. If you need to pass an unknown number of arguments you can use the apply method on the function as well._obj.methodName.apply(_obj, [arg1, arg2, arg3]);
Chris Gutierrez
Here is the documentation on the apply method.http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/Function.html#apply%28%29
Chris Gutierrez
+2  A: 

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);
          }
    }
}
OXMO456