提问者:小点点

如果不是null Javascript,则传递可选参数的正确方法


在JavaScript中,有没有一种方法可以将可选参数传递到函数中,如果它不是null的话?否则,根本不要传递此参数。

例如下面的函数,如果var_c为null,如何不插入c参数

function({
a:var_a,
b:var_b||'b', //if b is null, set is as string b
c:var_c||null //how to check if c is null and do not insert this parameter at all into function
}) 

共1个答案

匿名用户

见下文

function test(a, b, c){ 
    if (c == undefined){
        ....
    } else {
        ..
    }
....
}

if (c == null){
  test(a,b);
} else {
  test(a,b,c);
}