810 lines
34 KiB
Plaintext
810 lines
34 KiB
Plaintext
<%
|
|
require 'uv'
|
|
def code_for(file, executable=false)
|
|
@stripper ||= /(\A\(function\(\)\{\n|\}\)\(\);\Z|^ )/
|
|
return '' unless File.exists?("documentation/js/#{file}.js")
|
|
cs = File.read("documentation/coffee/#{file}.coffee")
|
|
js = File.read("documentation/js/#{file}.js").gsub(@stripper, '')
|
|
cshtml = Uv.parse(cs, 'xhtml', 'coffeescript', false, 'idle', false)
|
|
jshtml = Uv.parse(js, 'xhtml', 'javascript', false, 'idle', false)
|
|
append = executable == true ? '' : "alert(#{executable});"
|
|
run = executable == true ? 'run' : "run: #{executable}"
|
|
button = executable ? "<button onclick='javascript: #{js};#{append}'>#{run}</button>" : ''
|
|
"<div class='code'>#{cshtml}#{jshtml}#{button}<br class='clear' /></div>"
|
|
end
|
|
%>
|
|
|
|
|
|
<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
|
|
<title>CoffeeScript</title>
|
|
<link rel="stylesheet" type="text/css" href="documentation/css/docs.css" />
|
|
<link rel="stylesheet" type="text/css" href="documentation/css/idle.css" />
|
|
</head>
|
|
<body>
|
|
|
|
<div class="container">
|
|
|
|
<h1><sub style="font-size: 100px;">☕</sub> CoffeeScript</h1>
|
|
|
|
<p>
|
|
CoffeeScript is a little language that compiles into JavaScript. Think
|
|
of it as JavaScript's less ostentatious kid brother — the same genes,
|
|
roughly the same height, but a different sense of style. Apart from a handful of
|
|
bonus goodies, statements in CoffeeScript correspond one-to-one with their
|
|
equivalent in JavaScript, it's just another way of saying it.
|
|
</p>
|
|
|
|
<p>
|
|
<b>Disclaimer:</b>
|
|
CoffeeScript is just for fun and seriously alpha. I'm sure that there are still
|
|
plenty of holes in the walls and leaks in the roof. <i>There are no guarantees
|
|
that the syntax won't change between versions.</i> That said,
|
|
it compiles into clean JavaScript (the good parts) that can use existing
|
|
JavaScript libraries seamlessly, and passes through
|
|
<a href="http://www.jslint.com/">JSLint</a> without warnings. The compiled
|
|
output is quite readable — pretty-printed, with comments
|
|
preserved intact.
|
|
</p>
|
|
|
|
<p>
|
|
<b>Latest Version:</b>
|
|
<a href="http://gemcutter.org/gems/coffee-script">0.2.6</a>
|
|
</p>
|
|
|
|
<h2>Table of Contents</h2>
|
|
|
|
<p>
|
|
<a href="#overview">Mini Overview</a><br />
|
|
<a href="#installation">Installation and Usage</a><br />
|
|
<a href="#whitespace">Significant Whitespace</a><br />
|
|
<a href="#functions">Functions and Invocation</a><br />
|
|
<a href="#assignment">Assignment</a><br />
|
|
<a href="#objects_and_arrays">Objects and Arrays</a><br />
|
|
<a href="#lexical_scope">Lexical Scoping and Variable Safety</a><br />
|
|
<a href="#conditionals">Conditionals, Ternaries, and Conditional Assignment</a><br />
|
|
<a href="#existence">The Existential Operator</a><br />
|
|
<a href="#aliases">Aliases</a><br />
|
|
<a href="#splats">Splats...</a><br />
|
|
<a href="#arguments">Arguments are Arrays</a><br />
|
|
<a href="#while">While Loops</a><br />
|
|
<a href="#comprehensions">Comprehensions (Arrays, Objects, and Ranges)</a><br />
|
|
<a href="#slice_splice">Array Slicing and Splicing with Ranges</a><br />
|
|
<a href="#expressions">Everything is an Expression</a><br />
|
|
<a href="#inheritance">Inheritance, and Calling Super from a Subclass</a><br />
|
|
<a href="#blocks">Blocks</a><br />
|
|
<a href="#pattern_matching">Pattern Matching</a><br />
|
|
<a href="#long_arrow">Function Binding</a><br />
|
|
<a href="#embedded">Embedded JavaScript</a><br />
|
|
<a href="#switch">Switch/When/Else</a><br />
|
|
<a href="#try">Try/Catch/Finally</a><br />
|
|
<a href="#comparisons">Chained Comparisons</a><br />
|
|
<a href="#strings">Multiline Strings and Heredocs</a><br />
|
|
<a href="#resources">Resources</a><br />
|
|
<a href="#contributing">Contributing</a><br />
|
|
<a href="#change_log">Change Log</a><br />
|
|
</p>
|
|
|
|
<h2 id="overview">Mini Overview</h2>
|
|
|
|
<p><i>CoffeeScript on the left, compiled JavaScript output on the right.</i></p>
|
|
|
|
<%= code_for('overview', 'cubed_list') %>
|
|
|
|
<p>
|
|
For a longer CoffeeScript example, check out
|
|
<a href="documentation/underscore.html">Underscore.coffee</a>, a port
|
|
of the <a href="http://documentcloud.github.com/underscore/">Underscore.js</a>
|
|
library of helper functions. Underscore.coffee can pass the entire Underscore.js
|
|
test suite. The CoffeeScript version is faster than the original for a number
|
|
of methods (in general, due to the speed of CoffeeScript's array comprehensions), and
|
|
after being minified and gzipped, is only 241 bytes larger than the original
|
|
JavaScript version.
|
|
Additional examples are included in the source repository, inside the
|
|
<a href="http://github.com/jashkenas/coffee-script/tree/master/examples/">examples</a> folder.
|
|
</p>
|
|
|
|
<h2 id="installation">Installation and Usage</h2>
|
|
|
|
<p>
|
|
The CoffeeScript compiler is written in pure Ruby, and is available
|
|
as a Ruby Gem.
|
|
</p>
|
|
|
|
<pre>
|
|
gem install coffee-script</pre>
|
|
|
|
<p>
|
|
Installing the gem provides the <tt>coffee</tt> command, which can
|
|
be used to compile CoffeeScript <tt>.coffee</tt> files into JavaScript, as
|
|
well as debug them. In conjunction with
|
|
<a href="http://narwhaljs.org/">Narwhal</a>, the <tt>coffee</tt>
|
|
command also provides direct evaluation and an interactive REPL.
|
|
When compiling to JavaScript, <tt>coffee</tt> writes the output
|
|
as <tt>.js</tt> files in the same directory by default, but output
|
|
can be customized with the following options:
|
|
</p>
|
|
|
|
<table>
|
|
<tr>
|
|
<td width="25%"><code>-i, --interactive</code></td>
|
|
<td>
|
|
Launch an interactive CoffeeScript session.
|
|
Requires <a href="http://narwhaljs.org/">Narwhal</a>.
|
|
</td>
|
|
</tr>
|
|
<tr>
|
|
<td><code>-r, --run</code></td>
|
|
<td>
|
|
Compile and execute scripts without saving the intermediate
|
|
JavaScript. Requires <a href="http://narwhaljs.org/">Narwhal</a>.
|
|
</td>
|
|
</tr>
|
|
<tr>
|
|
<td><code>-o, --output [DIR]</code></td>
|
|
<td>
|
|
Write out all compiled JavaScript files into the specified directory.
|
|
</td>
|
|
</tr>
|
|
<tr>
|
|
<td><code>-w, --watch</code></td>
|
|
<td>
|
|
Watch the modification times of the coffee-scripts, recompiling as
|
|
soon as a change occurs.
|
|
</td>
|
|
</tr>
|
|
<tr>
|
|
<td><code>-p, --print</code></td>
|
|
<td>
|
|
Instead of writing out the JavaScript as a file, print it
|
|
directly to <b>stdout</b>.
|
|
</td>
|
|
</tr>
|
|
<tr>
|
|
<td><code>-l, --lint</code></td>
|
|
<td>
|
|
If the <tt>jsl</tt> (JavaScript Lint) command is installed, use it
|
|
to check the compilation of a CoffeeScript file. (Handy in
|
|
conjunction with <tt>--watch</tt>)
|
|
</td>
|
|
</tr>
|
|
<tr>
|
|
<td><code>-e, --eval</code></td>
|
|
<td>
|
|
Compile and print a little snippet of CoffeeScript directly from the
|
|
command line (or from <b>stdin</b>). For example:<br /><tt>coffee -e "square: (x) => x * x"</tt>
|
|
</td>
|
|
</tr>
|
|
<tr>
|
|
<td><code>-t, --tokens</code></td>
|
|
<td>
|
|
Instead of parsing the CoffeeScript, just lex it, and print out the
|
|
token stream: <tt>[:IDENTIFIER, "square"], [":", ":"], [:PARAM, "x"]</tt> ...
|
|
</td>
|
|
</tr>
|
|
<tr>
|
|
<td><code>-v, --verbose</code></td>
|
|
<td>
|
|
As the JavaScript is being generated, print out every step of code
|
|
generation, including lexical scope and the node in the
|
|
AST.
|
|
</td>
|
|
</tr>
|
|
<tr>
|
|
<td><code>-n, --no-wrap</code></td>
|
|
<td>
|
|
Compile the JavaScript without the top-level function safety wrapper.
|
|
(Used for CoffeeScript as a Narwhal module.)
|
|
</td>
|
|
</tr>
|
|
<tr>
|
|
<td><code>-g, --globals</code></td>
|
|
<td>
|
|
Suppress all variable declarations at the top-level, effectively adding
|
|
those variables to the global scope. (Used by the REPL.)
|
|
</td>
|
|
</tr>
|
|
<tr>
|
|
<td><code>--install-bundle</code></td>
|
|
<td>
|
|
Install the TextMate bundle for CoffeeScript syntax highlighting.
|
|
</td>
|
|
</tr>
|
|
</table>
|
|
|
|
<p>
|
|
<b>Examples:</b>
|
|
</p>
|
|
|
|
<pre>
|
|
coffee path/to/script.coffee
|
|
coffee --interactive
|
|
coffee --watch --lint experimental.coffee
|
|
coffee --print app/scripts/*.coffee > concatenation.js</pre>
|
|
|
|
<h2>Language Reference</h2>
|
|
|
|
<p>
|
|
<i>
|
|
This reference is structured so that it can be read from top to bottom,
|
|
if you like. Later sections use ideas and syntax previously introduced.
|
|
Familiarity with JavaScript is assumed.
|
|
In all of the following examples, the source CoffeeScript is provided on
|
|
the left, and the direct compilation into JavaScript is on the right.
|
|
</i>
|
|
</p>
|
|
|
|
<p id="whitespace">
|
|
<b class="header">Significant Whitespace</b>
|
|
CoffeeScript uses Python-style significant whitespace: You don't need to
|
|
use semicolons <tt>;</tt> to terminate expressions, ending
|
|
the line will do just as well. Semicolons can still be used to fit
|
|
multiple expressions onto a single line. Instead of using curly braces
|
|
<tt>{ }</tt> to delimit blocks of code (like <a href="#functions">functions</a>,
|
|
<a href="#conditionals">if-statements</a>,
|
|
<a href="#switch">switch</a>, and <a href="#try">try/catch</a>),
|
|
use indentation.
|
|
</p>
|
|
|
|
<p>
|
|
You can use newlines to break up your expression into smaller pieces,
|
|
as long as CoffeeScript can determine that the line hasn't finished yet.
|
|
</p>
|
|
|
|
<p id="functions">
|
|
<b class="header">Functions and Invocation</b>
|
|
Functions are defined by a list of parameters, an arrow, and the
|
|
function body. The empty function looks like this: <tt>=></tt>. All
|
|
functions in CoffeeScript are named by default, for easier debugging.
|
|
</p>
|
|
<%= code_for('functions', 'cube(5)') %>
|
|
<p>
|
|
If you'd like to create an anonymous function, just wrap it in parentheses:
|
|
<tt>((x) => x * x)</tt>
|
|
</p>
|
|
|
|
<p id="assignment">
|
|
<b class="header">Assignment</b>
|
|
Use a colon <tt>:</tt> to assign, as in
|
|
<a href="http://json.org">JSON</a>. Equal signs are only needed for
|
|
mathy things.
|
|
</p>
|
|
<%= code_for('assignment', 'greeting') %>
|
|
<p>
|
|
Declaration of new variables are pushed up to the top of the nearest
|
|
lexical scope, so that assignment may always be performed within expressions.
|
|
</p>
|
|
|
|
<p id="objects_and_arrays">
|
|
<b class="header">Objects and Arrays</b>
|
|
Object and Array literals look very similar to their JavaScript cousins.
|
|
When you spread out each assignment on a separate line, the commas are
|
|
optional. In this way, assigning object properties looks the same as
|
|
assigning local variables, and can be moved around freely. Feel free to mix
|
|
and match the two styles.
|
|
</p>
|
|
<%= code_for('objects_and_arrays', 'song.join(",")') %>
|
|
|
|
<p id="lexical_scope">
|
|
<b class="header">Lexical Scoping and Variable Safety</b>
|
|
The CoffeeScript compiler takes care to make sure that all of your variables
|
|
are properly declared within lexical scope — you never need to write
|
|
<tt>var</tt> yourself.
|
|
</p>
|
|
<%= code_for('scope', 'new_num') %>
|
|
<p>
|
|
Notice how the all of the variable declarations have been pushed up to
|
|
the top of the closest scope, the first time they appear.
|
|
<b>num</b> is not redeclared within the inner function, because it's
|
|
already in scope; the <b>new_num</b> within the function, on the other hand,
|
|
should not be able to change the value of the external variable of the same name, and
|
|
therefore has a declaration of its own.
|
|
</p>
|
|
<p>
|
|
Although suppressed within this documentation for clarity, all
|
|
CoffeeScript output is wrapped in an anonymous function:
|
|
<tt>(function(){ ... })();</tt> This safety wrapper, combined with the
|
|
automatic generation of the <tt>var</tt> keyword, make it exceedingly difficult
|
|
to pollute the global namespace by accident.
|
|
</p>
|
|
<p>
|
|
If you'd like to create top-level variables for other scripts to use,
|
|
attach them as properties on <b>window</b>, or on the <b>exports</b>
|
|
object in CommonJS. The <b>existential operator</b> (below), gives you a
|
|
reliable way to figure out where to add them, if you're targeting both
|
|
CommonJS and the browser: <tt>root: exports ? this</tt>
|
|
</p>
|
|
|
|
<p id="conditionals">
|
|
<b class="header">Conditionals, Ternaries, and Conditional Assignment</b>
|
|
<b>If/else</b> statements can be written without the use of parentheses and
|
|
curly brackets. As with functions and other block expressions,
|
|
multi-line conditionals are delimited by indentation. There's also a handy
|
|
postfix form, with the <tt>if</tt> or <tt>unless</tt> at the end.
|
|
</p>
|
|
<p>
|
|
CoffeeScript will compile <b>if</b> statements using the ternary operator
|
|
when possible, to make it easier to use the result as an expression.
|
|
</p>
|
|
<%= code_for('conditionals') %>
|
|
<p>
|
|
The conditional assignment operators are included: <tt>||=</tt>,
|
|
which only assigns a value to a variable if the variable's current value
|
|
is falsy, and <tt>&&=</tt>, which only replaces the value of
|
|
truthy variables.
|
|
</p>
|
|
|
|
<p id="existence">
|
|
<b class="header">The Existential Operator</b>
|
|
It's a little difficult to check for the existence of a variable in
|
|
JavaScript. <tt>if (variable) ...</tt> comes close, but fails for zero,
|
|
the empty string, and false. The existential operator <tt>?</tt> returns true unless
|
|
a variable is <b>null</b> or <b>undefined</b>, which makes it analogous
|
|
to Ruby's <tt>nil?</tt>
|
|
</p>
|
|
<p>
|
|
It can also be used for safer conditional assignment than <tt>||=</tt>
|
|
provides, for cases where you may be handling numbers or strings.
|
|
</p>
|
|
<%= code_for('existence', 'speed') %>
|
|
|
|
<p id="aliases">
|
|
<b class="header">Aliases</b>
|
|
Because the <tt>==</tt> operator frequently causes undesirable coercion,
|
|
is intransitive, and has a different meaning than in other languages,
|
|
CoffeeScript compiles <tt>==</tt> into <tt>===</tt>, and <tt>!=</tt> into
|
|
<tt>!==</tt>.
|
|
In addition, <tt>is</tt> compiles into <tt>===</tt>,
|
|
and <tt>isnt</tt> into <tt>!==</tt>.
|
|
</p>
|
|
<p>
|
|
You can use <tt>not</tt> as an alias for <tt>!</tt>.
|
|
</p>
|
|
<p>
|
|
For logic, <tt>and</tt> compiles to <tt>&&</tt>, and <tt>or</tt>
|
|
into <tt>||</tt>.
|
|
</p>
|
|
<p>
|
|
Instead of a newline or semicolon, <tt>then</tt> can be used to separate
|
|
conditions from expressions, in <b>while</b>,
|
|
<b>if</b>/<b>else</b>, and <b>switch</b>/<b>when</b> statements.
|
|
</p>
|
|
<p>
|
|
As in <a href="http://yaml.org/">YAML</a>, <tt>on</tt> and <tt>yes</tt>
|
|
are the same as boolean <tt>true</tt>, while <tt>off</tt> and <tt>no</tt> are boolean <tt>false</tt>.
|
|
</p>
|
|
<p>
|
|
For single-line statements, <tt>unless</tt> can be used as the inverse of <tt>if</tt>.
|
|
</p>
|
|
<%= code_for('aliases') %>
|
|
|
|
<p id="splats">
|
|
<b class="header">Splats...</b>
|
|
The JavaScript <b>arguments object</b> is a useful way to work with
|
|
functions that accept variable numbers of arguments. CoffeeScript provides
|
|
splats <tt>...</tt>, both for function definition as well as invocation,
|
|
making variable numbers of arguments a little bit more palatable.
|
|
</p>
|
|
<%= code_for('splats', true) %>
|
|
|
|
<p id="arguments">
|
|
<b class="header">Arguments are Arrays</b>
|
|
If you reference the <b>arguments object</b> directly, it will be converted
|
|
into a real Array, making all of the
|
|
<a href="https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Objects/Array">Array methods</a>
|
|
available.
|
|
</p>
|
|
<%= code_for('arguments', true) %>
|
|
|
|
<p id="while">
|
|
<b class="header">While Loops</b>
|
|
The only low-level loop that CoffeeScript provides is the <b>while</b> loop. The
|
|
main difference from JavaScript is that the <b>while</b> loop can be used
|
|
as an expression, returning an array containing the result of each iteration
|
|
through the loop.
|
|
</p>
|
|
<%= code_for('while', 'lyrics.join("\n")') %>
|
|
<p>
|
|
Other JavaScript loops, such as <b>for</b> loops and <b>do-while</b> loops
|
|
can be mimicked by variations on <b>while</b>, but the hope is that you
|
|
won't need to do that with CoffeeScript, either because you're using
|
|
<b>each</b> (<b>forEach</b>) style iterators, or...
|
|
</p>
|
|
|
|
<p id="comprehensions">
|
|
<b class="header">Comprehensions (Arrays, Objects, and Ranges)</b>
|
|
For your looping needs, CoffeeScript provides array comprehensions
|
|
similar to Python's. They replace (and compile into) <b>for</b> loops, with
|
|
optional guard clauses and the value of the current array index.
|
|
Unlike for loops, array comprehensions are expressions, and can be returned
|
|
and assigned. They should be able to handle most places where you otherwise
|
|
would use a loop, <b>each</b>/<b>forEach</b>, <b>map</b>, or <b>select</b>/<b>filter</b>.
|
|
</p>
|
|
<%= code_for('array_comprehensions') %>
|
|
<p>
|
|
If you know the start and end of your loop, or would like to step through
|
|
in fixed-size increments, you can use a range to specify the start and
|
|
end of your comprehension. (The long line-breaking "for" definitions in
|
|
the compiled JS below allow ranges to count downwards, as well as upwards).
|
|
</p>
|
|
<%= code_for('range_comprehensions', 'countdown') %>
|
|
<p>
|
|
Comprehensions can also be used to iterate over the keys and values in
|
|
an object. Use <tt>of</tt> to signal comprehension over the properties of
|
|
an object instead of the values in an array.
|
|
</p>
|
|
<%= code_for('object_comprehensions', 'ages.join(", ")') %>
|
|
|
|
<p id="slice_splice">
|
|
<b class="header">Array Slicing and Splicing with Ranges</b>
|
|
CoffeeScript borrows Ruby's
|
|
<a href="http://ruby-doc.org/core/classes/Range.html">range syntax</a>
|
|
for extracting slices of arrays. With two dots (<tt>3..5</tt>), the range
|
|
is inclusive: the first argument is the index of the first element in
|
|
the slice, and the second is the index of the last one. Three dots signify
|
|
a range that excludes the end.
|
|
</p>
|
|
<%= code_for('slices', 'numbers_copy') %>
|
|
<p>
|
|
The same syntax can be used with assignment to replace a segment of an
|
|
array with new values (to splice it).
|
|
</p>
|
|
<%= code_for('splices', 'numbers') %>
|
|
|
|
<p id="expressions">
|
|
<b class="header">Everything is an Expression (at least, as much as possible)</b>
|
|
You might have noticed how even though we don't add return statements
|
|
to CoffeeScript functions, they nonetheless return their final value.
|
|
The CoffeeScript compiler tries to make sure that all statements in the
|
|
language can be used as expressions. Watch how the <tt>return</tt> gets
|
|
pushed down into each possible branch of execution, in the function
|
|
below.
|
|
</p>
|
|
<%= code_for('expressions', 'eldest') %>
|
|
<p>
|
|
Even though functions will always return their final value, it's both possible
|
|
and encouraged to return early from a function body writing out the explicit
|
|
return (<tt>return value</tt>), when you know that you're done.
|
|
</p>
|
|
<p>
|
|
Because variable declarations occur at the top of scope, assignment can
|
|
be used within expressions, even for variables that haven't been seen before:
|
|
</p>
|
|
<%= code_for('expressions_assignment', 'six') %>
|
|
<p>
|
|
Things that would otherwise be statements in JavaScript, when used
|
|
as part of an expression in CoffeeScript, are converted into expressions
|
|
by wrapping them in a closure. This lets you do useful things, like assign
|
|
the result of a comprehension to a variable:
|
|
</p>
|
|
<%= code_for('expressions_comprehension', 'globals') %>
|
|
<p>
|
|
As well as silly things, like passing a <b>try/catch</b> statement directly
|
|
into a function call:
|
|
</p>
|
|
<%= code_for('expressions_try', true) %>
|
|
<p>
|
|
There are a handful of statements in JavaScript that can't be meaningfully
|
|
converted into expressions, namely <tt>break</tt>, <tt>continue</tt>,
|
|
and <tt>return</tt>. If you make use of them within a block of code,
|
|
CoffeeScript won't try to perform the conversion.
|
|
</p>
|
|
|
|
<p id="inheritance">
|
|
<b class="header">Inheritance, and Calling Super from a Subclass</b>
|
|
JavaScript's prototypal inheritance has always been a bit of a
|
|
brain-bender, with a whole family tree of libraries that provide a cleaner
|
|
syntax for classical inheritance on top of JavaScript's prototypes:
|
|
<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 a couple of small exceptions:
|
|
it's awkward to call <b>super</b> (the prototype object's
|
|
implementation of the current function), and it's awkward to correctly
|
|
set the prototype chain.
|
|
</p>
|
|
<p>
|
|
CoffeeScript provides <tt>extends</tt>
|
|
to help with prototype setup, <tt>::</tt> for quick access to an
|
|
object's prototype, and converts <tt>super()</tt> into a call against
|
|
the immediate ancestor's method of the same name.
|
|
</p>
|
|
<%= code_for('super', true) %>
|
|
|
|
<p id="blocks">
|
|
<b class="header">Blocks</b>
|
|
Many common looping functions (in Prototype, jQuery, and Underscore,
|
|
for example) take a single function as their final argument. To make
|
|
final functions easier to pass, CoffeeScript includes block syntax,
|
|
so you don't have to close the parentheses on the other side.
|
|
</p>
|
|
<%= code_for('blocks') %>
|
|
|
|
<p id="pattern_matching">
|
|
<b class="header">Pattern Matching (Destructuring Assignment)</b>
|
|
To make extracting values from complex arrays and objects more convenient,
|
|
CoffeeScript implements ECMAScript Harmony's proposed
|
|
<a href="http://wiki.ecmascript.org/doku.php?id=harmony:destructuring">destructuring assignment</a>
|
|
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
|
|
used for parallel assignment:
|
|
</p>
|
|
<%= code_for('parallel_assignment', 'bait') %>
|
|
<p>
|
|
But it's also helpful for dealing with functions that return multiple
|
|
values.
|
|
</p>
|
|
<%= code_for('multiple_return_values', 'forecast') %>
|
|
<p>
|
|
Pattern matching can be used with any depth of array and object nesting,
|
|
to help pull out deeply nested properties.
|
|
</p>
|
|
<%= code_for('object_extraction', 'poet + " — " + street') %>
|
|
|
|
<p id="long_arrow">
|
|
<b class="header">Function binding</b>
|
|
The long arrow <tt>==></tt> can be used to both define a function, and to bind
|
|
it to the current value of <tt>this</tt>, right on the spot. This is helpful
|
|
when using callback-based libraries like Prototype or jQuery, for creating
|
|
iterator functions to pass to <tt>each</tt>, or event-handler functions
|
|
to use with <tt>bind</tt>. Functions created with the long arrow are able to access
|
|
properties of the <tt>this</tt> where they're defined.
|
|
</p>
|
|
<%= code_for('long_arrow') %>
|
|
|
|
<p id="embedded">
|
|
<b class="header">Embedded JavaScript</b>
|
|
Hopefully, you'll never need to use it, but if you ever need to intersperse
|
|
snippets of JavaScript within your CoffeeScript, you can
|
|
use backticks to pass it straight through.
|
|
</p>
|
|
<%= code_for('embedded', 'hi()') %>
|
|
|
|
<p id="switch">
|
|
<b class="header">Switch/When/Else</b>
|
|
<b>Switch</b> statements in JavaScript are rather broken. You can only
|
|
do comparisons based on string equality, 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, assignable expression. The format is: <tt>switch</tt> condition,
|
|
<tt>when</tt> clauses, <tt>else</tt> the default case.
|
|
</p>
|
|
<p>
|
|
As in Ruby, <b>switch</b> statements in CoffeeScript can take multiple
|
|
values for each <b>when</b> clause. If any of the values match, the clause
|
|
runs.
|
|
</p>
|
|
<%= code_for('switch') %>
|
|
|
|
<p id="try">
|
|
<b class="header">Try/Catch/Finally</b>
|
|
Try/catch statements are just about the same as JavaScript (although
|
|
they work as expressions).
|
|
</p>
|
|
<%= code_for('try') %>
|
|
|
|
<p id="comparisons">
|
|
<b class="header">Chained Comparisons</b>
|
|
CoffeeScript borrows
|
|
<a href="http://docs.python.org/reference/expressions.html#notin">chained comparisons</a>
|
|
from Python — making it easy to test if a value falls within a
|
|
certain range.
|
|
</p>
|
|
<%= code_for('comparisons', 'healthy') %>
|
|
|
|
<p id="strings">
|
|
<b class="header">Multiline Strings and Heredocs</b>
|
|
Multiline strings are allowed in CoffeeScript.
|
|
</p>
|
|
<%= code_for('strings', 'moby_dick') %>
|
|
<p>
|
|
Heredocs can be used to hold formatted or indentation-sensitive text
|
|
(or, if you just don't feel like escaping quotes and apostrophes). The
|
|
indentation level that begins the heredoc is maintained throughout, so
|
|
you can keep it all aligned with the body of your code.
|
|
</p>
|
|
<%= code_for('heredocs') %>
|
|
|
|
<h2 id="resources">Resources</h2>
|
|
|
|
<ul>
|
|
<li>
|
|
<a href="http://github.com/jashkenas/coffee-script/">Source Code</a><br />
|
|
After checking out the source, make sure to run <tt>rake build:parser</tt>
|
|
to generate an up-to-date version of the Racc parser.
|
|
Use <tt>bin/coffee</tt> to test your changes,
|
|
<tt>rake test</tt> to run the test suite,
|
|
and <tt>rake gem:install</tt> to
|
|
create and install a custom version of the gem.
|
|
</li>
|
|
<li>
|
|
<a href="http://github.com/jashkenas/coffee-script/issues">Bugs, Feature Requests, and General Discussion</a>
|
|
</li>
|
|
<li>
|
|
<a href="http://github.com/jnicklas/bistro_car">BistroCar</a><br />
|
|
A Rails plugin by
|
|
<a href="http://github.com/jnicklas">Jonas Nicklas</a>
|
|
that includes CoffeeScript helpers,
|
|
bundling and minification.
|
|
</li>
|
|
<li>
|
|
<a href="http://github.com/inem/coffee-haml-filter">coffee-haml-filter</a><br />
|
|
A custom <a href="http://haml-lang.com/">HAML</a> filter, by
|
|
<a href="http://github.com/inem">Ivan Nemytchenko</a>, that embeds
|
|
snippets of CoffeeScript within your HAML templates.
|
|
</li>
|
|
</ul>
|
|
|
|
<h2 id="contributing">Contributing</h2>
|
|
|
|
<p>
|
|
Here's a wish list of things that would be wonderful to have contributed:
|
|
</p>
|
|
|
|
<ul>
|
|
<li>
|
|
<a href="http://github.com/jashkenas/coffee-script/issues#issue/8">
|
|
A CoffeeScript version of the compiler.
|
|
</a>
|
|
</li>
|
|
<li>
|
|
Test cases for any syntax errors you encounter that you think CoffeeScript
|
|
should be able to compile properly.
|
|
</li>
|
|
<li>
|
|
A tutorial that introduces CoffeeScript from the ground up for folks
|
|
without knowledge of JavaScript.
|
|
</li>
|
|
<li>
|
|
Integration with Processing.js's JavaScript API (this would depend on
|
|
having a JavaScript version of the compiler).
|
|
</li>
|
|
<li>
|
|
A lot of the code generation in <tt>nodes.rb</tt> gets into messy
|
|
string manipulation. Techniques for cleaning this up across the board
|
|
would be appreciated.
|
|
</li>
|
|
</ul>
|
|
|
|
<h2 id="change_log">Change Log</h2>
|
|
|
|
<p>
|
|
<b class="header" style="margin-top: 20px;">0.2.6</b>
|
|
Added Python-style chained comparisons, the conditional existence
|
|
operator <tt>?=</tt>, and some examples from <i>Beautiful Code</i>.
|
|
Bugfixes relating to statement-to-expression conversion, arguments-to-array
|
|
conversion, and the TextMate syntax highlighter.
|
|
</p>
|
|
|
|
<p>
|
|
<b class="header" style="margin-top: 20px;">0.2.5</b>
|
|
The conditions in switch statements can now take multiple values at once —
|
|
If any of them are true, the case will run. Added the long arrow <tt>==></tt>,
|
|
which defines and immediately binds a function to <tt>this</tt>. While loops can
|
|
now be used as expressions, in the same way that comprehensions can. Splats
|
|
can be used within pattern matches to soak up the rest of an array.
|
|
</p>
|
|
|
|
<p>
|
|
<b class="header" style="margin-top: 20px;">0.2.4</b>
|
|
Added ECMAScript Harmony style destructuring assignment, for dealing with
|
|
extracting values from nested arrays and objects. Added indentation-sensitive
|
|
heredocs for nicely formatted strings or chunks of code.
|
|
</p>
|
|
|
|
<p>
|
|
<b class="header" style="margin-top: 20px;">0.2.3</b>
|
|
Axed the unsatisfactory <tt>ino</tt> keyword, replacing it with <tt>of</tt> for
|
|
object comprehensions. They now look like: <tt>for prop, value of object</tt>.
|
|
</p>
|
|
|
|
<p>
|
|
<b class="header" style="margin-top: 20px;">0.2.2</b>
|
|
When performing a comprehension over an object, use <tt>ino</tt>, instead
|
|
of <tt>in</tt>, which helps us generate smaller, more efficient code at
|
|
compile time.
|
|
<br />
|
|
Added <tt>::</tt> as a shorthand for saying <tt>.prototype.</tt>
|
|
<br />
|
|
The "splat" symbol has been changed from a prefix asterisk <tt>*</tt>, to
|
|
a postfix ellipsis <tt>...</tt>
|
|
<br />
|
|
Added JavaScript's <tt>in</tt> operator,
|
|
empty <tt>return</tt> statements, and empty <tt>while</tt> loops.
|
|
<br />
|
|
Constructor functions that start with capital letters now include a
|
|
safety check to make sure that the new instance of the object is returned.
|
|
<br />
|
|
The <tt>extends</tt> keyword now functions identically to <tt>goog.inherits</tt>
|
|
in Google's Closure Library.
|
|
</p>
|
|
|
|
<p>
|
|
<b class="header" style="margin-top: 20px;">0.2.1</b>
|
|
Arguments objects are now converted into real arrays when referenced.
|
|
</p>
|
|
|
|
<p>
|
|
<b class="header" style="margin-top: 20px;">0.2.0</b>
|
|
Major release. Significant whitespace. Better statement-to-expression
|
|
conversion. Splats. Splice literals. Object comprehensions. Blocks.
|
|
The existential operator. Many thanks to all the folks who posted issues,
|
|
with special thanks to
|
|
<a href="http://github.com/kamatsu">Liam O'Connor-Davis</a> for whitespace
|
|
and expression help.
|
|
</p>
|
|
|
|
<p>
|
|
<b class="header" style="margin-top: 20px;">0.1.6</b>
|
|
Bugfix for running <tt>coffee --interactive</tt> and <tt>--run</tt>
|
|
from outside of the CoffeeScript directory. Bugfix for nested
|
|
function/if-statements.
|
|
</p>
|
|
|
|
<p>
|
|
<b class="header" style="margin-top: 20px;">0.1.5</b>
|
|
Array slice literals and array comprehensions can now both take Ruby-style
|
|
ranges to specify the start and end. JavaScript variable declaration is
|
|
now pushed up to the top of the scope, making all assignment statements into
|
|
expressions. You can use <tt>\</tt> to escape newlines.
|
|
The <tt>coffee-script</tt> command is now called <tt>coffee</tt>.
|
|
</p>
|
|
|
|
<p>
|
|
<b class="header" style="margin-top: 20px;">0.1.4</b>
|
|
The official CoffeeScript extension is now <tt>.coffee</tt> instead of
|
|
<tt>.cs</tt>, which properly belongs to
|
|
<a href="http://en.wikipedia.org/wiki/C_Sharp_(programming_language)">C#</a>.
|
|
Due to popular demand, you can now also use <tt>=</tt> to assign. Unlike
|
|
JavaScript, <tt>=</tt> can also be used within object literals, interchangeably
|
|
with <tt>:</tt>. Made a grammatical fix for chained function calls
|
|
like <tt>func(1)(2)(3)(4)</tt>. Inheritance and super no longer use
|
|
<tt>__proto__</tt>, so they should be IE-compatible now.
|
|
</p>
|
|
|
|
<p>
|
|
<b class="header" style="margin-top: 20px;">0.1.3</b>
|
|
The <tt>coffee</tt> command now includes <tt>--interactive</tt>,
|
|
which launches an interactive CoffeeScript session, and <tt>--run</tt>,
|
|
which directly compiles and executes a script. Both options depend on a
|
|
working installation of Narwhal.
|
|
The <tt>aint</tt> keyword has been replaced by <tt>isnt</tt>, which goes
|
|
together a little smoother with <tt>is</tt>.
|
|
Quoted strings are now allowed as identifiers within object literals: eg.
|
|
<tt>{"5+5": 10}</tt>.
|
|
All assignment operators now use a colon: <tt>+:</tt>, <tt>-:</tt>,
|
|
<tt>*:</tt>, etc.
|
|
</p>
|
|
|
|
<p>
|
|
<b class="header" style="margin-top: 20px;">0.1.2</b>
|
|
Fixed a bug with calling <tt>super()</tt> through more than one level of
|
|
inheritance, with the re-addition of the <tt>extends</tt> keyword.
|
|
Added experimental <a href="http://narwhaljs.org/">Narwhal</a>
|
|
support (as a Tusk package), contributed by
|
|
<a href="http://tlrobinson.net/">Tom Robinson</a>, including
|
|
<b>bin/cs</b> as a CoffeeScript REPL and interpreter.
|
|
New <tt>--no-wrap</tt> option to suppress the safety function
|
|
wrapper.
|
|
</p>
|
|
|
|
<p>
|
|
<b class="header" style="margin-top: 20px;">0.1.1</b>
|
|
Added <tt>instanceof</tt> and <tt>typeof</tt> as operators.
|
|
</p>
|
|
|
|
<p>
|
|
<b class="header" style="margin-top: 20px;">0.1.0</b>
|
|
Initial CoffeeScript release.
|
|
</p>
|
|
|
|
</div>
|
|
|
|
</body>
|
|
</html>
|