-
How to pass an undefined number of arguments to a function?
I'm not sure if it's possible but if yes can someone help?
Thanks!!!
-
The greatest pleasure in life is doing what people say you cannot do.
- Walter Bagehot
The height of cleverness is to be able to conceal it.
- Francois de La Rochefoucauld
-
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
-
Why would you do that?
Just do:
function (... args){
It will make an array called args from any arguments you pass it.
-
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!
-
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.
-
oh I see, thanks a lot ! much better!
-
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.
-
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?
Last edited by phantobass; 11-01-2007 at 01:07 AM.
-
Originally Posted by illustratedlife
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
-
args[0] would be the event. But if you are actually handling an event, you should probably be doing function undate(event:Event){ anyway. . .
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|