SitePoint Sponsor |
|
User Tag List
Results 1 to 9 of 9
-
Apr 15, 2007, 05:35 #1
- 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" )
{
}
-
Apr 15, 2007, 06:32 #2
- 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"; } }
-
Apr 15, 2007, 09:01 #3
- 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"; }
-
Apr 16, 2007, 01:52 #4
- 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.
-
Apr 16, 2007, 02:00 #5
- 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...
-
Apr 16, 2007, 07:17 #6
- 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
-
Apr 16, 2007, 07:21 #7
- 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.
-
Apr 16, 2007, 07:46 #8
- 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?
-
Apr 16, 2007, 10:29 #9
- 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