- For a longer CoffeeScript example, check out
- Underscore.coffee, a port
- of the Underscore.js
- 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
- examples folder.
-
-
Installation and Usage
- The CoffeeScript compiler is written in pure CoffeeScript, using a
- small DSL
- on top of the Jison parser generator, and is available
- as a Node.js utility. The core compiler however,
- does not depend on Node, and can be run in other server-side-JavaScript environments,
- or in the browser (see "Try CoffeeScript", above).
+ The CoffeeScript compiler
+ is itself written in CoffeeScript,
+ using the Jison parser generator. The command-line
+ version of coffee is available as a Node.js utility.
+ The core compiler however, does not depend on Node, and can be run in any
+ JavaScript environment, or in the browser (see "Try CoffeeScript", above).
To install, first make sure you have a working copy of the latest tagged version of
- Node.js, currently 0.1.102 or higher.
- Then clone the CoffeeScript
- source repository
- from GitHub, or download the latest
- release: 0.9.5.
- To install the CoffeeScript compiler system-wide
- under /usr/local, open the directory and run:
-
-
-
-sudo bin/cake install
-
-
- Alternatively, if you already have the
- Node Package Manager installed,
- you can use that to grab the latest CoffeeScript:
+ Node.js, and NPM
+ (the Node Package Manager). You can then install CoffeeScript with NPM:
npm install coffee-script
+
+ If you'd prefer to install the latest master version of CoffeeScript, you
+ can clone the CoffeeScript
+ source repository
+ from GitHub, or download
+ the source directly.
+ To install the CoffeeScript compiler system-wide
+ under /usr/local, open the directory and run:
+
+
+
+sudo bin/cake install
+
- Both of these provide the coffee command, which will execute CoffeeScripts
- under Node.js by default, but is also used to compile CoffeeScript
- .coffee files into JavaScript, or to run an an interactive REPL.
- When compiling to JavaScript, coffee writes the output
- as .js files in the same directory by default, but output
- can be customized with the following options:
+ Both of these provide the coffee command, which can execute
+ coffee scripts, compile .coffee files into .js, and
+ provides an interactive REPL. The coffee command takes the
+ following options:
You don't need to use parentheses to invoke a function if you're passing
- arguments: print "coffee". Implicit parentheses wrap forwards
- to the end of the line, or block expression.
+ arguments: print "coffee". The implicit call wraps forward
+ to the end of the line or block expression.
-
+
- 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,
- because it ends with an operator or a dot ... seen most commonly
- in jQuery-chaining style JavaScript.
+ Within object literals, indentation can be used to create nested objects.
- Functions and Invocation
+ Functions
Functions are defined by a list of parameters, an arrow, and the
function body. The empty function looks like this: ->
@@ -361,15 +343,16 @@ coffee --print app/scripts/*.coffee > concatenation.js
Objects and Arrays
Object and Array literals look very similar to their JavaScript cousins.
- When you spread out each property on a separate line, the commas are
- optional. Implicit objects may be created with indentation instead of
- brackets, winding up looking quite similar to YAML.
+ When each property is listed on its own line, the commas are
+ optional. Objects may be created using indentation instead of
+ explicit braces, similar to YAML.
<%= code_for('objects_and_arrays', 'song.join(",")') %>
In JavaScript, you can't use reserved words, like class, as properties
- of an object, without quoting them as strings. CoffeeScript notices and quotes
- them for you, so you don't have to worry about it (say, when using jQuery).
+ of an object, without quoting them as strings. CoffeeScript notices reserved words
+ used as keys in objects and quotes them for you, so you don't have to worry
+ about it (say, when using jQuery).
<%= code_for('objects_reserved') %>
@@ -408,7 +391,7 @@ coffee --print app/scripts/*.coffee > concatenation.js
attach them as properties on window, or on the exports
object in CommonJS. The existential operator (covered below), gives you a
reliable way to figure out where to add them, if you're targeting both
- CommonJS and the browser: root = exports ? this
+ CommonJS and the browser: exports ? this
@@ -423,7 +406,7 @@ coffee --print app/scripts/*.coffee > concatenation.js
CoffeeScript can compile if statements into JavaScript expressions,
using the ternary operator when possible, and closure wrapping otherwise. There
is no explicit ternary statement in CoffeeScript — you simply use
- a regular if statement inline.
+ a regular if statement on a single line.
<%= code_for('conditionals') %>
@@ -463,6 +446,25 @@ coffee --print app/scripts/*.coffee > concatenation.js
You can use in to test for array presence, and of to
test for JavaScript object-key presence.
+
+
+ All together now:
+
+
+
+
CoffeeScript
JavaScript
+
is
===
+
isnt
!==
+
not
!
+
and
&&
+
or
||
+
true, yes, on
true
+
false, no, off
false
+
@, this
this
+
of
in
+
in
(no JS equivalent)
+
+
<%= code_for('aliases') %>
@@ -818,6 +820,40 @@ coffee --print app/scripts/*.coffee > concatenation.js
run within a closure wrapper, so if you want to expose global variables or
functions, attach them to the window object.
+
+
+
+ Examples
+
+
+
+
+ frank06's riak-js, a Node.js client for
+ Riak, with support for HTTP
+ and Protocol Buffers.
+
@@ -909,29 +949,27 @@ coffee --print app/scripts/*.coffee > concatenation.js
— a CoffeeScript wrapper for CouchApps, web applications served
directly from CouchDB.
+
+ sstephenson's Eco
+ — high-performance embedded CoffeeScript templates, after EJS and ERB.
+
mauricemach's CoffeeKup
— Markup as CoffeeScript. After _why's
Markaby.
- jashkenas's Docco
+ jashkenas' Docco
— a quick-and-dirty literate-programming-style documentation generator
for CoffeeScript. Used to produce the annotated source.
-
- satyr's CUP
- — The CoffeeScript Unofficial Playpen, where the changes to
- the latest master can be experimented with.
-
-
- satyr's xqjs
- — A Firefox add-on that provides a simple JavaScript console
- with the ability to execute CoffeeScripts within the current window.
-
naturalethic's Congo
- — An asynchronous MongoDB ORM, for use with Node.js and CoffeeScript.
+ — an asynchronous MongoDB ORM, for use with Node.js and CoffeeScript.
+
+
+ mauricemach's Zappa
+ — a DSL for building web apps, built on top of Express and Socket.IO.
- For a longer CoffeeScript example, check out
- Underscore.coffee, a port
- of the Underscore.js
- 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
- examples folder.
-
-
Installation and Usage
- The CoffeeScript compiler is written in pure CoffeeScript, using a
- small DSL
- on top of the Jison parser generator, and is available
- as a Node.js utility. The core compiler however,
- does not depend on Node, and can be run in other server-side-JavaScript environments,
- or in the browser (see "Try CoffeeScript", above).
+ The CoffeeScript compiler
+ is itself written in CoffeeScript,
+ using the Jison parser generator. The command-line
+ version of coffee is available as a Node.js utility.
+ The core compiler however, does not depend on Node, and can be run in any
+ JavaScript environment, or in the browser (see "Try CoffeeScript", above).
To install, first make sure you have a working copy of the latest tagged version of
- Node.js, currently 0.1.102 or higher.
- Then clone the CoffeeScript
- source repository
- from GitHub, or download the latest
- release: 0.9.5.
- To install the CoffeeScript compiler system-wide
- under /usr/local, open the directory and run:
-
-
-
-sudo bin/cake install
-
-
- Alternatively, if you already have the
- Node Package Manager installed,
- you can use that to grab the latest CoffeeScript:
+ Node.js, and NPM
+ (the Node Package Manager). You can then install CoffeeScript with NPM:
npm install coffee-script
+
+ If you'd prefer to install the latest master version of CoffeeScript, you
+ can clone the CoffeeScript
+ source repository
+ from GitHub, or download
+ the source directly.
+ To install the CoffeeScript compiler system-wide
+ under /usr/local, open the directory and run:
+
+
+
+sudo bin/cake install
+
- Both of these provide the coffee command, which will execute CoffeeScripts
- under Node.js by default, but is also used to compile CoffeeScript
- .coffee files into JavaScript, or to run an an interactive REPL.
- When compiling to JavaScript, coffee writes the output
- as .js files in the same directory by default, but output
- can be customized with the following options:
+ Both of these provide the coffee command, which can execute
+ coffee scripts, compile .coffee files into .js, and
+ provides an interactive REPL. The coffee command takes the
+ following options:
You don't need to use parentheses to invoke a function if you're passing
- arguments: print "coffee". Implicit parentheses wrap forwards
- to the end of the line, or block expression.
+ arguments: print "coffee". The implicit call wraps forward
+ to the end of the line or block expression.
-
+
- 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,
- because it ends with an operator or a dot ... seen most commonly
- in jQuery-chaining style JavaScript.
+ Within object literals, indentation can be used to create nested objects.
- Functions and Invocation
+ Functions
Functions are defined by a list of parameters, an arrow, and the
function body. The empty function looks like this: ->
@@ -458,9 +440,9 @@ cube = function(x) {
Objects and Arrays
Object and Array literals look very similar to their JavaScript cousins.
- When you spread out each property on a separate line, the commas are
- optional. Implicit objects may be created with indentation instead of
- brackets, winding up looking quite similar to YAML.
+ When each property is listed on its own line, the commas are
+ optional. Objects may be created using indentation instead of
+ explicit braces, similar to YAML.
In JavaScript, you can't use reserved words, like class, as properties
- of an object, without quoting them as strings. CoffeeScript notices and quotes
- them for you, so you don't have to worry about it (say, when using jQuery).
+ of an object, without quoting them as strings. CoffeeScript notices reserved words
+ used as keys in objects and quotes them for you, so you don't have to worry
+ about it (say, when using jQuery).
@@ -579,7 +565,7 @@ inner = changeNumbers();;alert(inner);'>run: inner <
attach them as properties on window, or on the exports
object in CommonJS. The existential operator (covered below), gives you a
reliable way to figure out where to add them, if you're targeting both
- CommonJS and the browser: root = exports ? this
+ CommonJS and the browser: exports ? this
@@ -594,7 +580,7 @@ inner = changeNumbers();;alert(inner);'>run: inner <
CoffeeScript can compile if statements into JavaScript expressions,
using the ternary operator when possible, and closure wrapping otherwise. There
is no explicit ternary statement in CoffeeScript — you simply use
- a regular if statement inline.
+ a regular if statement on a single line.
mood = greatlyImproved if singing
@@ -657,6 +643,25 @@ options || (options =
You can use in to test for array presence, and of to
test for JavaScript object-key presence.
+
+
+ All together now:
+
+
+
+
CoffeeScript
JavaScript
+
is
===
+
isnt
!==
+
not
!
+
and
&&
+
or
||
+
true, yes, on
true
+
false, no, off
false
+
@, this
this
+
of
in
+
in
(no JS equivalent)
+
+
launch() if ignition ison
volume =10if band isnt SpinalTap
@@ -829,18 +834,16 @@ lunch = eat food for f
for roid, pos in asteroids
for roid2 in asteroids when roid isnt roid2
roid.explode() if roid.overlaps roid2
-