是否可以在Javascript中初始化函数参数? [英] Is it possible to initialize function parameters in Javascript?

查看:57
本文介绍了是否可以在Javascript中初始化函数参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


是否可以初始化javascript函数参数(使用MSIE

6.0及以上版本)?根据下面的链接,似乎可以这样做。

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


为什么我一直收到以下代码的错误?


<! - 开始示例代码 - >

< html>

< script language =" javascript">

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

alert(''First Parameter is''+ paramOne);

alert(''Second Parameter is''+ paramTwo);

}


<! - 首次通话 - >

myFunc(我的第一个参数消息,我的第二个参数

消息);

<! - 第二次通话 - >

myFunc(我的第一个参数消息);

< / script>

< / html>

<! - 结束示例代码 - - >


我希望在第二次

电话中显示这是默认消息。

有人会给我一个提示吗?


谢谢,

hiroshi



***通过开发人员发送的指南 http://www.developersdex.com ***

不要只参加USENET ......获得奖励!

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!

推荐答案

文章< 40 * ********************@news.frii.net>,

Hiroshi Ochi< hi ********* *@nospam.com>写道:
In article <40*********************@news.frii.net>,
Hiroshi Ochi <hi**********@nospam.com> wrote:
你好,

是否可以初始化javascript函数参数(使用MSIE
6.0及以上版本)?根据下面的链接,似乎可以这样做。

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




你会注意到这是一个JavaScript 2.0参考。我相信IE

6.0实现Javascript 1.5

Robert



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

Robert


嗨Robert,

嗯,太糟糕了。我想我会做下面的解决方法:


<! - 开始示例代码 - >

< html>

< script language =" javascript">

函数myFunc(paramOne,paramTwo){

if(paramTwo == undefined)

paramTwo = someValue; //初始化默认值

...

}

< / script>

< / html> ;

<! - 结束示例代码 - >


谢谢,

hiroshi


***通过开发人员指南 http://www.developersdex.com ***

不要只是参加USENET ......获得奖励!
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!


Hiroshi Ochi< hi ** ********@nospam.com>在留言新闻中写道:< 40 ********************* @ news.frii.net> ...
Hiroshi Ochi <hi**********@nospam.com> wrote in message news:<40*********************@news.frii.net>...
嗨罗伯特,

嗯,太糟糕了。我想我会做下面的解决方法:

<! - 启动示例代码 - >
< html>
< script language =" ; javascript">
函数myFunc(paramOne,paramTwo){
if(paramTwo == undefined)
paramTwo = someValue; //初始化默认值

}
< / script>
< / html>
<! - 结束示例代码 - > ;
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 -->




与未定义的*进行比较可能*存在风险,并涉及向后兼容性问题。我会去:if(!paramTwo) - 对于一个未定义的

参数测试。


实际上JS有一个很好的快捷方式用于你的目的:


if(paramTwo == undefined)

paramTwo = someValue; //初始化默认值


变为


paramTwo = paramTwo || someValue



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


这篇关于是否可以在Javascript中初始化函数参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