Skip to content

Commit 7fdd21b

Browse files
committed
Add example of destructuring assignment in class constructors for options
1 parent 1731f7d commit 7fdd21b

File tree

4 files changed

+52
-0
lines changed

4 files changed

+52
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class Person
2+
constructor: (options) ->
3+
{@name, @age, @height} = options
4+

documentation/index.html.erb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -761,6 +761,11 @@ Expressions
761761
Destructuring assignment can even be combined with splats.
762762
</p>
763763
<%= code_for('patterns_and_splats', 'contents.join("")') %>
764+
<p>
765+
Destructuring assignment is also useful when combined with class constructors
766+
to assign propeties to your instance from an options object passed to the constructor.
767+
</p>
768+
<%= code_for('constructor_destructuring', 'contents.join("")') %>
764769

765770
<p>
766771
<span id="fat_arrow" class="bookmark"></span>

documentation/js/constructor_destructuring.js

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.html

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1735,6 +1735,37 @@ <h2>
17351735
tag = "<impossible>";
17361736
17371737
_ref = tag.split(""), open = _ref[0], contents = 3 <= _ref.length ? __slice.call(_ref, 1, _i = _ref.length - 1) : (_i = 1, []), close = _ref[_i++];
1738+
;alert(contents.join(""));'>run: contents.join("")</div><br class='clear' /></div>
1739+
<p>
1740+
Destructuring assignment is also useful when combined with class constructors
1741+
to assign propeties to your instance from an options object passed to the constructor.
1742+
</p>
1743+
<div class='code'><pre class="idle">class Person
1744+
constructor: (options) -&gt;
1745+
{@name, @age, @height} = options
1746+
1747+
</pre><pre class="idle"><span class="Storage">var</span> Person;
1748+
1749+
Person <span class="Keyword">=</span> (<span class="Storage">function</span>() {
1750+
1751+
<span class="Storage">function</span> <span class="FunctionName">Person</span>(<span class="FunctionArgument">options</span>) {
1752+
<span class="Variable">this</span>.<span class="LibraryConstant">name</span> <span class="Keyword">=</span> options.<span class="LibraryConstant">name</span>, <span class="Variable">this</span>.age <span class="Keyword">=</span> options.age, <span class="Variable">this</span>.<span class="LibraryConstant">height</span> <span class="Keyword">=</span> options.<span class="LibraryConstant">height</span>;
1753+
}
1754+
1755+
<span class="Keyword">return</span> Person;
1756+
1757+
})();
1758+
</pre><script>window.example29 = "class Person\n constructor: (options) -> \n {@name, @age, @height} = options\n\nalert contents.join(\"\")"</script><div class='minibutton load' onclick='javascript: loadConsole(example29);'>load</div><div class='minibutton ok' onclick='javascript: var Person;
1759+
1760+
Person = (function() {
1761+
1762+
function Person(options) {
1763+
this.name = options.name, this.age = options.age, this.height = options.height;
1764+
}
1765+
1766+
return Person;
1767+
1768+
})();
17381769
;alert(contents.join(""));'>run: contents.join("")</div><br class='clear' /></div>
17391770

17401771
<p>

0 commit comments

Comments
 (0)