473,811 Members | 3,264 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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="javas cript">
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 10999
In article <40************ *********@news. frii.net>,
Hiroshi Ochi <hi**********@n ospam.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="javas cript">
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**********@n ospam.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="javas cript">
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
compatibilit y 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
compatibili ty 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.undefine d = window.undefine d;

- 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
compatibilit y 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*****@agrico reunited.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**********@n ospam.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.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.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/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #9
Lee <RE************ **@cox.net> wrote in message news:<c8******* *@drn.newsguy.c om>...
Saint Jude said:
Comparing against undefined *may* be risky, and involve backward
compatibilit y 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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
3130
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 alert(a2) I will see 42 there too. I'm doubting, but I was
0
1864
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 2003, although I'm not sure what is actually causing the problem. I can't summarize the issue, so please read on to find out more. I've created a BaseDialogEditor class that allows a developer (namely me) to create Modal editors for controls at...
4
38259
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 global and any functions should be able to access it. I have been having trouble getting this to work. In the 1st of the 2 examples below I initialize the Array "initArray" with 4 form text fields. When I try to use initArray inside either of...
13
2558
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 report with a multiple-value discrete value and a rangevalue. The report shows fine in the viewer, but when I hit the export to pdf
7
1733
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"); alert(pointer); }
4
3854
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. My client-side JavaScript function takes 4 parameters (which are expected to be integers). The idea of passing a single parameter and parsing it on the client has occurred to me, but since I am sure I am not the only person who has situations that...
4
2387
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. Because I want to use some ajax-requests and other javascript-functions on my xhtml, I need to dynamically add event handlers to any possible dom-elements. All solutions I found so fare are for specific, pre-known
3
3758
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) { print(arguments); //etc }
0
9727
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10647
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10398
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10133
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9204
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7669
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4339
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3865
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3017
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.