Is it possible to initialize function parameters in Javascript?

H

Hiroshi Ochi

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/js20/rationale/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
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen in
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,008
Latest member
HaroldDark

Latest Threads

Top