CoffeeScript: Expand array in function call
Asked Answered
P

2

9

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.

Permission answered 9/6, 2012 at 10:52 Comment(0)
L
11

Try

$('#my-element').toggle(true, config.toggleOptions...)
Lepidote answered 9/6, 2012 at 11:14 Comment(1)
Thanks. Now that you show that example, it seems kind of obvious, but it wasn't before I knew it.Permission
B
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 ]
Bul answered 31/12, 2013 at 7:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.