帮助我编写一个简单的函数,在未声明时传递参数值 0

发布于 2024-10-04 19:27:07 字数 379 浏览 0 评论 0原文

我不知道最好的编写方式是什么以及如何...我有一个像

function myFunc(myVal1, myVal2){/*... various things ...*/};

myVal1 和 myVal2 是数字的

函数有时我希望能够通过调用 myFunc(myVal1 )。这样做我希望 myVal2 的标准值(如果没有声明的话)为 0。

我想写一些东西,

function myFunc(myVal1, myVal2){
    if(myVal2 == NaN)
         myVal2 = 0;
    }
}

但我不认为这是正确的方法。 非常感谢您的帮助!

I don't know what is the best way to write it and how to... I have a function like

function myFunc(myVal1, myVal2){/*... various things ...*/};

where myVal1 and myVal2 are numbers

Sometimes I want to be able to recall the function passing only one parameter just by calling myFunc(myVal1). Doing so I want that myVal2 would have a standard value, if not declared, of 0.

I was thinking to write something

function myFunc(myVal1, myVal2){
    if(myVal2 == NaN)
         myVal2 = 0;
    }
}

But I don't believe that it is the right way to do it.
Thx very much for the help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

明月松间行 2024-10-11 19:27:07

未设置的参数的类型未定义,请像这样测试它。

function myFunc(myVal1, myVal2){ 
    if(typeof myVal2 == "undefined") 
         myVal2 = 0; 
    //the rest of the function goes here
} 

Arguments that are not set have type undefined, test for it like this.

function myFunc(myVal1, myVal2){ 
    if(typeof myVal2 == "undefined") 
         myVal2 = 0; 
    //the rest of the function goes here
} 
记忆之渊 2024-10-11 19:27:07
function myFunc(myVal1, myVal2){ 
    // if myVal2 is defined, keep the value, otherwise assign 0
    myVal2 = myVal2 || 0; 
} 

应该可以解决问题。

更多通往罗马的方法(除了 typeof myVal2 == "undefined"):

function myFunc(myVal1, myVal2){ 
    // if myVal2 is defined, keep the value, otherwise assign 0
    // using a ternary operator
    var myVal2check = myVal2 ? myVal2 :  0; 
}


function myFunc(myVal1, myVal2){ 
    // if myVal2 is defined, keep the value, otherwise assign 0
    // if myVal2 needs to be an integer
    myVal2 = parseInt(myVal2,10) ||  0; 
}

function myFunc(myVal1, myVal2){ 
    // if myVal2 is defined, keep the value, otherwise assign 0
    // using a regexp to determine the 'undefinedness'
    var myVal2check = String(myVal2).match(/undef/i) ? myVal2 :  0; 
}

最后,特别是对于 David马滕森

function myFunc(myVal1){ 
    // no myVal2 defined in the parameter section
    var myVal2check = window.myVal2 ? myVal2 :  0;
}
function myFunc(myVal1, myVal2){ 
    // if myVal2 is defined, keep the value, otherwise assign 0
    myVal2 = myVal2 || 0; 
} 

should do the trick.

more ways to Rome (besides typeof myVal2 == "undefined"):

function myFunc(myVal1, myVal2){ 
    // if myVal2 is defined, keep the value, otherwise assign 0
    // using a ternary operator
    var myVal2check = myVal2 ? myVal2 :  0; 
}


function myFunc(myVal1, myVal2){ 
    // if myVal2 is defined, keep the value, otherwise assign 0
    // if myVal2 needs to be an integer
    myVal2 = parseInt(myVal2,10) ||  0; 
}

function myFunc(myVal1, myVal2){ 
    // if myVal2 is defined, keep the value, otherwise assign 0
    // using a regexp to determine the 'undefinedness'
    var myVal2check = String(myVal2).match(/undef/i) ? myVal2 :  0; 
}

and finally, especially for David Mårtensson

function myFunc(myVal1){ 
    // no myVal2 defined in the parameter section
    var myVal2check = window.myVal2 ? myVal2 :  0;
}
分开我的手 2024-10-11 19:27:07
if (!myVal2) {
  myVal2 = 0;
}

如果您希望 myVal2 默认为 0 以外的任何值,则必须更加明确,因为 0 的计算结果为 false。所以:

if (!myVal2===undefined) {
  myVal2 = 3;
}
if (!myVal2) {
  myVal2 = 0;
}

If you want myVal2 to default to anything other than 0, you have to be more explicit, as 0 evaluates to false. So:

if (!myVal2===undefined) {
  myVal2 = 3;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文