473,386 Members | 1,803 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 10970
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: svend | last post by:
I'm messing with some code here... Lets say I have this array: a1 = ; And I apply slice(0) on it, to create a copy: a2 = a1.slice(0); But this isn't a true copy. If I go a1 = 42, and then...
0
by: Dave | last post by:
Hi everyone, (I already posted this to the VS.NET IDE news group without any responses, so I'm attempting one more time in this group) The issue I'm having is occuring in the IDE of VS.NET...
4
by: Mark Hannon | last post by:
I am trying to initialize an array only once so it can be seen & used by any functions that need it. As I understand it, if a variable is declared by itself outside of any functions, its scope is...
13
by: Alison Givens | last post by:
....... that nobody knows the answer. I can't imagine that I am the only one that uses parameters in CR. So, my question again: I have the following problem. (VB.NET 2003 with CR) I have a...
7
by: Andy Baxter | last post by:
Is there a way to pass parameters to functions by reference instead of by value in javascript? i.e. if I have code like this: function setPointer(pointer) { pointer=new String("hello world");...
4
by: Nathan Sokalski | last post by:
I am a beginner with AJAX, and have managed to learn how to use it when passing single parameters, but I want to return more than one value to the client-side JavaScript function that displays it....
4
by: simon | last post by:
hi, I would like to separate my javascript completely from my xhtml. in the end there should be only <script type="text/javascript" src="javalib.js"></script> in the head-tag to my javascript....
3
by: monojohnny | last post by:
Hi, I know you can do stuff with introspection to gather up passed-in args for a Javascript function and that you can list all defined functions like: function a(abc,xyz,zzz) {...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.