469,312 Members | 2,493 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,312 developers. It's quick & easy.

Is it possible to initialize function parameters in Javascript?

Hello,

Is it possible to initialize javascript function parameters (using MSIE
6.0 and above)? According to the link below, it seems possible to do
this.

http://www.mozilla.org/js/language/j...ale/named.html

Why I keep getting error with the below code?

<!-- start sample code -->
<html>
<script language="javascript">
function myFunc(paramOne,paramTwo="This is Default Message"){
alert('First Parameter is '+ paramOne );
alert('Second Parameter is '+ paramTwo );
}

<!-- first call -->
myFunc("My First Parameter message","My Second Parameter
Message");
<!-- second call -->
myFunc("My First Parameter message");
</script>
</html>
<!-- end sample code -->

I am expecting it to show the 'This is Default Message' in the second
call.

Would somebody give me a hint?

Thanks,
hiroshi


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 23 '05 #1
13 10821
In article <40*********************@news.frii.net>,
Hiroshi Ochi <hi**********@nospam.com> wrote:
Hello,

Is it possible to initialize javascript function parameters (using MSIE
6.0 and above)? According to the link below, it seems possible to do
this.

http://www.mozilla.org/js/language/j...ale/named.html


You'll note that this is a JavaScript 2.0 reference. I believe that IE
6.0 implements Javascript 1.5

Robert
Jul 23 '05 #2
Hi Robert,

Hmm, too bad. I think I'll just do the below workaround:

<!-- start sample code -->
<html>
<script language="javascript">
function myFunc(paramOne,paramTwo){
if(paramTwo == undefined)
paramTwo = someValue; // initialize default value
...
}
</script>
</html>
<!-- end sample code -->

thanks,
hiroshi

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 23 '05 #3
Hiroshi Ochi <hi**********@nospam.com> wrote in message news:<40*********************@news.frii.net>...
Hi Robert,

Hmm, too bad. I think I'll just do the below workaround:

<!-- start sample code -->
<html>
<script language="javascript">
function myFunc(paramOne,paramTwo){
if(paramTwo == undefined)
paramTwo = someValue; // initialize default value
...
}
</script>
</html>
<!-- end sample code -->


Comparing against undefined *may* be risky, and involve backward
compatibility problems. I'd go for: if(!paramTwo) - for an undefined
parameter test.

In fact JS has a good shortcut for your purpose:

if(paramTwo == undefined)
paramTwo = someValue; // initialize default value

becomes

paramTwo = paramTwo || someValue
Jul 23 '05 #4
Lee
Saint Jude said:
Comparing against undefined *may* be risky, and involve backward
compatibility problems. I'd go for: if(!paramTwo) - for an undefined
parameter test.

In fact JS has a good shortcut for your purpose:

if(paramTwo == undefined)
paramTwo = someValue; // initialize default value

becomes

paramTwo = paramTwo || someValue

I doubt that undefined would be a problem with any
implementation of Javascript. On the other hand,
both of your methods assume that zero, false, and
"" are not valid parameter values.

Jul 23 '05 #5
Lee wrote:
Saint Jude said:
Comparing against undefined *may* be risky, and involve backward
compatibility problems. I'd go for: if(!paramTwo) - for an undefined
parameter test.

In fact JS has a good shortcut for your purpose:

if(paramTwo == undefined)
paramTwo = someValue; // initialize default value

becomes

paramTwo = paramTwo || someValue

I doubt that undefined would be a problem with any
implementation of Javascript. On the other hand,
both of your methods assume that zero, false, and
"" are not valid parameter values.


IE 4 doesn't like it; it produces the extremely helpful error message
"undefined is undefined." :)

But there is no need to risk the problem as - undefined - can be
implemented in unsupporting environments with:-

this.undefined = this.undefined;

-or:-

window.undefined = window.undefined;

- executed as inline code as the page loads.

Recent discussion on the identity operator - === - suggest that it has
probably moved into the set of more recent javascript features that can
now universally be relied upon as both Netscape 4 and IE 4 understand it
and they are the generation of browsers apparently now dropping out of
use. The identity operator doesn't type convert so identity comparison
with a normalised - undefined - should give an unambiguous result:

if(paramTwo === undefined){
... //paramTwo needs defaulting
}

- on current browsers without the risk of errors. But a - typeof - test
(if slower) should also produce a reliable result:-

if(typeof paramTwo == "undefined"){
... //paramTwo needs defaulting
}

- without any real language version concerns as typeof has been around
since JavaScript 1.1.

Richard.
Jul 23 '05 #6
Lee wrote:
Saint Jude said:
Comparing against undefined *may* be risky, and involve backward
compatibility problems. I'd go for: if(!paramTwo) - for an undefined
parameter test.

In fact JS has a good shortcut for your purpose:

if(paramTwo == undefined)
paramTwo = someValue; // initialize default value

becomes

