Take multiple arguments in AS3 method

How to accept multiple arguments in a custom method? How:

Proxy(101, 2.02, "303");

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

function Task(var1:int, var2:Number, var3:String){ 
    // work with vars
}
+3
source share
2 answers

You cannot just pass an args array, as in your question. You will have to pass each element of the args array separately.

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 it:

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

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

Just be careful. The performance of apply () is much slower than calling a function with the traditional method.

+8
source

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

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);
          }
    }
}
+4
source

Source: https://habr.com/ru/post/1725845/


All Articles