;

PDA

Click to See Complete Forum and Search --> : How to pass an undefined number of arguments to a function?


phantobass
10-31-2007, 09:57 PM
I'm not sure if it's possible but if yes can someone help?

Thanks!!!

691175002
10-31-2007, 11:04 PM
http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/arguments.html

phantobass
10-31-2007, 11:22 PM
I've found something else:

function getArguments(arg=null)
{
//do something with arg
}

where arg can be an array of argument.

Is that cool? or not

illustratedlife
10-31-2007, 11:39 PM
Why would you do that?

Just do:

function (... args){

It will make an array called args from any arguments you pass it.

phantobass
10-31-2007, 11:59 PM
I would do that in case I don't want to pass any argument at all, in order to avoid the "you passed 0 arguments, was expecting 3" kind of compiling error, I guess!

illustratedlife
11-01-2007, 12:06 AM
I don't mean why would you want to pass x args, but why would you mess with making the array yourself. Try ... args. It's much cleaner.

phantobass
11-01-2007, 12:30 AM
oh I see, thanks a lot ! much better!

illustratedlife
11-01-2007, 12:34 AM
No worries.

My favorite place to use ... is for event listeners. Often times, you want a function to be triggered, but the function doesn't need to know anything about the event that triggered it. For example, if you have a function update, you may need to call it sometimes from other functions and other times with an event listener. Since update doesn't care who called it, function update(... args) will allow you to easily invoke it from sibling functions or use it as an event callback.

phantobass
11-01-2007, 12:39 AM
very nice tip! thanks! I think it actually answer a question I posted here in another thread!
Though if an event is passed to update, how do you grab the variables of the event?

Martin Munoz
11-01-2007, 01:27 AM
No worries.

My favorite place to use ... is for event listeners. Often times, you want a function to be triggered, but the function doesn't need to know anything about the event that triggered it. For example, if you have a function update, you may need to call it sometimes from other functions and other times with an event listener. Since update doesn't care who called it, function update(... args) will allow you to easily invoke it from sibling functions or use it as an event callback.

You could have function update(event:Event = null) as well :)

illustratedlife
11-01-2007, 02:39 AM
args[0] would be the event. But if you are actually handling an event, you should probably be doing function undate(event:Event){ anyway. . .