CoffeeScript: Expand array in function call

2019-04-25 11:23发布

In Ruby I can call methods with array elements used as positional parameters like this

method(fixed_arg1, fixed_arg2, *array_of_additional_args)

Here the "*" operator expands the array in place.

I'm trying to do the same in CoffeeScript, but haven't found a way. Specifically, I want to pass additional arguments in a call to a jQuery function

$('#my-element').toggle(true, *config.toggleOptions)

The syntax above does not work, obviously, and I'm looking for a way that does.

2条回答
▲ chillily
2楼-- · 2019-04-25 12:00

Try

$('#my-element').toggle(true, config.toggleOptions...)
查看更多
叛逆
3楼-- · 2019-04-25 12:10

You need to splat it.

fun(1,2,3,4,5)

fun = (first, second, rest...) ->
alert first # 1
alert second # 2
alert rest   # [3, 4, 5 ]
查看更多
登录 后发表回答