Passing unlimited arguments to a function

I recently stumbled across Flash Extensions which specializes in Flash Training & Solutions. There I found a few video tutorials and thats where this comes from 😉

Here’s how you can pass unlimited arguments to a function. 🙂 Flex Developers from the C++ and Java worlds might be familiar with this. Yet, this could be new stuff for some. The most common method used to pass multiple arguments to a function where the number of arguments are not predefined is to pass the arguments as a complex object like Array or something.


private function normalUsage(list:Array):void{
Alert.show(list.join(":"));
}

The other better way to handle this is something like this. Use “…args” in the function and then you can pass any number of arguments. –

private function dataToPop():void{
newUsage(1,2,3,5,6,7,8,9,10);
}


private function newUsage(...args):void{
Alert.show(args.join(":"));
}

Try it out. This could be useful at many places. 😉

Advertisements

3 Responses to Passing unlimited arguments to a function

  1. thanks for the info im is very use full to my study

  2. Thanks in advance. I am using the ..args to send multiple arguments to a netConnection class (AMFPHP usage). My problem is how I can pass those args to another function like:

    public function joupConnection(method:String, result:Function, status:Function, …args)
    {
    super.connect(‘http://….’);
    res = new Responder(result, status);
    super.call(‘joup.’ + method, res, args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8], args[9], args[10]);
    }

    Like this is working but this is not good especially if you have more than 10 args.

    Any ideas?

  3. Kenny says:

    Mikhail,

    You are looking for the apply() method.

    ex:

    public function ProxyDelegate(…args)
    {

    var _service:NetConnection = new NetConnection();
    var _responder:Responder = new Responder(onResult, onFault);

    _service.connect(“http://localhost/amfphp/gateway.php”);

    args.unshift(“YourPhpClass.yourPhpMethod”, _responder);

    _service.call.apply(_service, args);

    }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: