Last Updated: February 25, 2016
·
788
· ktusznio

Destructuring arguments in Coffeescript

A traditional function might look like this:

createPost = (title, content, options = {}) ->
    category = options.category
    headline = options.headline

Using CoffeeScript's destructuring assignment makes this nicer:

createPost = (title, content, options = {}) ->
    {category, headline} = options

And finally, if you don't need the explicit options variable, you can even do:

createPost = (title, content, {category, headline} = {}) ->

1 Response
Add your response

Note: The default value of "{}" is important to prevent type errors when no "options" argument is provided.

over 1 year ago ·