Skip to content

Commit 304432c

Browse files
committed
Merge pull request #2417 from gabehollombe/doc_for_switch_with_no_control_expression
Documentation for switch statements with no control expression
2 parents f8c6b49 + 334dcbd commit 304432c

5 files changed

+65
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class Person
2+
constructor: (options) ->
3+
{@name, @age, @height} = options
4+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
score = 76
2+
grade = switch
3+
when score < 60 then 'F'
4+
when score < 70 then 'D'
5+
when score < 80 then 'C'
6+
when score < 90 then 'B'
7+
else 'A'
8+
# grade == 'C'

documentation/index.html.erb

+10
Original file line numberDiff line numberDiff line change
@@ -812,6 +812,11 @@ Expressions
812812
Destructuring assignment can even be combined with splats.
813813
</p>
814814
<%= code_for('patterns_and_splats', 'contents.join("")') %>
815+
<p>
816+
Destructuring assignment is also useful when combined with class constructors
817+
to assign propeties to your instance from an options object passed to the constructor.
818+
</p>
819+
<%= code_for('constructor_destructuring', 'contents.join("")') %>
815820

816821
<p>
817822
<span id="fat-arrow" class="bookmark"></span>
@@ -869,6 +874,11 @@ Expressions
869874
</p>
870875
<%= code_for('switch') %>
871876

877+
<p>
878+
Switch statements can also be used without a control expression, turning them in to a cleaner alternative to if/else chains.
879+
</p>
880+
<%= code_for('switch_with_no_expression') %>
881+
872882
<p>
873883
<span id="try" class="bookmark"></span>
874884
<b class="header">Try/Catch/Finally</b>

documentation/js/constructor_destructuring.js

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.html

+31
Original file line numberDiff line numberDiff line change
@@ -1786,6 +1786,37 @@ <h2>
17861786
tag = "<impossible>";
17871787
17881788
_ref = tag.split(""), open = _ref[0], contents = 3 <= _ref.length ? __slice.call(_ref, 1, _i = _ref.length - 1) : (_i = 1, []), close = _ref[_i++];
1789+
;alert(contents.join(""));'>run: contents.join("")</div><br class='clear' /></div>
1790+
<p>
1791+
Destructuring assignment is also useful when combined with class constructors
1792+
to assign propeties to your instance from an options object passed to the constructor.
1793+
</p>
1794+
<div class='code'><pre class="idle">class Person
1795+
constructor: (options) -&gt;
1796+
{@name, @age, @height} = options
1797+
1798+
</pre><pre class="idle"><span class="Storage">var</span> Person;
1799+
1800+
Person <span class="Keyword">=</span> (<span class="Storage">function</span>() {
1801+
1802+
<span class="Storage">function</span> <span class="FunctionName">Person</span>(<span class="FunctionArgument">options</span>) {
1803+
<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>;
1804+
}
1805+
1806+
<span class="Keyword">return</span> Person;
1807+
1808+
})();
1809+
</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;
1810+
1811+
Person = (function() {
1812+
1813+
function Person(options) {
1814+
this.name = options.name, this.age = options.age, this.height = options.height;
1815+
}
1816+
1817+
return Person;
1818+
1819+
})();
17891820
;alert(contents.join(""));'>run: contents.join("")</div><br class='clear' /></div>
17901821

17911822
<p>

0 commit comments

Comments
 (0)