基于条件语句的多参数调用函数

Tra*_*rax 2 javascript function conditional-statements

标题说明了一切我有以下功能:

var foo = function(arg1, arg2,arg3) {
    // code
}
Run Code Online (Sandbox Code Playgroud)

我想做类似的事情:

foo('bar', (x == true ? arg2, arg3 : arg2,arg3))
Run Code Online (Sandbox Code Playgroud)

但是我遇到了SyntaxError: Unexpected token ,做这样的事情的正确语法是什么?

It-*_*t-Z 6

我会像 JCOC611 所说的那样具有可读性......

然而,“正确”的方式是使用 .apply():

foo.apply(this, (x == true ? [arg1, arg2, arg3] : [arg1 ,arg2, arg3]))
Run Code Online (Sandbox Code Playgroud)


JCO*_*611 5

我认为为了保存几个角色不值得。拥有可读的代码更有价值。只需要一个 minifier/uglyfier,然后执行以下操作:

if(x === true){
   foo('bar', arg2, arg3);
}else{
   foo('bar', arg2, arg3);
}
Run Code Online (Sandbox Code Playgroud)