A Flash Developer Resource Site

Results 1 to 11 of 11

Thread: How to pass an undefined number of arguments to a function?

  1. #1
    Junior Member
    Join Date
    Oct 2007
    Posts
    22

    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!!!

  2. #2
    Knows where you live
    Join Date
    Oct 2004
    Posts
    944
    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

  3. #3
    Junior Member
    Join Date
    Oct 2007
    Posts
    22
    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

  4. #4
    Multitouch Aficionado
    Join Date
    Mar 2006
    Posts
    275
    Why would you do that?

    Just do:

    function (... args){

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

  5. #5
    Junior Member
    Join Date
    Oct 2007
    Posts
    22
    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!

  6. #6
    Multitouch Aficionado
    Join Date
    Mar 2006
    Posts
    275
    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.

  7. #7
    Junior Member
    Join Date
    Oct 2007
    Posts
    22
    oh I see, thanks a lot ! much better!

  8. #8
    Multitouch Aficionado
    Join Date
    Mar 2006
    Posts
    275
    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.

  9. #9
    Junior Member
    Join Date
    Oct 2007
    Posts
    22
    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.

  10. #10
    Member
    Join Date
    May 2007
    Posts
    35
    Quote 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

  11. #11
    Multitouch Aficionado
    Join Date
    Mar 2006
    Posts
    275
    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
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center