SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast ptolemy's Avatar
    Join Date
    Feb 2002
    Location
    Geneva, Switzerland
    Posts
    74
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Creating a function with optional arguments

    How can I create a function that takes optional arguments?

    -ptolemy

  2. #2
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    All arguments are optional automatically.

    function name(arguments){}

    Flawless
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---

  3. #3
    SitePoint Enthusiast ptolemy's Avatar
    Join Date
    Feb 2002
    Location
    Geneva, Switzerland
    Posts
    74
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Don't know how I never thought of that one...

    Thanks,


    -ptolemy

  4. #4
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)


    Flawless
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---

  5. #5
    JavaScript Guru (Big Ego) Arielladog's Avatar
    Join Date
    Jul 1999
    Location
    SC, USA
    Posts
    390
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey,

    Just remember that inside a function there is an array called "arguments" that you can use to access all arguments:

    function myFunc(){
    for(i=0;i<arguments.length;i++){
    alert("arguments["+i+"] = " + arguments[i]);
    }
    }

    myFunc("this",1,"IS",4,'U');

    aDog

  6. #6
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Very true - except i rarely find an occasion that i need that.

    However i VERY rarely have to pass arguments around.

    The closest i do is to pass (event) around for Netscape
    browsers. ( which don't recieve event properly )

    Flawless
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---

  7. #7
    JavaScript Guru (Big Ego) Arielladog's Avatar
    Join Date
    Jul 1999
    Location
    SC, USA
    Posts
    390
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    One way around that is to just go:

    function handleEvents(){
    event = arguments.callee.caller.arguments[0];
    }

    http://javascriptkit.com/dhtmltutors/nsevents.shtml

    aDog

  8. #8
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Granted - but not really all that worthwhile - is it...
    Especially when the callee property doesn't always work
    the way you want it to in all browsers.

    You're far better off calling the function with handleEvent(event) and delcaring it in the function declaration just to be safe!

    Flawless
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •