1
0
Fork 0
mirror of https://github.com/jashkenas/coffeescript.git synced 2022-11-09 12:23:24 -05:00
jashkenas--coffeescript/documentation/sections/destructuring.md
Geoffrey Booth b28e398396 [CS2] Docs updates (#4536)
* Docs: named functions and function declarations

* No more prototypal `extends`; update docs and example

* More comprehensive documentation of the existential operator; closes #1631

* Better document operators, including `from`

* No fat arrow class methods anymore

* Destructuring shouldn’t say that default values are applied in case of undefined or null

* Spinoff generator and async functions into their own sections; reorder things so that the sections on functions come just before classes, and destructuring goes next to the operators (which discuss assignment)

* Rewrite “CoffeeScript 2” section, making it less practical and more explanatory; move practical info into “Usage”

* Update “Variable Scoping and Lexical Safety” section to remove incorrect reference to Ruby (fixes #2360), add missing details about the safety wrapper, add note about `let`/`const`.

* Updated browser compiler

* Updated docs

* Rewrite Literate CoffeeScript breaking changes

* Split apart the “Breaking Changes” and “Unsupported Features” sections into separate sidebar items and files

* Add example of `not in`, closes #3281

* Fix words in bold that should be in backticks

* Consolidate some breaking changes sections

* Add Node API documentation; closes #3551

* Move the chaining documentation out of the changelog into its own section
2017-05-05 19:44:11 -07:00

1.7 KiB
Raw Blame History

Destructuring Assignment

Just like JavaScript (since ES2015), CoffeeScript has destructuring assignment syntax. When you assign an array or object literal to a value, CoffeeScript breaks up and matches both sides against each other, assigning the values on the right to the variables on the left. In the simplest case, it can be used for parallel assignment:

codeFor('parallel_assignment', 'theBait')

But its also helpful for dealing with functions that return multiple values.

codeFor('multiple_return_values', 'forecast')

Destructuring assignment can be used with any depth of array and object nesting, to help pull out deeply nested properties.

codeFor('object_extraction', 'name + "-" + street')

Destructuring assignment can even be combined with splats.

codeFor('patterns_and_splats', 'contents.join("")')

Expansion can be used to retrieve elements from the end of an array without having to assign the rest of its values. It works in function parameter lists as well.

codeFor('expansion', 'first + " " + last')

Destructuring assignment is also useful when combined with class constructors to assign properties to your instance from an options object passed to the constructor.

codeFor('constructor_destructuring', 'tim.age + " " + tim.height')

The above example also demonstrates that if properties are missing in the destructured object or array, you can, just like in JavaScript, provide defaults. Note though that unlike with the existential operator, the default is only applied with the value is missing or undefinedpassing null will set a value of null, not the default.