CoffeeScript: Expand array in function call

断了今生、忘了曾经 提交于 2019-12-06 23:49:32

问题


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.


回答1:


Try

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



回答2:


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 ]


来源:https://stackoverflow.com/questions/10960374/coffeescript-expand-array-in-function-call

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!