jashkenas--coffeescript/documentation/sections/splats.md

1.5 KiB
Raw Blame History

Splats, or Rest Parameters/Spread Syntax

The JavaScript arguments object is a useful way to work with functions that accept variable numbers of arguments. CoffeeScript provides splats ..., both for function definition as well as invocation, making variable numbers of arguments a little bit more palatable. ES2015 adopted this feature as their rest parameters.

codeFor('splats', true)

Splats also let us elide array elements...

codeFor('array_spread', 'all')

...and object properties.

codeFor('object_spread', 'JSON.stringify(currentUser)')

In ECMAScript this is called spread syntax, and has been supported for arrays since ES2015 but is coming soon for objects. Until object spread syntax is officially supported, the CoffeeScript compiler outputs the same polyfill as Babels rest spread transform; but once it is supported, we will revise the compilers output. Note that there are very subtle differences between the polyfill and the current proposal.