Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

optional function parameters

Status
Not open for further replies.

RobBroekhuis

Technical User
Oct 15, 2001
1,971
US
I'm unclear on the syntax for defining a function with optional parameters. One reference I found digging through the web suggested this should work:

function ref(url,linkname,extra='')
{
window.document.write('<a href= target=&quot;resource&quot;>'
+linkname+'</a>'+extra);
}

but IE throws an error. How should I rewrite this?


Rob
[flowerface]
 
Code:
   function ref(url, linkname, extra) {
      if (extra == null) {
         extra = &quot;&quot;
      }
      window.document.write('<a href=[URL unfurl="true"]http://&quot;;'+url[/URL] + '&quot; target=&quot;resource&quot;>' + linkname + '</a>' + extra);
      return;
    }
That's one way to do it.
OK?

________________________________________
[hippy]Roger J Coult; Grimsby, UK
In the game of life the dice have an odd number of sides.
 
I just wanted to throw in a cool trick. Instead of using:
Code:
if (extra == null) {
you can use:
Code:
if (!extra) {
to check for the existance of a value.

-kaht

banghead.gif
 
woja, kaht,
Yeah, that's the approach I did take (kaht's actually). I just came across the other syntax on the web and was wondering if I could use it. I think the particular page was describing Javascript 2.0


Rob
[flowerface]
 
There's a problem with using kaht's solution, in general (it's ok with strings) but:
Code:
   function mult1(aNumber, aBase) {
      if (!aBase) aBase = 1;
      return aNumber * aBase;
   }
   function mult2(aNumber, aBase) {
      if (aBase == null) aBase = 1;
      return aNumber * aBase;
   }
   window.alert(&quot;1. &quot; + mult1(10));
   window.alert(&quot;2. &quot; + mult2(10));
   window.alert(&quot;3. &quot; + mult1(10, 0));
   window.alert(&quot;4. &quot; + mult2(10, 0));
Will display:
[tt]
1. 10
2. 10
3. 0
4. 10
[/tt]
because [tt]0[/tt] will cause the [tt]aBase[/tt] argument to be changed to [tt]1[/tt] in [tt]mult1()[/tt]. Omitted arguments in a function always have the value [tt]null[/tt] but other values than [tt]null[/t] are treated as [tt]false[/tt] and cause the &quot;optional argument&quot; code to be evoked using kaht's method.

Hope this helps.

________________________________________
[hippy]Roger J Coult; Grimsby, UK
In the game of life the dice have an odd number of sides.
 
Whoops!:

because [tt]0[/tt] will cause the aBase argument to be changed to [tt]1[/tt] in [tt]mult1()[/tt]. Omitted arguments in a function always have the value null but other values than [tt]null[/tt] are treated as [tt]false[/tt] and cause the &quot;optional argument&quot; code to be evoked using kaht's method.

Hope this helps.


________________________________________
[hippy]Roger J Coult; Grimsby, UK
In the game of life the dice have an odd number of sides.
 
Roger, I would consider multi1 to have the correct behavior. If you multiply 10 by nothing, you should get zero, not 10. In other words,
Code:
if (!aBase) aBase = 0;
is correct.

Sincerely,

Tom Anderson
Order amid Chaos, Inc.
 
Whoops, I got the answers wrong.
[tt]mult1[/tt] would display [tt]10[/tt] in 3 and [tt]mult2[/tt] would display [tt]0[/tt] in 4. That makes us both right, Tom.

Soryy for the error (should have checked).


[tt]________________________________________________________________
[pc2]Roger
Life is a game of cards in which the deck contains only jokers.[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top