我正在编写带有可选参数的Javascript函数,并且想为可选参数分配一个默认值。如何为它分配默认值?
我以为是这样,但是不起作用:
function(nodeBox,str = "hai")
{
// ...
}
如果str
为null,未定义或0,则此代码会将其设置为“ hai”
function(nodeBox, str) {
str = str || "hai";
.
.
.
如果还需要传递0,则可以使用:
function(nodeBox, str) {
if (typeof str === "undefined" || str === null) {
str = "hai";
}
.
.
.
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句