2009-12-21 11:41:45 -05:00
|
|
|
<%
|
|
|
|
require 'uv'
|
|
|
|
def code_for(file, executable=false)
|
|
|
|
@stripper ||= /(\A\(function\(\)\{\n|\}\)\(\);\Z|^ )/
|
2009-12-23 19:42:18 -05:00
|
|
|
return '' unless File.exists?("documentation/js/#{file}.js")
|
2009-12-25 14:18:05 -08:00
|
|
|
cs = File.read("documentation/coffee/#{file}.coffee")
|
2009-12-21 11:41:45 -05:00
|
|
|
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
|
|
|
|
%>
|
|
|
|
|
|
|
|
|
2009-12-19 22:55:58 -05:00
|
|
|
<!DOCTYPE html>
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
|
2009-12-23 20:24:55 -05:00
|
|
|
<title>CoffeeScript</title>
|
2009-12-21 11:41:45 -05:00
|
|
|
<link rel="stylesheet" type="text/css" href="documentation/css/docs.css" />
|
|
|
|
<link rel="stylesheet" type="text/css" href="documentation/css/idle.css" />
|
2009-12-19 22:55:58 -05:00
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
|
|
|
|
<div class="container">
|
|
|
|
|
2009-12-21 11:41:45 -05:00
|
|
|
<h1><sub style="font-size: 100px;">☕</sub> CoffeeScript</h1>
|
|
|
|
|
2009-12-19 22:55:58 -05:00
|
|
|
<p>
|
|
|
|
CoffeeScript is a little language that compiles into JavaScript. Think
|
2009-12-23 19:42:18 -05:00
|
|
|
of it as JavaScript's less ostentatious kid brother — the same genes,
|
2009-12-24 00:12:07 -08:00
|
|
|
roughly the same height, but a different sense of style. Apart from a handful of
|
2009-12-23 20:24:55 -05:00
|
|
|
bonus goodies, statements in CoffeeScript correspond one-to-one with their
|
2009-12-23 19:42:18 -05:00
|
|
|
equivalent in JavaScript, it's just another way of saying it.
|
2009-12-21 11:41:45 -05:00
|
|
|
</p>
|
|
|
|
|
|
|
|
<p>
|
2009-12-22 12:18:27 -05:00
|
|
|
<b>Disclaimer:</b>
|
2009-12-24 01:22:41 -05:00
|
|
|
CoffeeScript is just for fun and seriously alpha. I'm sure that there are still
|
2010-01-10 17:36:06 -05:00
|
|
|
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,
|
2009-12-23 19:42:18 -05:00
|
|
|
it compiles into clean JavaScript (the good parts) that can use existing
|
2009-12-23 23:01:39 -08:00
|
|
|
JavaScript libraries seamlessly, and passes through
|
2009-12-23 19:42:18 -05:00
|
|
|
<a href="http://www.jslint.com/">JSLint</a> without warnings. The compiled
|
2009-12-23 20:57:35 -05:00
|
|
|
output is quite readable — pretty-printed, with comments
|
2009-12-23 19:42:18 -05:00
|
|
|
preserved intact.
|
2009-12-21 11:41:45 -05:00
|
|
|
</p>
|
2010-01-04 22:19:45 -05:00
|
|
|
|
2010-01-04 19:15:24 -05:00
|
|
|
<p>
|
|
|
|
<b>Latest Version:</b>
|
2010-02-08 10:58:49 -05:00
|
|
|
<a href="http://gemcutter.org/gems/coffee-script">0.3.2</a>
|
2010-01-04 19:15:24 -05:00
|
|
|
</p>
|
2009-12-21 11:41:45 -05:00
|
|
|
|
|
|
|
<h2>Table of Contents</h2>
|
|
|
|
|
|
|
|
<p>
|
2009-12-23 19:42:18 -05:00
|
|
|
<a href="#overview">Mini Overview</a><br />
|
|
|
|
<a href="#installation">Installation and Usage</a><br />
|
2010-02-14 15:41:08 -05:00
|
|
|
<a href="#try_coffee">Try CoffeeScript</a><br />
|
2010-01-04 22:19:45 -05:00
|
|
|
<a href="#whitespace">Significant Whitespace</a><br />
|
2009-12-21 11:41:45 -05:00
|
|
|
<a href="#functions">Functions and Invocation</a><br />
|
|
|
|
<a href="#assignment">Assignment</a><br />
|
2009-12-23 19:42:18 -05:00
|
|
|
<a href="#objects_and_arrays">Objects and Arrays</a><br />
|
2009-12-21 11:41:45 -05:00
|
|
|
<a href="#lexical_scope">Lexical Scoping and Variable Safety</a><br />
|
|
|
|
<a href="#conditionals">Conditionals, Ternaries, and Conditional Assignment</a><br />
|
2009-12-23 23:01:39 -08:00
|
|
|
<a href="#aliases">Aliases</a><br />
|
2010-01-05 21:40:36 -05:00
|
|
|
<a href="#splats">Splats...</a><br />
|
2010-01-05 09:29:10 -05:00
|
|
|
<a href="#arguments">Arguments are Arrays</a><br />
|
2009-12-21 11:41:45 -05:00
|
|
|
<a href="#while">While Loops</a><br />
|
2010-01-04 22:19:45 -05:00
|
|
|
<a href="#comprehensions">Comprehensions (Arrays, Objects, and Ranges)</a><br />
|
|
|
|
<a href="#slice_splice">Array Slicing and Splicing with Ranges</a><br />
|
2010-01-04 22:51:02 -05:00
|
|
|
<a href="#expressions">Everything is an Expression</a><br />
|
2010-01-26 23:23:59 -05:00
|
|
|
<a href="#existence">The Existential Operator</a><br />
|
2009-12-24 16:49:23 -08:00
|
|
|
<a href="#inheritance">Inheritance, and Calling Super from a Subclass</a><br />
|
2010-01-11 22:55:01 -05:00
|
|
|
<a href="#pattern_matching">Pattern Matching</a><br />
|
2010-01-26 23:23:59 -05:00
|
|
|
<a href="#fat_arrow">Function Binding</a><br />
|
2009-12-21 11:41:45 -05:00
|
|
|
<a href="#embedded">Embedded JavaScript</a><br />
|
2009-12-24 01:33:59 -08:00
|
|
|
<a href="#switch">Switch/When/Else</a><br />
|
2009-12-21 11:41:45 -05:00
|
|
|
<a href="#try">Try/Catch/Finally</a><br />
|
2010-01-17 18:12:59 -05:00
|
|
|
<a href="#comparisons">Chained Comparisons</a><br />
|
2010-01-12 00:00:38 -05:00
|
|
|
<a href="#strings">Multiline Strings and Heredocs</a><br />
|
2009-12-26 09:05:57 -08:00
|
|
|
<a href="#resources">Resources</a><br />
|
2009-12-24 00:12:07 -08:00
|
|
|
<a href="#change_log">Change Log</a><br />
|
2009-12-19 22:55:58 -05:00
|
|
|
</p>
|
2009-12-31 13:01:10 -05:00
|
|
|
|
2009-12-23 19:42:18 -05:00
|
|
|
<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') %>
|
2010-01-04 23:26:27 -05:00
|
|
|
|
2010-01-04 22:51:02 -05:00
|
|
|
<p>
|
2010-01-04 23:26:27 -05:00
|
|
|
For a longer CoffeeScript example, check out
|
2010-01-04 22:51:02 -05:00
|
|
|
<a href="documentation/underscore.html">Underscore.coffee</a>, a port
|
2010-01-10 17:14:20 -05:00
|
|
|
of the <a href="http://documentcloud.github.com/underscore/">Underscore.js</a>
|
|
|
|
library of helper functions. Underscore.coffee can pass the entire Underscore.js
|
2010-01-11 22:55:01 -05:00
|
|
|
test suite. The CoffeeScript version is faster than the original for a number
|
2010-01-10 17:14:20 -05:00
|
|
|
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
|
2010-01-05 00:45:04 -05:00
|
|
|
<a href="http://github.com/jashkenas/coffee-script/tree/master/examples/">examples</a> folder.
|
2010-01-04 22:51:02 -05:00
|
|
|
</p>
|
2009-12-23 19:42:18 -05:00
|
|
|
|
|
|
|
<h2 id="installation">Installation and Usage</h2>
|
2009-12-24 22:29:30 -08:00
|
|
|
|
2009-12-24 00:12:07 -08:00
|
|
|
<p>
|
|
|
|
The CoffeeScript compiler is written in pure Ruby, and is available
|
|
|
|
as a Ruby Gem.
|
|
|
|
</p>
|
2009-12-23 19:42:18 -05:00
|
|
|
|
|
|
|
<pre>
|
2009-12-24 00:12:07 -08:00
|
|
|
gem install coffee-script</pre>
|
2009-12-23 19:42:18 -05:00
|
|
|
|
2009-12-19 22:56:27 -05:00
|
|
|
<p>
|
2009-12-26 08:57:13 -08:00
|
|
|
Installing the gem provides the <tt>coffee</tt> command, which can
|
2009-12-25 14:18:05 -08:00
|
|
|
be used to compile CoffeeScript <tt>.coffee</tt> files into JavaScript, as
|
|
|
|
well as debug them. In conjunction with
|
2010-02-08 10:58:49 -05:00
|
|
|
<a href="http://nodejs.org/">Node.js</a> (or <a href="http://narwhaljs.org/">Narwhal</a>), the <tt>coffee</tt>
|
2009-12-25 14:18:05 -08:00
|
|
|
command also provides direct evaluation and an interactive REPL.
|
2009-12-26 08:57:13 -08:00
|
|
|
When compiling to JavaScript, <tt>coffee</tt> writes the output
|
2009-12-25 00:16:56 -08:00
|
|
|
as <tt>.js</tt> files in the same directory by default, but output
|
2009-12-23 19:42:18 -05:00
|
|
|
can be customized with the following options:
|
|
|
|
</p>
|
|
|
|
|
|
|
|
<table>
|
|
|
|
<tr>
|
2009-12-25 00:16:56 -08:00
|
|
|
<td width="25%"><code>-i, --interactive</code></td>
|
|
|
|
<td>
|
2009-12-25 14:18:05 -08:00
|
|
|
Launch an interactive CoffeeScript session.
|
2010-02-08 10:58:49 -05:00
|
|
|
Requires <a href="http://nodejs.org/">Node.js</a>,
|
|
|
|
or <a href="http://narwhaljs.org/">Narwhal</a>, with <tt>--narwhal</tt>.
|
2009-12-25 00:16:56 -08:00
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td><code>-r, --run</code></td>
|
|
|
|
<td>
|
2010-01-04 23:26:27 -05:00
|
|
|
Compile and execute scripts without saving the intermediate
|
2010-02-08 10:58:49 -05:00
|
|
|
JavaScript. Requires <a href="http://nodejs.org/">Node.js</a>,
|
|
|
|
or <a href="http://narwhaljs.org/">Narwhal</a>, with <tt>--narwhal</tt>.
|
2009-12-25 00:16:56 -08:00
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td><code>-o, --output [DIR]</code></td>
|
2009-12-23 19:42:18 -05:00
|
|
|
<td>
|
|
|
|
Write out all compiled JavaScript files into the specified directory.
|
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td><code>-w, --watch</code></td>
|
|
|
|
<td>
|
2009-12-23 23:01:39 -08:00
|
|
|
Watch the modification times of the coffee-scripts, recompiling as
|
2009-12-23 19:42:18 -05:00
|
|
|
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
|
2009-12-24 22:29:30 -08:00
|
|
|
to check the compilation of a CoffeeScript file. (Handy in
|
2009-12-23 23:01:39 -08:00
|
|
|
conjunction with <tt>--watch</tt>)
|
2009-12-23 19:42:18 -05:00
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td><code>-e, --eval</code></td>
|
|
|
|
<td>
|
|
|
|
Compile and print a little snippet of CoffeeScript directly from the
|
2010-01-26 10:52:05 -05:00
|
|
|
command line (or from <b>stdin</b>). For example:<br /><tt>coffee -e "square: (x) -> x * x"</tt>
|
2009-12-23 19:42:18 -05:00
|
|
|
</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
|
2010-01-26 23:23:59 -05:00
|
|
|
generation, including lexical scope and the nodes in the
|
2009-12-23 19:42:18 -05:00
|
|
|
AST.
|
|
|
|
</td>
|
|
|
|
</tr>
|
2009-12-24 17:37:24 -08:00
|
|
|
<tr>
|
|
|
|
<td><code>-n, --no-wrap</code></td>
|
|
|
|
<td>
|
2010-01-10 17:14:20 -05:00
|
|
|
Compile the JavaScript without the top-level function safety wrapper.
|
2010-01-29 23:30:54 -05:00
|
|
|
(Used for CoffeeScript as a Node.js module.)
|
2010-01-10 17:14:20 -05:00
|
|
|
</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.)
|
2009-12-24 17:37:24 -08:00
|
|
|
</td>
|
|
|
|
</tr>
|
2009-12-23 23:01:39 -08:00
|
|
|
<tr>
|
|
|
|
<td><code>--install-bundle</code></td>
|
|
|
|
<td>
|
|
|
|
Install the TextMate bundle for CoffeeScript syntax highlighting.
|
|
|
|
</td>
|
|
|
|
</tr>
|
2009-12-23 19:42:18 -05:00
|
|
|
</table>
|
|
|
|
|
|
|
|
<p>
|
|
|
|
<b>Examples:</b>
|
|
|
|
</p>
|
|
|
|
|
|
|
|
<pre>
|
2009-12-26 08:57:13 -08:00
|
|
|
coffee path/to/script.coffee
|
|
|
|
coffee --interactive
|
|
|
|
coffee --watch --lint experimental.coffee
|
|
|
|
coffee --print app/scripts/*.coffee > concatenation.js</pre>
|
2009-12-23 19:42:18 -05:00
|
|
|
|
2010-02-14 15:41:08 -05:00
|
|
|
<h2 id="try_coffee">Try CoffeeScript</h2>
|
2010-02-13 16:23:03 -05:00
|
|
|
|
|
|
|
<p>
|
|
|
|
Here's a live version of the CoffeeScript compiler, running within
|
|
|
|
your browser.<br /> Hit the <b>compile</b> button to generate JavaScript
|
|
|
|
on the right-hand side.
|
|
|
|
You can also paste in any of the examples from below.
|
|
|
|
</p>
|
|
|
|
|
|
|
|
<div class="code repl_wrapper">
|
|
|
|
<textarea id="repl_source">reverse: (string) ->
|
|
|
|
string.split('').reverse()
|
|
|
|
|
|
|
|
print reverse 'tpircseeffoc'</textarea>
|
|
|
|
<pre id="repl_results"></pre>
|
|
|
|
<button onclick="javascript: repl_compile();">compile</button>
|
|
|
|
<br class="clear" />
|
|
|
|
</div>
|
|
|
|
|
2009-12-23 19:42:18 -05:00
|
|
|
<h2>Language Reference</h2>
|
|
|
|
|
|
|
|
<p>
|
|
|
|
<i>
|
2009-12-24 09:56:44 -08:00
|
|
|
This reference is structured so that it can be read from top to bottom,
|
2009-12-23 19:42:18 -05:00
|
|
|
if you like. Later sections use ideas and syntax previously introduced.
|
2009-12-23 23:01:39 -08:00
|
|
|
Familiarity with JavaScript is assumed.
|
2009-12-23 19:42:18 -05:00
|
|
|
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>
|
|
|
|
|
2010-01-04 22:19:45 -05:00
|
|
|
<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
|
2010-01-05 00:19:22 -05:00
|
|
|
<tt>{ }</tt> to delimit blocks of code (like <a href="#functions">functions</a>,
|
2009-12-24 22:29:30 -08:00
|
|
|
<a href="#conditionals">if-statements</a>,
|
2010-01-04 22:19:45 -05:00
|
|
|
<a href="#switch">switch</a>, and <a href="#try">try/catch</a>),
|
|
|
|
use indentation.
|
2009-12-23 19:42:18 -05:00
|
|
|
</p>
|
|
|
|
|
2010-01-26 23:23:59 -05:00
|
|
|
<p>
|
|
|
|
You don't need to use parentheses to invoke a function, if you're passing
|
|
|
|
arguments:<br /><tt>print "coffee"</tt>
|
|
|
|
</p>
|
|
|
|
|
2010-01-05 21:40:36 -05:00
|
|
|
<p>
|
|
|
|
You can use newlines to break up your expression into smaller pieces,
|
2010-01-17 18:33:31 -05:00
|
|
|
as long as CoffeeScript can determine that the line hasn't finished yet.
|
2010-01-05 21:40:36 -05:00
|
|
|
</p>
|
|
|
|
|
2009-12-23 19:42:18 -05:00
|
|
|
<p id="functions">
|
|
|
|
<b class="header">Functions and Invocation</b>
|
2009-12-24 22:29:30 -08:00
|
|
|
Functions are defined by a list of parameters, an arrow, and the
|
2010-01-26 10:52:05 -05:00
|
|
|
function body. The empty function looks like this: <tt>-></tt>. All
|
2010-01-17 18:33:31 -05:00
|
|
|
functions in CoffeeScript are named by default, for easier debugging.
|
2009-12-23 19:42:18 -05:00
|
|
|
</p>
|
|
|
|
<%= code_for('functions', 'cube(5)') %>
|
2010-01-17 18:33:31 -05:00
|
|
|
<p>
|
|
|
|
If you'd like to create an anonymous function, just wrap it in parentheses:
|
2010-01-26 10:52:05 -05:00
|
|
|
<tt>((x) -> x * x)</tt>
|
2010-01-17 18:33:31 -05:00
|
|
|
</p>
|
2009-12-23 19:42:18 -05:00
|
|
|
|
|
|
|
<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
|
2010-02-15 13:43:35 -05:00
|
|
|
mathy things. While colons are preferred, the two may be used interchangeably,
|
|
|
|
even within object literals.
|
2009-12-23 19:42:18 -05:00
|
|
|
</p>
|
|
|
|
<%= code_for('assignment', 'greeting') %>
|
2009-12-25 23:17:34 -08:00
|
|
|
<p>
|
2010-01-17 18:33:31 -05:00
|
|
|
Declaration of new variables are pushed up to the top of the nearest
|
2010-01-04 23:26:27 -05:00
|
|
|
lexical scope, so that assignment may always be performed within expressions.
|
2009-12-25 23:17:34 -08:00
|
|
|
</p>
|
2009-12-23 19:42:18 -05:00
|
|
|
|
|
|
|
<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
|
2010-01-17 18:33:31 -05:00
|
|
|
assigning local variables, and can be moved around freely. Feel free to mix
|
2010-01-04 22:19:45 -05:00
|
|
|
and match the two styles.
|
2009-12-23 19:42:18 -05:00
|
|
|
</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
|
2009-12-23 23:01:39 -08:00
|
|
|
are properly declared within lexical scope — you never need to write
|
|
|
|
<tt>var</tt> yourself.
|
2009-12-23 19:42:18 -05:00
|
|
|
</p>
|
|
|
|
<%= code_for('scope', 'new_num') %>
|
|
|
|
<p>
|
2010-01-04 22:19:45 -05:00
|
|
|
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.
|
2009-12-23 19:42:18 -05:00
|
|
|
</p>
|
|
|
|
<p>
|
2009-12-23 23:01:39 -08:00
|
|
|
Although suppressed within this documentation for clarity, all
|
2009-12-23 19:42:18 -05:00
|
|
|
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
|
2010-01-26 23:23:59 -05:00
|
|
|
to pollute the global namespace by accident.
|
2010-01-17 18:33:31 -05:00
|
|
|
</p>
|
2010-01-26 23:23:59 -05:00
|
|
|
<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
|
2010-01-17 18:33:31 -05:00
|
|
|
reliable way to figure out where to add them, if you're targeting both
|
|
|
|
CommonJS and the browser: <tt>root: exports ? this</tt>
|
2009-12-23 19:42:18 -05:00
|
|
|
</p>
|
|
|
|
|
|
|
|
<p id="conditionals">
|
|
|
|
<b class="header">Conditionals, Ternaries, and Conditional Assignment</b>
|
2009-12-28 21:02:40 -05:00
|
|
|
<b>If/else</b> statements can be written without the use of parentheses and
|
2010-01-04 22:19:45 -05:00
|
|
|
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.
|
2009-12-23 19:42:18 -05:00
|
|
|
</p>
|
2009-12-24 01:38:32 -08:00
|
|
|
<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>
|
2009-12-23 19:42:18 -05:00
|
|
|
<%= code_for('conditionals') %>
|
|
|
|
<p>
|
2010-01-04 22:19:45 -05:00
|
|
|
The conditional assignment operators are included: <tt>||=</tt>,
|
2009-12-23 19:42:18 -05:00
|
|
|
which only assigns a value to a variable if the variable's current value
|
2009-12-25 07:31:51 -08:00
|
|
|
is falsy, and <tt>&&=</tt>, which only replaces the value of
|
2009-12-23 19:42:18 -05:00
|
|
|
truthy variables.
|
|
|
|
</p>
|
|
|
|
|
2009-12-23 23:01:39 -08:00
|
|
|
<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>,
|
2009-12-24 17:21:20 -08:00
|
|
|
and <tt>isnt</tt> into <tt>!==</tt>.
|
2009-12-23 23:01:39 -08:00
|
|
|
</p>
|
|
|
|
<p>
|
|
|
|
You can use <tt>not</tt> as an alias for <tt>!</tt>.
|
|
|
|
</p>
|
2009-12-24 14:37:30 -08:00
|
|
|
<p>
|
|
|
|
For logic, <tt>and</tt> compiles to <tt>&&</tt>, and <tt>or</tt>
|
|
|
|
into <tt>||</tt>.
|
|
|
|
</p>
|
2009-12-24 01:33:59 -08:00
|
|
|
<p>
|
|
|
|
Instead of a newline or semicolon, <tt>then</tt> can be used to separate
|
2009-12-24 22:29:30 -08:00
|
|
|
conditions from expressions, in <b>while</b>,
|
2009-12-24 01:33:59 -08:00
|
|
|
<b>if</b>/<b>else</b>, and <b>switch</b>/<b>when</b> statements.
|
|
|
|
</p>
|
2009-12-23 23:01:39 -08:00
|
|
|
<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>.
|
2009-12-23 19:42:18 -05:00
|
|
|
</p>
|
2009-12-23 23:01:39 -08:00
|
|
|
<p>
|
|
|
|
For single-line statements, <tt>unless</tt> can be used as the inverse of <tt>if</tt>.
|
|
|
|
</p>
|
2010-02-08 10:58:49 -05:00
|
|
|
<p>
|
|
|
|
As a shortcut for <tt>this.property</tt>, you can use <tt>@property</tt>.
|
|
|
|
</p>
|
2009-12-23 23:01:39 -08:00
|
|
|
<%= code_for('aliases') %>
|
2009-12-23 19:42:18 -05:00
|
|
|
|
2010-01-04 23:26:27 -05:00
|
|
|
<p id="splats">
|
2010-01-05 21:40:36 -05:00
|
|
|
<b class="header">Splats...</b>
|
2010-01-04 23:26:27 -05:00
|
|
|
The JavaScript <b>arguments object</b> is a useful way to work with
|
|
|
|
functions that accept variable numbers of arguments. CoffeeScript provides
|
2010-01-05 21:40:36 -05:00
|
|
|
splats <tt>...</tt>, both for function definition as well as invocation,
|
2010-01-17 18:33:31 -05:00
|
|
|
making variable numbers of arguments a little bit more palatable.
|
2010-01-04 23:26:27 -05:00
|
|
|
</p>
|
|
|
|
<%= code_for('splats', true) %>
|
|
|
|
|
2010-01-05 09:29:10 -05:00
|
|
|
<p id="arguments">
|
|
|
|
<b class="header">Arguments are Arrays</b>
|
|
|
|
If you reference the <b>arguments object</b> directly, it will be converted
|
2010-01-05 21:40:36 -05:00
|
|
|
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>
|
2010-01-05 09:29:10 -05:00
|
|
|
available.
|
|
|
|
</p>
|
|
|
|
<%= code_for('arguments', true) %>
|
|
|
|
|
2009-12-23 19:42:18 -05:00
|
|
|
<p id="while">
|
|
|
|
<b class="header">While Loops</b>
|
2010-01-13 23:24:45 -05:00
|
|
|
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.
|
2009-12-23 19:42:18 -05:00
|
|
|
</p>
|
2010-01-13 23:24:45 -05:00
|
|
|
<%= code_for('while', 'lyrics.join("\n")') %>
|
2009-12-23 19:42:18 -05:00
|
|
|
<p>
|
2009-12-23 20:24:55 -05:00
|
|
|
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
|
2009-12-23 19:42:18 -05:00
|
|
|
<b>each</b> (<b>forEach</b>) style iterators, or...
|
|
|
|
</p>
|
|
|
|
|
2010-01-04 22:19:45 -05:00
|
|
|
<p id="comprehensions">
|
|
|
|
<b class="header">Comprehensions (Arrays, Objects, and Ranges)</b>
|
2009-12-23 20:24:55 -05:00
|
|
|
For your looping needs, CoffeeScript provides array comprehensions
|
2009-12-23 19:42:18 -05:00
|
|
|
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') %>
|
2009-12-26 20:46:31 -08:00
|
|
|
<p>
|
2010-01-04 22:19:45 -05:00
|
|
|
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
|
2010-01-10 15:52:23 -05:00
|
|
|
end of your comprehension. (The long line-breaking "for" definitions in
|
|
|
|
the compiled JS below allow ranges to count downwards, as well as upwards).
|
2010-01-04 22:19:45 -05:00
|
|
|
</p>
|
2010-01-10 15:52:23 -05:00
|
|
|
<%= code_for('range_comprehensions', 'countdown') %>
|
2010-01-04 22:19:45 -05:00
|
|
|
<p>
|
2010-01-10 15:52:23 -05:00
|
|
|
Comprehensions can also be used to iterate over the keys and values in
|
2010-01-11 22:55:01 -05:00
|
|
|
an object. Use <tt>of</tt> to signal comprehension over the properties of
|
2010-01-11 00:01:16 -05:00
|
|
|
an object instead of the values in an array.
|
2009-12-26 20:46:31 -08:00
|
|
|
</p>
|
2010-01-04 22:19:45 -05:00
|
|
|
<%= code_for('object_comprehensions', 'ages.join(", ")') %>
|
2009-12-23 19:42:18 -05:00
|
|
|
|
2010-01-04 22:19:45 -05:00
|
|
|
<p id="slice_splice">
|
|
|
|
<b class="header">Array Slicing and Splicing with Ranges</b>
|
2009-12-25 16:35:57 -08:00
|
|
|
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.
|
2009-12-23 19:42:18 -05:00
|
|
|
</p>
|
2009-12-25 16:35:57 -08:00
|
|
|
<%= code_for('slices', 'numbers_copy') %>
|
2010-01-04 22:19:45 -05:00
|
|
|
<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') %>
|
2010-01-04 23:26:27 -05:00
|
|
|
|
2010-01-04 22:51:02 -05:00
|
|
|
<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') %>
|
2010-01-10 17:14:20 -05:00
|
|
|
<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>
|
2010-01-04 22:51:02 -05:00
|
|
|
<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) %>
|
2010-01-17 18:12:59 -05:00
|
|
|
<p>
|
|
|
|
There are a handful of statements in JavaScript that can't be meaningfully
|
2010-01-26 23:23:59 -05:00
|
|
|
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,
|
2010-01-17 18:12:59 -05:00
|
|
|
CoffeeScript won't try to perform the conversion.
|
|
|
|
</p>
|
2010-02-13 15:25:04 -05:00
|
|
|
|
2010-01-26 23:23:59 -05:00
|
|
|
<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. CoffeeScript's 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>
|
|
|
|
The accessor variant of the existential operator <tt>?.</tt> can be used to soak
|
|
|
|
up null references in a chain of properties. Use it instead
|
|
|
|
of the dot accessor <tt>.</tt> in cases where the base value may be <b>null</b>
|
|
|
|
or <b>undefined</b>. If all of the properties exist then you'll get the expected
|
|
|
|
result, if the chain is broken, <b>undefined</b> is returned instead of
|
|
|
|
the <b>TypeError</b> that would be raised otherwise.
|
|
|
|
</p>
|
|
|
|
<%= code_for('soaks') %>
|
|
|
|
<p>
|
2010-02-13 15:25:04 -05:00
|
|
|
Soaking up nulls is similar to Ruby's
|
|
|
|
<a href="http://andand.rubyforge.org/">andand gem</a>, and to the
|
2010-01-26 23:23:59 -05:00
|
|
|
<a href="http://groovy.codehaus.org/Operators#Operators-SafeNavigationOperator%28%3F.%29">safe navigation operator</a>
|
|
|
|
in Groovy.
|
|
|
|
</p>
|
2009-12-23 19:42:18 -05:00
|
|
|
|
2009-12-24 16:49:23 -08:00
|
|
|
<p id="inheritance">
|
|
|
|
<b class="header">Inheritance, and Calling Super from a Subclass</b>
|
2009-12-23 19:42:18 -05:00
|
|
|
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:
|
2009-12-23 20:24:55 -05:00
|
|
|
<a href="http://code.google.com/p/base2/">Base2</a>,
|
2009-12-23 19:42:18 -05:00
|
|
|
<a href="http://prototypejs.org/">Prototype.js</a>,
|
|
|
|
<a href="http://jsclass.jcoglan.com/">JS.Class</a>, etc.
|
2009-12-23 20:48:55 -05:00
|
|
|
The libraries provide syntactic sugar, but the built-in inheritance would
|
2009-12-24 16:49:23 -08:00
|
|
|
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
|
2010-01-11 22:55:01 -05:00
|
|
|
set the prototype chain.
|
2010-01-10 17:14:20 -05:00
|
|
|
</p>
|
|
|
|
<p>
|
|
|
|
CoffeeScript provides <tt>extends</tt>
|
2010-01-10 16:16:59 -05:00
|
|
|
to help with prototype setup, <tt>::</tt> for quick access to an
|
2010-01-11 22:55:01 -05:00
|
|
|
object's prototype, and converts <tt>super()</tt> into a call against
|
2010-01-10 16:16:59 -05:00
|
|
|
the immediate ancestor's method of the same name.
|
2009-12-23 19:42:18 -05:00
|
|
|
</p>
|
|
|
|
<%= code_for('super', true) %>
|
|
|
|
|
2010-01-11 22:55:01 -05:00
|
|
|
<p id="pattern_matching">
|
|
|
|
<b class="header">Pattern Matching (Destructuring Assignment)</b>
|
2010-01-12 00:09:23 -05:00
|
|
|
To make extracting values from complex arrays and objects more convenient,
|
|
|
|
CoffeeScript implements ECMAScript Harmony's proposed
|
2010-01-11 22:55:01 -05:00
|
|
|
<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,
|
2010-01-12 00:09:23 -05:00
|
|
|
to help pull out deeply nested properties.
|
2010-01-11 22:55:01 -05:00
|
|
|
</p>
|
|
|
|
<%= code_for('object_extraction', 'poet + " — " + street') %>
|
|
|
|
|
2010-01-26 23:23:59 -05:00
|
|
|
<p id="fat_arrow">
|
2010-01-13 23:24:45 -05:00
|
|
|
<b class="header">Function binding</b>
|
2010-01-26 23:23:59 -05:00
|
|
|
The fat arrow <tt>=></tt> can be used to both define a function, and to bind
|
2010-01-13 23:24:45 -05:00
|
|
|
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
|
2010-01-26 23:23:59 -05:00
|
|
|
to use with <tt>bind</tt>. Functions created with the fat arrow are able to access
|
2010-01-13 23:24:45 -05:00
|
|
|
properties of the <tt>this</tt> where they're defined.
|
|
|
|
</p>
|
2010-01-26 23:23:59 -05:00
|
|
|
<%= code_for('fat_arrow') %>
|
2010-01-13 23:24:45 -05:00
|
|
|
|
2009-12-23 19:42:18 -05:00
|
|
|
<p id="embedded">
|
|
|
|
<b class="header">Embedded JavaScript</b>
|
2010-01-11 22:55:01 -05:00
|
|
|
Hopefully, you'll never need to use it, but if you ever need to intersperse
|
2010-01-11 09:09:06 -05:00
|
|
|
snippets of JavaScript within your CoffeeScript, you can
|
|
|
|
use backticks to pass it straight through.
|
2009-12-23 19:42:18 -05:00
|
|
|
</p>
|
2009-12-23 20:24:55 -05:00
|
|
|
<%= code_for('embedded', 'hi()') %>
|
2009-12-23 19:42:18 -05:00
|
|
|
|
|
|
|
<p id="switch">
|
2009-12-24 01:33:59 -08:00
|
|
|
<b class="header">Switch/When/Else</b>
|
2009-12-23 23:01:39 -08:00
|
|
|
<b>Switch</b> statements in JavaScript are rather broken. You can only
|
2009-12-24 00:12:07 -08:00
|
|
|
do comparisons based on string equality, and need to remember to <b>break</b> at the end of
|
2009-12-24 01:22:41 -05:00
|
|
|
every <b>case</b> statement to avoid accidentally falling through to
|
2009-12-24 01:33:59 -08:00
|
|
|
the default case. CoffeeScript compiles <b>switch</b> statements into JavaScript if-else chains, allowing you to
|
2009-12-23 19:42:18 -05:00
|
|
|
compare any object (via <b>===</b>), preventing fall-through, and resulting
|
2009-12-24 01:33:59 -08:00
|
|
|
in a returnable, assignable expression. The format is: <tt>switch</tt> condition,
|
|
|
|
<tt>when</tt> clauses, <tt>else</tt> the default case.
|
2009-12-23 19:42:18 -05:00
|
|
|
</p>
|
2010-01-13 23:24:45 -05:00
|
|
|
<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>
|
2009-12-23 19:42:18 -05:00
|
|
|
<%= code_for('switch') %>
|
|
|
|
|
|
|
|
<p id="try">
|
|
|
|
<b class="header">Try/Catch/Finally</b>
|
2009-12-23 23:01:39 -08:00
|
|
|
Try/catch statements are just about the same as JavaScript (although
|
2009-12-24 00:12:07 -08:00
|
|
|
they work as expressions).
|
2009-12-23 19:42:18 -05:00
|
|
|
</p>
|
|
|
|
<%= code_for('try') %>
|
|
|
|
|
2010-01-17 18:12:59 -05:00
|
|
|
<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') %>
|
|
|
|
|
2010-01-05 00:19:22 -05:00
|
|
|
<p id="strings">
|
2010-01-12 00:00:38 -05:00
|
|
|
<b class="header">Multiline Strings and Heredocs</b>
|
2009-12-23 19:42:18 -05:00
|
|
|
Multiline strings are allowed in CoffeeScript.
|
|
|
|
</p>
|
2009-12-24 01:22:41 -05:00
|
|
|
<%= code_for('strings', 'moby_dick') %>
|
2010-01-12 00:00:38 -05:00
|
|
|
<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') %>
|
2009-12-31 13:01:10 -05:00
|
|
|
|
2009-12-26 09:05:57 -08:00
|
|
|
<h2 id="resources">Resources</h2>
|
2009-12-31 13:01:10 -05:00
|
|
|
|
2009-12-31 18:22:51 -05:00
|
|
|
<ul>
|
|
|
|
<li>
|
|
|
|
<a href="http://github.com/jashkenas/coffee-script/">Source Code</a><br />
|
2010-01-11 22:55:01 -05:00
|
|
|
Use <tt>bin/coffee</tt> to test your changes,
|
2010-01-10 17:36:06 -05:00
|
|
|
<tt>rake test</tt> to run the test suite,
|
2010-02-08 10:58:49 -05:00
|
|
|
<tt>rake build:parser</tt> to regenerate the Racc parser if you're
|
|
|
|
working on the grammar,
|
2010-01-10 17:36:06 -05:00
|
|
|
and <tt>rake gem:install</tt> to
|
|
|
|
create and install a custom version of the gem.
|
2009-12-31 18:22:51 -05:00
|
|
|
</li>
|
|
|
|
<li>
|
2010-01-27 16:38:49 -05:00
|
|
|
<a href="http://github.com/jashkenas/coffee-script/issues">CoffeeScript Issues</a><br />
|
|
|
|
Bugs reports, feature requests, and general discussion all belong here.
|
2009-12-31 18:22:51 -05:00
|
|
|
</li>
|
2010-01-26 23:23:59 -05:00
|
|
|
<li>
|
2010-01-27 16:38:49 -05:00
|
|
|
<a href="http://github.com/mattly/rack-coffee">rack-coffee</a><br />
|
|
|
|
Rack middleware for serving CoffeeScripts as JavaScript directly to
|
|
|
|
the browser, without having to compile them first. From
|
|
|
|
<a href="http://github.com/mattly">Matt Lyon</a>.
|
2010-01-26 23:23:59 -05:00
|
|
|
</li>
|
2010-01-04 23:26:27 -05:00
|
|
|
<li>
|
|
|
|
<a href="http://github.com/jnicklas/bistro_car">BistroCar</a><br />
|
2010-01-05 09:29:10 -05:00
|
|
|
A Rails plugin by
|
2010-01-05 00:19:22 -05:00
|
|
|
<a href="http://github.com/jnicklas">Jonas Nicklas</a>
|
|
|
|
that includes CoffeeScript helpers,
|
2010-01-04 23:26:27 -05:00
|
|
|
bundling and minification.
|
|
|
|
</li>
|
2010-01-14 14:44:03 -05:00
|
|
|
<li>
|
|
|
|
<a href="http://github.com/inem/coffee-haml-filter">coffee-haml-filter</a><br />
|
2010-01-17 18:12:59 -05:00
|
|
|
A custom <a href="http://haml-lang.com/">HAML</a> filter, by
|
2010-01-14 14:44:03 -05:00
|
|
|
<a href="http://github.com/inem">Ivan Nemytchenko</a>, that embeds
|
|
|
|
snippets of CoffeeScript within your HAML templates.
|
|
|
|
</li>
|
2010-01-27 16:38:49 -05:00
|
|
|
<li>
|
|
|
|
<a href="http://github.com/creationix/coffeepot">CoffeePot</a><br />
|
|
|
|
An implementation of CoffeeScript, written in CoffeeScript, by
|
|
|
|
<a href="http://github.com/creationix">Tim Caswell</a>. Compiles just
|
|
|
|
a limited subset at this point.
|
|
|
|
</li>
|
2009-12-31 18:22:51 -05:00
|
|
|
</ul>
|
2009-12-24 22:29:30 -08:00
|
|
|
|
2010-01-26 23:23:59 -05:00
|
|
|
<h2 id="change_log">Change Log</h2>
|
2010-02-13 15:25:04 -05:00
|
|
|
|
2010-02-08 10:58:49 -05:00
|
|
|
<p>
|
|
|
|
<b class="header" style="margin-top: 20px;">0.3.2</b>
|
|
|
|
<tt>@property</tt> is now a shorthand for <tt>this.property</tt>.<br />
|
|
|
|
Switched the default JavaScript engine from Narwhal to Node.js. Pass
|
|
|
|
the <tt>--narwhal</tt> flag if you'd like to continue using it.
|
|
|
|
</p>
|
2009-12-24 22:29:30 -08:00
|
|
|
|
2009-12-24 09:54:12 -08:00
|
|
|
<p>
|
2010-01-26 23:23:59 -05:00
|
|
|
<b class="header" style="margin-top: 20px;">0.3.0</b>
|
|
|
|
CoffeeScript 0.3 includes major syntax changes:
|
|
|
|
<br />
|
|
|
|
The function symbol was changed to
|
|
|
|
<tt>-></tt>, and the bound function symbol is now <tt>=></tt>.
|
|
|
|
<br />
|
|
|
|
Parameter lists in function definitions must now be wrapped in parentheses.
|
|
|
|
<br />
|
|
|
|
Added property soaking, with the <tt>?.</tt> operator.
|
|
|
|
<br />
|
|
|
|
Made parentheses optional, when invoking functions with arguments.
|
|
|
|
<br />
|
|
|
|
Removed the obsolete block literal syntax.
|
2009-12-24 09:54:12 -08:00
|
|
|
</p>
|
2009-12-24 22:29:30 -08:00
|
|
|
|
2010-01-17 18:12:59 -05:00
|
|
|
<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>
|
|
|
|
|
2010-01-13 23:24:45 -05:00
|
|
|
<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>
|
|
|
|
|
2010-01-12 00:09:23 -05:00
|
|
|
<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>
|
2010-01-11 22:55:01 -05:00
|
|
|
|
2010-01-11 00:01:16 -05:00
|
|
|
<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
|
2010-01-11 08:46:50 -05:00
|
|
|
object comprehensions. They now look like: <tt>for prop, value of object</tt>.
|
2010-01-11 00:01:16 -05:00
|
|
|
</p>
|
2009-12-31 13:01:10 -05:00
|
|
|
|
2010-01-10 14:45:44 -05:00
|
|
|
<p>
|
|
|
|
<b class="header" style="margin-top: 20px;">0.2.2</b>
|
2010-01-10 16:16:59 -05:00
|
|
|
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 />
|
2010-01-10 14:45:44 -05:00
|
|
|
The "splat" symbol has been changed from a prefix asterisk <tt>*</tt>, to
|
2010-01-10 16:16:59 -05:00
|
|
|
a postfix ellipsis <tt>...</tt>
|
|
|
|
<br />
|
|
|
|
Added JavaScript's <tt>in</tt> operator,
|
2010-01-10 14:45:44 -05:00
|
|
|
empty <tt>return</tt> statements, and empty <tt>while</tt> loops.
|
2010-01-10 16:16:59 -05:00
|
|
|
<br />
|
2010-01-10 14:45:44 -05:00
|
|
|
Constructor functions that start with capital letters now include a
|
|
|
|
safety check to make sure that the new instance of the object is returned.
|
2010-01-10 16:16:59 -05:00
|
|
|
<br />
|
2010-01-10 14:45:44 -05:00
|
|
|
The <tt>extends</tt> keyword now functions identically to <tt>goog.inherits</tt>
|
|
|
|
in Google's Closure Library.
|
|
|
|
</p>
|
|
|
|
|
2010-01-05 09:29:10 -05:00
|
|
|
<p>
|
|
|
|
<b class="header" style="margin-top: 20px;">0.2.1</b>
|
|
|
|
Arguments objects are now converted into real arrays when referenced.
|
|
|
|
</p>
|
|
|
|
|
2010-01-05 00:19:22 -05:00
|
|
|
<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.
|
2010-01-17 18:12:59 -05:00
|
|
|
The existential operator. Many thanks to all the folks who posted issues,
|
2010-01-05 09:29:10 -05:00
|
|
|
with special thanks to
|
2010-01-05 00:19:22 -05:00
|
|
|
<a href="http://github.com/kamatsu">Liam O'Connor-Davis</a> for whitespace
|
|
|
|
and expression help.
|
|
|
|
</p>
|
|
|
|
|
2009-12-27 12:49:11 -08:00
|
|
|
<p>
|
|
|
|
<b class="header" style="margin-top: 20px;">0.1.6</b>
|
|
|
|
Bugfix for running <tt>coffee --interactive</tt> and <tt>--run</tt>
|
2009-12-31 13:01:10 -05:00
|
|
|
from outside of the CoffeeScript directory. Bugfix for nested
|
|
|
|
function/if-statements.
|
2009-12-27 12:49:11 -08:00
|
|
|
</p>
|
2009-12-31 13:01:10 -05:00
|
|
|
|
2009-12-26 21:25:37 -08:00
|
|
|
<p>
|
|
|
|
<b class="header" style="margin-top: 20px;">0.1.5</b>
|
|
|
|
Array slice literals and array comprehensions can now both take Ruby-style
|
2009-12-31 13:01:10 -05:00
|
|
|
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
|
2009-12-26 21:25:37 -08:00
|
|
|
expressions. You can use <tt>\</tt> to escape newlines.
|
|
|
|
The <tt>coffee-script</tt> command is now called <tt>coffee</tt>.
|
|
|
|
</p>
|
2009-12-25 16:35:57 -08:00
|
|
|
|
2009-12-25 14:43:24 -08:00
|
|
|
<p>
|
|
|
|
<b class="header" style="margin-top: 20px;">0.1.4</b>
|
|
|
|
The official CoffeeScript extension is now <tt>.coffee</tt> instead of
|
2009-12-25 16:35:57 -08:00
|
|
|
<tt>.cs</tt>, which properly belongs to
|
2009-12-25 14:43:24 -08:00
|
|
|
<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
|
2009-12-25 16:35:57 -08:00
|
|
|
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.
|
2009-12-25 14:43:24 -08:00
|
|
|
</p>
|
2009-12-25 14:18:05 -08:00
|
|
|
|
2009-12-25 00:16:56 -08:00
|
|
|
<p>
|
|
|
|
<b class="header" style="margin-top: 20px;">0.1.3</b>
|
2009-12-26 08:57:13 -08:00
|
|
|
The <tt>coffee</tt> command now includes <tt>--interactive</tt>,
|
2009-12-25 00:16:56 -08:00
|
|
|
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.
|
2009-12-25 14:18:05 -08:00
|
|
|
The <tt>aint</tt> keyword has been replaced by <tt>isnt</tt>, which goes
|
2009-12-25 00:16:56 -08:00
|
|
|
together a little smoother with <tt>is</tt>.
|
|
|
|
Quoted strings are now allowed as identifiers within object literals: eg.
|
|
|
|
<tt>{"5+5": 10}</tt>.
|
2009-12-25 14:18:05 -08:00
|
|
|
All assignment operators now use a colon: <tt>+:</tt>, <tt>-:</tt>,
|
2009-12-25 00:16:56 -08:00
|
|
|
<tt>*:</tt>, etc.
|
|
|
|
</p>
|
2009-12-24 22:29:30 -08:00
|
|
|
|
2009-12-24 12:02:28 -08:00
|
|
|
<p>
|
2009-12-24 17:05:55 -08:00
|
|
|
<b class="header" style="margin-top: 20px;">0.1.2</b>
|
2009-12-24 22:29:30 -08:00
|
|
|
Fixed a bug with calling <tt>super()</tt> through more than one level of
|
2009-12-24 17:05:55 -08:00
|
|
|
inheritance, with the re-addition of the <tt>extends</tt> keyword.
|
2009-12-24 22:29:30 -08:00
|
|
|
Added experimental <a href="http://narwhaljs.org/">Narwhal</a>
|
|
|
|
support (as a Tusk package), contributed by
|
2009-12-24 17:05:55 -08:00
|
|
|
<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>
|
2009-12-24 22:29:30 -08:00
|
|
|
|
2009-12-24 17:05:55 -08:00
|
|
|
<p>
|
|
|
|
<b class="header" style="margin-top: 20px;">0.1.1</b>
|
2009-12-24 12:02:28 -08:00
|
|
|
Added <tt>instanceof</tt> and <tt>typeof</tt> as operators.
|
|
|
|
</p>
|
2009-12-24 22:29:30 -08:00
|
|
|
|
2009-12-24 00:12:07 -08:00
|
|
|
<p>
|
2009-12-24 17:05:55 -08:00
|
|
|
<b class="header" style="margin-top: 20px;">0.1.0</b>
|
2009-12-24 00:12:07 -08:00
|
|
|
Initial CoffeeScript release.
|
|
|
|
</p>
|
2009-12-21 11:41:45 -05:00
|
|
|
|
2009-12-19 22:55:58 -05:00
|
|
|
</div>
|
|
|
|
|
2010-02-13 15:25:04 -05:00
|
|
|
<script type="text/javascript" src="lib/coffee_script/underscore.js"></script>
|
|
|
|
<script type="text/javascript" src="lib/coffee_script/rewriter.js"></script>
|
|
|
|
<script type="text/javascript" src="lib/coffee_script/lexer.js"></script>
|
|
|
|
<script type="text/javascript" src="lib/coffee_script/parser.js"></script>
|
|
|
|
<script type="text/javascript" src="lib/coffee_script/scope.js"></script>
|
|
|
|
<script type="text/javascript" src="lib/coffee_script/nodes.js"></script>
|
|
|
|
<script type="text/javascript" src="lib/coffee_script/coffee-script.js"></script>
|
|
|
|
|
2010-02-13 16:23:03 -05:00
|
|
|
<script type="text/javascript">
|
|
|
|
window.repl_compile = function() {
|
|
|
|
var source = document.getElementById('repl_source').value;
|
|
|
|
var js = '';
|
|
|
|
try {
|
|
|
|
js = compile(source, {no_wrap: true});
|
|
|
|
} catch(error) {
|
|
|
|
alert(error);
|
|
|
|
}
|
|
|
|
document.getElementById('repl_results').innerHTML = js;
|
|
|
|
}
|
|
|
|
</script>
|
|
|
|
|
2009-12-19 22:55:58 -05:00
|
|
|
</body>
|
|
|
|
</html>
|