mirror of
https://github.com/jashkenas/coffeescript.git
synced 2022-11-09 12:23:24 -05:00
more better super docs, better switch docs
This commit is contained in:
parent
64879cdc66
commit
cdfb5091be
4 changed files with 40 additions and 14 deletions
|
@ -2,6 +2,8 @@ switch day
|
|||
case "Tuesday" then eat_breakfast()
|
||||
case "Wednesday" then go_to_the_park()
|
||||
case "Saturday"
|
||||
if day is bingo_day then go_to_bingo().
|
||||
if day is bingo_day
|
||||
go_to_bingo()
|
||||
go_dancing().
|
||||
case "Sunday" then go_to_church()
|
||||
else go_to_work().
|
|
@ -290,6 +290,12 @@ coffee-script --print app/scripts/*.cs > concatenation.js</pre>
|
|||
<a href="http://code.google.com/p/base2/">Base2</a>,
|
||||
<a href="http://prototypejs.org/">Prototype.js</a>,
|
||||
<a href="http://jsclass.jcoglan.com/">JS.Class</a>, etc.
|
||||
The libraries provide syntactic sugar, but the built-in inheritance would
|
||||
be completely usable if it weren't for one small exception:
|
||||
it's very awkward to call <b>super</b>, the prototype object's
|
||||
implementation of the current function. CoffeeScript converts
|
||||
<tt>super()</tt> calls into calls against the immediate ancestor's
|
||||
method of the same name.
|
||||
</p>
|
||||
<%= code_for('super', true) %>
|
||||
|
||||
|
@ -320,12 +326,14 @@ coffee-script --print app/scripts/*.cs > concatenation.js</pre>
|
|||
|
||||
<p id="switch">
|
||||
<b class="header">Switch/Case/Else</b>
|
||||
Switch statements in JavaScript are fundamentally broken. You can only
|
||||
do string comparisons, and need to break at the end of each case
|
||||
statment to prevent falling through to the default case. CoffeeScript
|
||||
compiles switch statements into if-else chains, allowing you to
|
||||
Switch statements in JavaScript are rather broken. You can only
|
||||
do string comparisons, and need to remember to <b>break</b> at the end of
|
||||
every <b>case</b> statement to avoid accidentally falling through to
|
||||
the default case. CoffeeScript
|
||||
compiles <b>switch</b> statements into JavaScript if-else chains, allowing you to
|
||||
compare any object (via <b>===</b>), preventing fall-through, and resulting
|
||||
in a returnable expression.
|
||||
in a returnable, assignable expression. To specify the default case, just
|
||||
use <tt>else</tt>.
|
||||
</p>
|
||||
<%= code_for('switch') %>
|
||||
|
||||
|
|
|
@ -4,7 +4,10 @@
|
|||
} else if (day === "Wednesday") {
|
||||
go_to_the_park();
|
||||
} else if (day === "Saturday") {
|
||||
day === bingo_day ? go_to_bingo() : null;
|
||||
if (day === bingo_day) {
|
||||
go_to_bingo();
|
||||
go_dancing();
|
||||
}
|
||||
} else if (day === "Sunday") {
|
||||
go_to_church();
|
||||
} else {
|
||||
|
|
27
index.html
27
index.html
|
@ -505,6 +505,12 @@ var three_to_six = nums.slice(3, 6 + 1);
|
|||
<a href="http://code.google.com/p/base2/">Base2</a>,
|
||||
<a href="http://prototypejs.org/">Prototype.js</a>,
|
||||
<a href="http://jsclass.jcoglan.com/">JS.Class</a>, etc.
|
||||
The libraries provide syntactic sugar, but the built-in inheritance would
|
||||
be completely usable if it weren't for one small exception:
|
||||
it's very awkward to call <b>super</b>, the prototype object's
|
||||
implementation of the current function. CoffeeScript converts
|
||||
<tt>super()</tt> calls into calls against the immediate ancestor's
|
||||
method of the same name.
|
||||
</p>
|
||||
<div class='code'><pre class="idle"><span class="FunctionName">Animal</span><span class="Keyword">:</span> <span class="Storage">=></span> .
|
||||
<span class="FunctionName">Animal.prototype.move</span><span class="Keyword">:</span> <span class="FunctionArgument">meters</span> <span class="Storage">=></span>
|
||||
|
@ -631,18 +637,22 @@ let_the_wild_rumpus_begin() <span class="Keyword">unless</span> answer <span cla
|
|||
|
||||
<p id="switch">
|
||||
<b class="header">Switch/Case/Else</b>
|
||||
Switch statements in JavaScript are fundamentally broken. You can only
|
||||
do string comparisons, and need to break at the end of each case
|
||||
statment to prevent falling through to the default case. CoffeeScript
|
||||
compiles switch statements into if-else chains, allowing you to
|
||||
Switch statements in JavaScript are rather broken. You can only
|
||||
do string comparisons, and need to remember to <b>break</b> at the end of
|
||||
every <b>case</b> statement to avoid accidentally falling through to
|
||||
the default case. CoffeeScript
|
||||
compiles <b>switch</b> statements into JavaScript if-else chains, allowing you to
|
||||
compare any object (via <b>===</b>), preventing fall-through, and resulting
|
||||
in a returnable expression.
|
||||
in a returnable, assignable expression. To specify the default case, just
|
||||
use <tt>else</tt>.
|
||||
</p>
|
||||
<div class='code'><pre class="idle"><span class="Keyword">switch</span> day
|
||||
<span class="Keyword">case</span> <span class="String"><span class="String">"</span>Tuesday<span class="String">"</span></span> <span class="Keyword">then</span> eat_breakfast()
|
||||
<span class="Keyword">case</span> <span class="String"><span class="String">"</span>Wednesday<span class="String">"</span></span> <span class="Keyword">then</span> go_to_the_park()
|
||||
<span class="Keyword">case</span> <span class="String"><span class="String">"</span>Saturday<span class="String">"</span></span>
|
||||
<span class="Keyword">if</span> day <span class="Keyword">is</span> bingo_day <span class="Keyword">then</span> go_to_bingo().
|
||||
<span class="Keyword">if</span> day <span class="Keyword">is</span> bingo_day
|
||||
go_to_bingo()
|
||||
go_dancing().
|
||||
<span class="Keyword">case</span> <span class="String"><span class="String">"</span>Sunday<span class="String">"</span></span> <span class="Keyword">then</span> go_to_church()
|
||||
<span class="Keyword">else</span> go_to_work().
|
||||
</pre><pre class="idle"><span class="Keyword">if</span> (day <span class="Keyword">===</span> <span class="String"><span class="String">"</span>Tuesday<span class="String">"</span></span>) {
|
||||
|
@ -650,7 +660,10 @@ let_the_wild_rumpus_begin() <span class="Keyword">unless</span> answer <span cla
|
|||
} <span class="Keyword">else</span> <span class="Keyword">if</span> (day <span class="Keyword">===</span> <span class="String"><span class="String">"</span>Wednesday<span class="String">"</span></span>) {
|
||||
go_to_the_park();
|
||||
} <span class="Keyword">else</span> <span class="Keyword">if</span> (day <span class="Keyword">===</span> <span class="String"><span class="String">"</span>Saturday<span class="String">"</span></span>) {
|
||||
day <span class="Keyword">===</span> bingo_day ? go_to_bingo() : <span class="BuiltInConstant">null</span>;
|
||||
<span class="Keyword">if</span> (day <span class="Keyword">===</span> bingo_day) {
|
||||
go_to_bingo();
|
||||
go_dancing();
|
||||
}
|
||||
} <span class="Keyword">else</span> <span class="Keyword">if</span> (day <span class="Keyword">===</span> <span class="String"><span class="String">"</span>Sunday<span class="String">"</span></span>) {
|
||||
go_to_church();
|
||||
} <span class="Keyword">else</span> {
|
||||
|
|
Loading…
Reference in a new issue