Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call function with multiple arguments based on conditional statement

Title says it all I have the following function :

var foo = function(arg1, arg2,arg3) {
    // code
}

I want to do something like :

foo('bar', (x == true ? arg2, arg3 : arg2,arg3))

But I get hit with SyntaxError: Unexpected token , what is the right syntax to do something like this?

like image 957
Trax Avatar asked Jul 30 '26 00:07

Trax


1 Answers

I would go with readability as JCOC611 said...

Yet, the "right" way is using .apply():

foo.apply(this, (x == true ? [arg1, arg2, arg3] : [arg1 ,arg2, arg3]))
like image 166
It-Z Avatar answered Aug 01 '26 18:08

It-Z



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!