Skip to content

Commit

Permalink
Document destructuring defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
lydell committed Aug 29, 2015
1 parent c0c13c1 commit 10aca2a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
6 changes: 3 additions & 3 deletions documentation/coffee/constructor_destructuring.coffee
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class Person
constructor: (options) ->
{@name, @age, @height} = options
constructor: (options) ->
{@name, @age, @height = 'average'} = options

tim = new Person age: 4
tim = new Person name: 'Tim', age: 4

12 changes: 8 additions & 4 deletions documentation/index.html.js
Original file line number Diff line number Diff line change
Expand Up @@ -766,9 +766,7 @@ Expressions
<p>
<span id="destructuring" class="bookmark"></span>
<b class="header">Destructuring Assignment</b>
To make extracting values from complex arrays and objects more convenient,
CoffeeScript implements ECMAScript Harmony's proposed
<a href="https://wiki.ecmascript.org/doku.php?id=harmony:destructuring">destructuring assignment</a>
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
Expand Down Expand Up @@ -797,7 +795,13 @@ Expressions
Destructuring assignment is also useful when combined with class constructors
to assign properties to your instance from an options object passed to the constructor.
</p>
<%= codeFor('constructor_destructuring', 'tim.age') %>
<%= codeFor('constructor_destructuring', 'tim.age + " " + tim.height') %>
<p>
The above example also demonstrates that if properties are missing in the
destructured object or array, you can, just like in JavaScript, provide
defaults. The difference with JavaScript is that CoffeeScript, as always,
treats both null and undefined the same.
</p>

<p>
<span id="fat-arrow" class="bookmark"></span>
Expand Down

0 comments on commit 10aca2a

Please sign in to comment.