<% require 'uv' def code_for(file, executable=false) @stripper ||= /(\A\(function\(\)\{\n|\}\)\(\);\Z|^ )/ cs = File.read("documentation/cs/#{file}.cs") 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 ? "" : '' "
#{cshtml}#{jshtml}#{button}
" end %> CoffeeScript, briefly...

CoffeeScript

CoffeeScript is a little language that compiles into JavaScript. Think of it as JavaScript's simpleminded kid brother — the same genes, the same accent, but a different sense of style. Apart from a handful of bonus goodies, statements in CoffeeScript correspond one-to-one with their JavaScript equivalent, it's just another way of saying it.

Disclaimer: CoffeeScript is just for fun and seriously alpha. There is no guarantee, explicit or implied, of its suitability for any purpose. That said, it compiles into pretty-printed JavaScript (the good parts) that can pass through JSLint warning-free.

Table of Contents

This document is structured so that it can be read from top to bottom, if you like. Later sections use ideas and syntax previously introduced.

Punctuation Primer
Functions and Invocation
Objects and Arrays
Assignment
Lexical Scoping and Variable Safety
Conditionals, Ternaries, and Conditional Assignment
Everything is an Expression
While Loops
Array Comprehensions
Array Slice Literals
Inheritance, and Calling Super from a Subclass Embedded JavaScript
Switch/Case/Else
Try/Catch/Finally
Multiline Strings

In all of the following examples, the source CoffeeScript is provided on the left, and the direct compilation into JavaScript is on the right.

Punctuation Primer You don't need to use semicolons to (;) terminate expressions, ending the line will do just as well. So newlines can matter, but whitespace is not otherwise significant. Instead of using curly braces ({ }) to delimit blocks of code, a period (.) marks the end of a function, if statement, or try/catch.

Functions and Invocation Let's start with the best part, shall we? Function literals are my absolute favorite thing about CoffeeScript.

<%= code_for('functions', 'cube(5)') %>

Objects and Arrays Object and Array literals look very similar. When you spread out each assignment on a separate line, the commas are optional.

<%= code_for('objects_and_arrays', 'song.join(",")') %>

Assignment All assignment in CoffeeScript, whether to a variable or to an object property, uses a colon. Equal signs are only needed for mathy things.

<%= code_for('assignment', 'greeting') %>

Lexical Scoping and Variable Safety The CoffeeScript compiler takes care to make sure that all of your variables are properly defined within lexical scope — you never need to declare var yourself.

<%= code_for('scope', 'new_num') %>

Notice how the variables are declared with var the first time they appear. The second reference of num, within the function, is not redeclared because num is still in scope. As opposed to the second new_num, in the last line.

Conditionals, Ternaries, and Conditional Assignment

<%= code_for('conditionals') %>

Everything is an Expression You might have noticed how even though we don't add return statements to CoffeScript functions, they nonetheless return their final value. The CoffeeScript compiler tries to make sure that every little language construct can be used as an expression.

<%= code_for('expressions', 'eldest') %>

When compiling a function definition, CoffeeScript tries to push down the return statement to each of the potential final lines of the function. It uses the same mechanism to push down assignment statements. If statement are compiled into ternary operators when possible, so that they can be used as expressions.

While Loops The only low-level loop that CoffeeScript provides is the while loop.

<%= code_for('while') %>

Array Comprehensions Most of your looping needs should be handled by array comprehensions. They replace (and compile into) for loops, handling each/forEach style loops, as well as select/filter. Unlike for loops, array comprehensions are expressions, and can be returned and assigned.

<%= code_for('array_comprehensions') %>

Array Slice Literals CoffeeScript includes a literal syntax for extracting slices of arrays. The first argument is the index of the first element in the slice, and the second is the index of the last one.

<%= code_for('slices', 'three_to_six') %>

Inheritance, and Calling Super from a Subclass JavaScript's prototypal inheritance has always been a bit of a brain-bender, with a whole family tree of libraries (Base2, Prototype ).

<%= code_for('super', true) %>

Embedded JavaScript If you ever need to interpolate literal JavaScript snippets, you can use backticks to pass JavaScript straight through.

<%= code_for('embedded', true) %>

Switch/Case/Else Switch statements in JavaScript are fundamentally broken. You can only do string comparisons, and need to break at the end of each case statment to prevent falling through to the default case. CoffeeScript compiles switch statements into if-else chains, allowing you to compare any object (via ===), preventing fall-through, and resulting in a returnable expression.

<%= code_for('switch') %>

Try/Catch/Finally Try/catch statements just about the same as JavaScript (although they work as expressions). No braces required.

<%= code_for('try') %>

Multiline Strings Multiline strings are allowed in CoffeeScript.

<%= code_for('strings') %>