SitePoint Sponsor

User Tag List

Results 1 to 9 of 9

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2007
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default parameters in javascript functions

    Is it possible to have default parameters in javascript functions like you can in other laguages such as PHP? I would like to do something like this:

    function setTagHoverColor( tagType, className, color = "#FFFF00" )
    {
    }

  2. #2
    SitePoint Guru
    Join Date
    Apr 2007
    Posts
    813
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    unfortunately, no such capability in this point of time, but you can always do this

    Code:
    function setTagHoverColor( tagType, className, color )
    {
    if (color == undefined) {
    color = "#FFFF00";
    }
    }

  3. #3
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The if is not needed:
    Code:
    function setTagHoverColor(tagType, className, color) {
        color = color || "#ff0";
    }

  4. #4
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The latter won't work if 0 is an allowed parameter value. "Undefined" check looks cleaner, though more verbose.

  5. #5
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    True, though color expects a String. You would basically need check to check that as well if you wanna be bulletproof. Somebody might pass a function, null, object etc...

  6. #6
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,631
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I wonder what is driving this question? I thought I had seen the exact same question before, quite recently.

    http://www.sitepoint.com/forums/show...54#post3347854
    Ian Anderson
    www.siteguru.co.uk

  7. #7
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah right, and I replied to that one as well. I am apparently senile.

  8. #8
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,631
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)


    Different guy asking the question this time though.

    Which begs the question - does nobody bother with the search function anymore?
    Ian Anderson
    www.siteguru.co.uk

  9. #9
    SitePoint Guru
    Join Date
    Apr 2007
    Posts
    813
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    furthermore if we have functions that may contain unlimited parameters for example

    function x(a,b,c*) {

    }
    x(1,2,3) // call with parameter 1,2,3
    x(1,2,3,4) // call with parameter 1,2,3,4

    this will need to deal with special variable call argument and special method call apply. Haven't really explore deeply into it yet.


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
  •