paramTwo = paramTwo || someValue


I doubt that undefined would be a problem with any
implementation of Javascript. On the other hand,
both of your methods assume that zero, false, and
"" are not valid parameter values.


Which is why it is probably best to test for precisely what the parameter
should be:

if (typeof paramTwo != 'string') {
paramTwo = 'default string';
}

- or -

if (typeof paramTwo != 'number') {
paramTwo = 0;
}

etc

--
| Grant Wagner <gw*****@agricoreunited.com>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/...ce/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/a...ence_entry.asp

* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
* http://www.mozilla.org/docs/web-deve...upgrade_2.html
Jul 23 '05 #7
JRS: In article <40*********************@news.frii.net>, seen in
news:comp.lang.javascript, Hiroshi Ochi <hi**********@nospam.com> posted
at Thu, 13 May 2004 06:06:42 :
Is it possible to initialize javascript function parameters (using MSIE
6.0 and above)? function myFunc(paramOne,paramTwo="This is Default Message"){
...


Be aware that one can do something like

function myFunc(paramOne, paramTwo) {
if (!paramTwo) paramTwo = "This is Default Message"
....

But consider whether one might need to be able to supply such as 0,
false, NaN, ... for paramTwo.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #8
Lee <RE**************@cox.net> writes:
I doubt that undefined would be a problem with any
implementation of Javascript.


In later versions of Javascript, "undefined" is merely a global
variable. In, e.g., IE 5, that variable was not defined, so writing
if (blah == undefined) ...
would give the undeclared variable error:
'undefined' is undefined
Kindof cute, really :)

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #9
Lee <RE**************@cox.net> wrote in message news:<c8********@drn.newsguy.com>...
Saint Jude said:
Comparing against undefined *may* be risky, and involve backward
compatibility problems. I'd go for: if(!paramTwo) - for an undefined
parameter test.

In fact JS has a good shortcut for your purpose:

if(paramTwo == undefined)
paramTwo = someValue; // initialize default value

becomes

paramTwo = paramTwo || someValue

I doubt that undefined would be a problem with any
implementation of Javascript. On the other hand,
both of your methods assume that zero, false, and
"" are not valid parameter values.


Lee, you are absolutely right about zero, false etc. I realised while
doing my laundry. That syntax is unfortunately unsafe to use.

Re: undefined

I'm not so sure about this. Perhaps you can put me straight.
According to MS JScript specification, undefined requries
JScript 5.5 (IE 5.5+). So I would suggest:
if(typeof(arg)=="undefined")
instead.

BTW, which is the safer to use - typeof operator, or typeof function ?
Jul 23 '05 #10
th**************@hotmail.com (Saint Jude) writes:
BTW, which is the safer to use - typeof operator, or typeof function ?


There is no typeof function, only the operator.
Writing
typeof foo
and
typeof(foo)
is equivalent, just as
1+4
and
1+(4)
are. It's just a parenthesis around an expression.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #11
Lee
Saint Jude said:
I'm not so sure about this. Perhaps you can put me straight.
According to MS JScript specification, undefined requries
JScript 5.5 (IE 5.5+). So I would suggest:
if(typeof(arg)=="undefined")
instead.


Yes, that's actually what I always use. I posted in haste.

Jul 23 '05 #12
JRS: In article <c8********@drn.newsguy.com>, seen in
news:comp.lang.javascript, Lee <RE**************@cox.net> posted at Fri,
14 May 2004 07:16:51 :
Saint Jude said:
Comparing against undefined *may* be risky, and involve backward
compatibility problems. I'd go for: if(!paramTwo) - for an undefined
parameter test.

In fact JS has a good shortcut for your purpose:

if(paramTwo == undefined)
paramTwo = someValue; // initialize default value

becomes

paramTwo = paramTwo || someValue

I doubt that undefined would be a problem with any
implementation of Javascript. On the other hand,
both of your methods assume that zero, false, and
"" are not valid parameter values.


Not quite; if one of them is a valid value then it can be the default.

P2 = 0
P2 = P2 || 0

results in P2 becoming the second 0 & no longer the first one, an
undetectable assignment.
ISTM that, in IE4,
X |= Y is X = X | Y
but X ||= Y is illegal. I wonder why.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #13
Hiroshi Ochi <hi**********@nospam.com> wrote:
Hi Robert,

Hmm, too bad. I think I'll just do the below workaround:

<!-- start sample code -->
<html>
<script language="javascript">
function myFunc(paramOne,paramTwo){
if(paramTwo == undefined)
paramTwo = someValue; // initialize default value
...
}
</script>
</html>
<!-- end sample code -->


x == undefined evaluates to true if x is null. Use typeof paramTwo ==
"undefined" if that matters to you.

Regards,
Steve
Jul 23 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Mark Hannon | last post: by
13 posts views Thread by Alison Givens | last post: by
3 posts views Thread by monojohnny | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.