Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add example of destructuring assignment in constructors for easy options initialization #2378

Merged
merged 1 commit into from
Mar 5, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions documentation/coffee/constructor_destructuring.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Person
constructor: (options) ->
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm pretty sure ({@name, @age, @height}) works.

{@name, @age, @height} = options

5 changes: 5 additions & 0 deletions documentation/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,11 @@ Expressions
Destructuring assignment can even be combined with splats.
</p>
<%= code_for('patterns_and_splats', 'contents.join("")') %>
<p>
Destructuring assignment is also useful when combined with class constructors
to assign propeties to your instance from an options object passed to the constructor.
</p>
<%= code_for('constructor_destructuring', 'contents.join("")') %>

<p>
<span id="fat_arrow" class="bookmark"></span>
Expand Down
12 changes: 12 additions & 0 deletions documentation/js/constructor_destructuring.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -1735,6 +1735,37 @@ <h2>
tag = "<impossible>";

_ref = tag.split(""), open = _ref[0], contents = 3 <= _ref.length ? __slice.call(_ref, 1, _i = _ref.length - 1) : (_i = 1, []), close = _ref[_i++];
;alert(contents.join(""));'>run: contents.join("")</div><br class='clear' /></div>
<p>
Destructuring assignment is also useful when combined with class constructors
to assign propeties to your instance from an options object passed to the constructor.
</p>
<div class='code'><pre class="idle">class Person
constructor: (options) -&gt;
{@name, @age, @height} = options

</pre><pre class="idle"><span class="Storage">var</span> Person;

Person <span class="Keyword">=</span> (<span class="Storage">function</span>() {

<span class="Storage">function</span> <span class="FunctionName">Person</span>(<span class="FunctionArgument">options</span>) {
<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>;
}

<span class="Keyword">return</span> Person;

})();
</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;

Person = (function() {

function Person(options) {
this.name = options.name, this.age = options.age, this.height = options.height;
}

return Person;

})();
;alert(contents.join(""));'>run: contents.join("")</div><br class='clear' /></div>

<p>
Expand Down