2009-12-21 11:41:45 -05:00
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" / >
2010-02-27 02:21:26 -05:00
< link rel = "shortcut icon" href = "documentation/images/favicon.ico" / >
2009-12-19 22:55:58 -05:00
< / head >
2010-12-12 21:41:04 -05:00
< body >
2010-02-21 16:36:34 -05:00
2010-02-21 14:30:21 -05:00
< div id = "fadeout" > < / div >
< div id = "flybar" >
2010-02-21 16:13:09 -05:00
< a id = "logo" href = "#top" > < / a >
2010-03-08 06:34:07 -05:00
< div class = "navigation toc" >
2010-02-21 14:30:21 -05:00
< div class = "button" >
Table of Contents
< / div >
2010-12-12 21:41:04 -05:00
< div class = "contents menu" >
2010-11-21 19:53:31 -05:00
< a href = "#overview" > Overview< / a >
2010-02-21 14:30:21 -05:00
< a href = "#installation" > Installation and Usage< / a >
2010-02-21 16:13:09 -05:00
< a href = "#language" > Language Reference< / a >
2010-12-21 22:24:24 -05:00
< a href = "#literals" > Literals: Functions, Objects and Arrays< / a >
2010-02-21 14:30:21 -05:00
< a href = "#lexical_scope" > Lexical Scoping and Variable Safety< / a >
2010-07-16 22:44:07 -04:00
< a href = "#conditionals" > If, Else, Unless, and Conditional Assignment< / a >
2010-02-21 14:30:21 -05:00
< a href = "#splats" > Splats...< / a >
2010-12-21 22:24:24 -05:00
< a href = "#loops" > Loops and Comprehensions< / a >
2010-12-04 12:52:51 -05:00
< a href = "#slices" > Array Slicing and Splicing< / a >
2010-02-21 14:30:21 -05:00
< a href = "#expressions" > Everything is an Expression< / a >
2010-12-21 22:24:24 -05:00
< a href = "#operators" > Operators and Aliases< / a >
2010-02-27 20:12:47 -05:00
< a href = "#classes" > Classes, Inheritance, and Super< / a >
2010-12-23 16:30:35 -05:00
< a href = "#destructuring" > Destructuring Assignment< / a >
2010-02-21 14:30:21 -05:00
< a href = "#fat_arrow" > Function Binding< / a >
< a href = "#embedded" > Embedded JavaScript< / a >
2010-12-21 22:24:24 -05:00
< a href = "#switch" > Switch and Try/Catch< / a >
2010-02-21 14:30:21 -05:00
< a href = "#comparisons" > Chained Comparisons< / a >
2010-11-21 21:12:59 -05:00
< a href = "#strings" > String Interpolation, Heredocs, and Block Comments< / a >
< a href = "#regexes" > Extended Regular Expressions< / a >
2010-02-21 18:37:52 -05:00
< a href = "#cake" > Cake, and Cakefiles< / a >
2010-02-25 00:26:59 -05:00
< a href = "#scripts" > "text/coffeescript" Script Tags< / a >
2011-08-04 23:45:38 -04:00
< a href = "#resources" > Books, Screencasts, Examples and Resources< / a >
2010-02-21 14:30:21 -05:00
< a href = "#change_log" > Change Log< / a >
< / div >
< / div >
2010-02-21 16:13:09 -05:00
< div class = "navigation try" >
< div class = "button" >
Try CoffeeScript
2010-12-21 21:35:43 -05:00
< div class = "repl_bridge" > < / div >
2010-02-21 16:13:09 -05:00
< / div >
< div class = "contents repl_wrapper" >
< div class = "code" >
2010-12-12 21:41:04 -05:00
< div class = "screenshadow tl" > < / div >
< div class = "screenshadow tr" > < / div >
< div class = "screenshadow bl" > < / div >
< div class = "screenshadow br" > < / div >
< div id = "repl_source_wrap" >
2011-02-09 18:52:35 -05:00
< textarea id = "repl_source" rows = "100" spellcheck = "false" > alert "Hello CoffeeScript!"< / textarea >
2010-12-12 21:41:04 -05:00
< / div >
2010-12-24 14:02:10 -05:00
< div id = "repl_results_wrap" > < pre id = "repl_results" > < / pre > < / div >
2011-02-10 11:28:02 -05:00
< div class = "minibutton dark run" title = "Ctrl-Enter" > Run< / div >
2011-09-18 21:53:08 -04:00
< a class = "minibutton permalink" id = "repl_permalink" > Link< / a >
2010-02-21 16:13:09 -05:00
< br class = "clear" / >
< / div >
< / div >
< / div >
2010-03-08 06:34:07 -05:00
< div class = "navigation annotated" >
< div class = "button" >
Annotated Source
< / div >
2010-12-12 21:41:04 -05:00
< div class = "contents menu" >
2010-03-08 09:13:15 -05:00
< a href = "documentation/docs/grammar.html" > Grammar Rules — src/grammar< / a >
< a href = "documentation/docs/lexer.html" > Lexing Tokens — src/lexer< / a >
2010-03-08 06:34:07 -05:00
< a href = "documentation/docs/rewriter.html" > The Rewriter — src/rewriter< / a >
< a href = "documentation/docs/nodes.html" > The Syntax Tree — src/nodes< / a >
< a href = "documentation/docs/scope.html" > Lexical Scope — src/scope< / a >
2010-03-15 23:53:43 -04:00
< a href = "documentation/docs/helpers.html" > Helpers & Utility Functions — src/helpers< / a >
2010-03-08 06:34:07 -05:00
< a href = "documentation/docs/coffee-script.html" > The CoffeeScript Module — src/coffee-script< / a >
< a href = "documentation/docs/cake.html" > Cake & Cakefiles — src/cake< / a >
< a href = "documentation/docs/command.html" > "coffee" Command-Line Utility — src/command< / a >
< a href = "documentation/docs/optparse.html" > Option Parsing — src/optparse< / a >
< a href = "documentation/docs/repl.html" > Interactive REPL — src/repl< / a >
< / div >
< / div >
2010-02-25 20:48:54 -05:00
< div id = "error" style = "display:none;" > < / div >
2010-02-21 14:30:21 -05:00
< / div >
2009-12-19 22:55:58 -05:00
< div class = "container" >
2010-02-21 16:13:09 -05:00
< span class = "bookmark" id = "top" > < / span >
2009-12-19 22:55:58 -05:00
< p >
2010-12-12 15:02:27 -05:00
< b > CoffeeScript is a little language that compiles into JavaScript.< / b > Underneath
2011-11-08 18:01:45 -05:00
all those awkward braces and semicolons, JavaScript has always had
2010-11-21 21:12:59 -05:00
a gorgeous object model at its heart. CoffeeScript is an attempt to expose
2010-11-21 12:38:27 -05:00
the good parts of JavaScript in a simple way.
2009-12-21 11:41:45 -05:00
< / p >
< p >
2010-11-21 12:38:27 -05:00
The golden rule of CoffeeScript is: < i > "It's just JavaScript"< / i > . The code
compiles one-to-one into the equivalent JS, and there is
2010-11-21 21:12:59 -05:00
no interpretation at runtime. You can use any existing JavaScript library
2011-11-08 18:01:45 -05:00
seamlessly from CoffeeScript (and vice-versa). The compiled output is
readable and pretty-printed, passes through
< a href = "http://www.javascriptlint.com/" > JavaScript Lint< / a >
2010-12-21 22:34:53 -05:00
without warnings, will work in every JavaScript implementation, and tends
to run as fast or faster than the equivalent handwritten JavaScript.
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 >
2011-11-08 18:01:45 -05:00
< a href = "http://github.com/jashkenas/coffee-script/tarball/1.1.3" > 1.1.3< / a >
2010-01-04 19:15:24 -05:00
< / p >
2009-12-21 11:41:45 -05:00
2010-02-21 14:30:21 -05:00
< h2 >
< span id = "overview" class = "bookmark" > < / span >
2010-11-21 12:38:27 -05:00
Overview
2010-02-21 14:30:21 -05:00
< / h2 >
2009-12-23 19:42:18 -05:00
< p > < i > CoffeeScript on the left, compiled JavaScript output on the right.< / i > < / p >
2009-12-31 13:01:10 -05:00
< div class = 'code' > < pre class = "idle" > < span class = "Comment" > < span class = "Comment" > #< / span > Assignment:< / span >
2010-07-29 00:51:35 -04:00
number < span class = "Keyword" > =< / span > < span class = "Number" > 42< / span >
opposite < span class = "Keyword" > =< / span > < span class = "BuiltInConstant" > true< / span >
2009-12-23 19:42:18 -05:00
2009-12-31 13:01:10 -05:00
< span class = "Comment" > < span class = "Comment" > #< / span > Conditions:< / span >
2010-07-29 00:51:35 -04:00
number < span class = "Keyword" > =< / span > < span class = "Keyword" > -< / span > < span class = "Number" > 42< / span > < span class = "Keyword" > if< / span > opposite
2009-12-23 19:42:18 -05:00
2009-12-31 13:01:10 -05:00
< span class = "Comment" > < span class = "Comment" > #< / span > Functions:< / span >
2011-07-12 13:13:46 -04:00
< span class = "FunctionName" > square < / span > < span class = "Keyword" > =< / span > < span class = "FunctionArgument" > (x)< / span > < span class = "Storage" > -> < / span > x < span class = "Keyword" > *< / span > x
2009-12-23 19:42:18 -05:00
2009-12-31 13:01:10 -05:00
< span class = "Comment" > < span class = "Comment" > #< / span > Arrays:< / span >
2010-07-29 00:51:35 -04:00
list < span class = "Keyword" > =< / span > [< span class = "Number" > 1< / span > , < span class = "Number" > 2< / span > , < span class = "Number" > 3< / span > , < span class = "Number" > 4< / span > , < span class = "Number" > 5< / span > ]
2009-12-23 19:42:18 -05:00
2009-12-31 13:01:10 -05:00
< span class = "Comment" > < span class = "Comment" > #< / span > Objects:< / span >
2010-07-29 00:51:35 -04:00
math < span class = "Keyword" > =< / span >
2010-09-21 23:58:05 -04:00
root: Math.sqrt
square: square
2011-07-12 13:13:46 -04:00
< span class = "FunctionName" > cube< / span > < span class = "Keyword" > :< / span > < span class = "FunctionArgument" > (x)< / span > < span class = "Storage" > -> < / span > x < span class = "Keyword" > *< / span > square x
2009-12-23 19:42:18 -05:00
2010-01-05 00:34:18 -05:00
< span class = "Comment" > < span class = "Comment" > #< / span > Splats:< / span >
2011-07-12 13:13:46 -04:00
< span class = "FunctionName" > race < / span > < span class = "Keyword" > =< / span > < span class = "FunctionArgument" > (winner, runners...)< / span > < span class = "Storage" > -> < / span >
2010-01-25 00:14:00 -05:00
print winner, runners
2010-01-05 00:34:18 -05:00
< span class = "Comment" > < span class = "Comment" > #< / span > Existence:< / span >
2010-01-25 00:14:00 -05:00
alert < span class = "String" > < span class = "String" > " < / span > I knew it!< span class = "String" > " < / span > < / span > < span class = "Keyword" > if< / span > elvis< span class = "Keyword" > ?< / span >
2010-01-05 00:34:18 -05:00
2009-12-31 13:01:10 -05:00
< span class = "Comment" > < span class = "Comment" > #< / span > Array comprehensions:< / span >
2010-11-21 12:38:27 -05:00
cubes < span class = "Keyword" > =< / span > (math.cube num < span class = "Keyword" > for< / span > num < span class = "Keyword" > in< / span > list)
2010-12-15 21:11:43 -05:00
< / pre > < pre class = "idle" > < span class = "Storage" > var< / span > cubes, list, math, num, number, opposite, race, square;
2010-04-03 20:43:50 -04:00
< span class = "Storage" > var< / span > __slice < span class = "Keyword" > =< / span > < span class = "LibraryClassType" > Array< / span > .< span class = "LibraryConstant" > prototype< / span > .slice;
2011-09-20 23:42:19 -04:00
2010-02-25 17:18:28 -05:00
number < span class = "Keyword" > =< / span > < span class = "Number" > 42< / span > ;
2011-09-20 23:42:19 -04:00
2010-06-28 00:19:58 -04:00
opposite < span class = "Keyword" > =< / span > < span class = "BuiltInConstant" > true< / span > ;
2011-09-20 23:42:19 -04:00
2011-08-14 16:24:26 -04:00
< span class = "Keyword" > if< / span > (opposite) number < span class = "Keyword" > =< / span > < span class = "Keyword" > -< / span > < span class = "Number" > 42< / span > ;
2011-09-20 23:42:19 -04:00
2010-05-15 01:18:05 -04:00
< span class = "FunctionName" > square< / span > = < span class = "Storage" > function< / span > (< span class = "FunctionArgument" > x< / span > ) {
2010-02-25 17:18:28 -05:00
< span class = "Keyword" > return< / span > x < span class = "Keyword" > *< / span > x;
};
2011-09-20 23:42:19 -04:00
2010-02-25 17:18:28 -05:00
list < span class = "Keyword" > =< / span > [< span class = "Number" > 1< / span > , < span class = "Number" > 2< / span > , < span class = "Number" > 3< / span > , < span class = "Number" > 4< / span > , < span class = "Number" > 5< / span > ];
2011-09-20 23:42:19 -04:00
2010-02-25 17:18:28 -05:00
math < span class = "Keyword" > =< / span > {
root: < span class = "LibraryClassType" > Math< / span > .sqrt,
square: square,
2010-05-15 01:18:05 -04:00
< span class = "FunctionName" > cube< / span > : < span class = "Storage" > function< / span > (< span class = "FunctionArgument" > x< / span > ) {
2010-02-25 17:18:28 -05:00
< span class = "Keyword" > return< / span > x < span class = "Keyword" > *< / span > square(x);
}
};
2011-09-20 23:42:19 -04:00
2010-11-21 12:38:27 -05:00
< span class = "FunctionName" > race< / span > = < span class = "Storage" > function< / span > () {
< span class = "Storage" > var< / span > runners, winner;
winner < span class = "Keyword" > =< / span > arguments[< span class = "Number" > 0< / span > ], runners < span class = "Keyword" > =< / span > < span class = "Number" > 2< / span > < span class = "Keyword" > < =< / span > arguments.< span class = "LibraryConstant" > length< / span > ? __slice.< span class = "LibraryFunction" > call< / span > (arguments, < span class = "Number" > 1< / span > ) : [];
2010-02-25 17:18:28 -05:00
< span class = "Keyword" > return< / span > < span class = "LibraryFunction" > print< / span > (winner, runners);
};
2011-09-20 23:42:19 -04:00
2011-08-14 16:24:26 -04:00
< span class = "Keyword" > if< / span > (< span class = "Keyword" > typeof< / span > elvis < span class = "Keyword" > !< / span > < span class = "Keyword" > ==< / span > < span class = "String" > < span class = "String" > " < / span > undefined< span class = "String" > " < / span > < / span > < span class = "Keyword" > & < / span > < span class = "Keyword" > & < / span > elvis < span class = "Keyword" > !< / span > < span class = "Keyword" > ==< / span > < span class = "BuiltInConstant" > null< / span > ) < span class = "LibraryFunction" > alert< / span > (< span class = "String" > < span class = "String" > " < / span > I knew it!< span class = "String" > " < / span > < / span > );
2011-09-20 23:42:19 -04:00
2010-11-21 21:12:59 -05:00
cubes < span class = "Keyword" > =< / span > (< span class = "Storage" > function< / span > () {
2010-12-15 21:11:43 -05:00
< span class = "Storage" > var< / span > _i, _len, _results;
2010-11-21 12:38:27 -05:00
_results < span class = "Keyword" > =< / span > [];
< span class = "Keyword" > for< / span > (_i < span class = "Keyword" > =< / span > < span class = "Number" > 0< / span > , _len < span class = "Keyword" > =< / span > list.< span class = "LibraryConstant" > length< / span > ; _i < span class = "Keyword" > < < / span > _len; _i< span class = "Keyword" > ++< / span > ) {
num < span class = "Keyword" > =< / span > list[_i];
_results.< span class = "LibraryFunction" > push< / span > (math.cube(num));
}
< span class = "Keyword" > return< / span > _results;
2010-12-23 15:41:42 -05:00
})();
2010-12-21 21:51:39 -05:00
< / pre > < script > window . example1 = "# Assignment:\nnumber = 42\nopposite = true\n\n# Conditions:\nnumber = -42 if opposite\n\n# Functions:\nsquare = (x) -> x * x\n\n# Arrays:\nlist = [1, 2, 3, 4, 5]\n\n# Objects:\nmath =\n root: Math.sqrt\n square: square\n cube: (x) -> x * square x\n\n# Splats:\nrace = (winner, runners...) ->\n print winner, runners\n\n# Existence:\nalert \"I knew it!\" if elvis?\n\n# Array comprehensions:\ncubes = (math.cube num for num in list)\n\nalert cubes" < / script > < div class = 'minibutton ok' onclick = 'javascript: var cubes , list , math , num , number , opposite , race , square ;
2010-04-03 20:43:50 -04:00
var __slice = Array.prototype.slice;
2011-09-20 23:42:19 -04:00
2010-02-25 17:18:28 -05:00
number = 42;
2011-09-20 23:42:19 -04:00
2010-06-28 00:19:58 -04:00
opposite = true;
2011-09-20 23:42:19 -04:00
2011-08-14 16:24:26 -04:00
if (opposite) number = -42;
2011-09-20 23:42:19 -04:00
2010-05-15 01:18:05 -04:00
square = function(x) {
2010-02-25 17:18:28 -05:00
return x * x;
};
2011-09-20 23:42:19 -04:00
2010-02-25 17:18:28 -05:00
list = [1, 2, 3, 4, 5];
2011-09-20 23:42:19 -04:00
2010-02-25 17:18:28 -05:00
math = {
root: Math.sqrt,
square: square,
2010-05-15 01:18:05 -04:00
cube: function(x) {
2010-02-25 17:18:28 -05:00
return x * square(x);
}
};
2011-09-20 23:42:19 -04:00
2010-11-21 12:38:27 -05:00
race = function() {
var runners, winner;
winner = arguments[0], runners = 2 < = arguments.length ? __slice.call(arguments, 1) : [];
2010-02-25 17:18:28 -05:00
return print(winner, runners);
};
2011-09-20 23:42:19 -04:00
2011-08-14 16:24:26 -04:00
if (typeof elvis !== "undefined" & & elvis !== null) alert("I knew it!");
2011-09-20 23:42:19 -04:00
2010-11-21 21:12:59 -05:00
cubes = (function() {
2010-12-15 21:11:43 -05:00
var _i, _len, _results;
2010-11-21 12:38:27 -05:00
_results = [];
for (_i = 0, _len = list.length; _i < _len ; _i + + ) {
num = list[_i];
_results.push(math.cube(num));
}
return _results;
2011-09-20 23:42:19 -04:00
})();
;alert(cubes);'>run: cubes< / div > < br class = 'clear' / > < / div >
2010-01-04 23:26:27 -05:00
2010-02-21 14:30:21 -05:00
< h2 >
< span id = "installation" class = "bookmark" > < / span >
Installation and Usage
< / h2 >
2009-12-25 03:16:56 -05:00
2009-12-24 03:12:07 -05:00
< p >
2010-12-23 16:30:35 -05:00
The CoffeeScript compiler is itself
< a href = "documentation/docs/grammar.html" > written in CoffeeScript< / a > ,
2010-12-23 16:44:12 -05:00
using the < a href = "http://jison.org" > Jison parser generator< / a > . The
command-line version of < tt > coffee< / tt > is available as a
< a href = "http://nodejs.org/" > Node.js< / a > utility. The
< a href = "extras/coffee-script.js" > core compiler< / a > however, does not
depend on Node, and can be run in any JavaScript environment, or in the
2010-12-23 16:30:35 -05:00
browser (see "Try CoffeeScript", above).
2010-02-21 17:30:41 -05:00
< / p >
2010-02-21 18:37:52 -05:00
2010-02-21 17:30:41 -05:00
< p >
2010-12-18 11:54:40 -05:00
To install, first make sure you have a working copy of the latest stable version of
2010-12-23 16:30:35 -05:00
< a href = "http://nodejs.org/" > Node.js< / a > , and < a href = "http://npmjs.org" > npm< / a >
(the Node Package Manager). You can then install CoffeeScript with npm:
2010-06-28 01:06:53 -04:00
< / p >
2010-07-17 10:28:06 -04:00
2010-06-28 01:06:53 -04:00
< pre >
2011-05-01 12:08:15 -04:00
npm install -g coffee-script< / pre >
< p >
(Leave off the < tt > -g< / tt > if you don't wish to install globally.)
< / p >
2011-09-20 23:42:19 -04:00
2011-05-01 12:08:15 -04:00
< p >
If you'd prefer to install the latest master version of CoffeeScript, you
can clone the CoffeeScript
< a href = "http://github.com/jashkenas/coffee-script" > source repository< / a >
from GitHub, or download
< a href = "http://github.com/jashkenas/coffee-script/tarball/master" > the source< / a > directly.
To install the CoffeeScript compiler system-wide
under < tt > /usr/local< / tt > , open the directory and run:
< / p >
2010-11-21 19:53:31 -05:00
< pre >
sudo bin/cake install< / pre >
2010-11-21 21:12:59 -05:00
2010-12-21 14:25:28 -05:00
< p >
2010-12-21 20:43:13 -05:00
If installing on Ubuntu or Debian,
< a href = "http://opinionated-programmer.com/2010/12/installing-coffeescript-on-debian-or-ubuntu/" > be
2010-12-21 14:25:28 -05:00
careful not to use the existing out-of-date package< / a > . If installing on
2011-07-09 01:26:30 -04:00
Windows, your best bet is probably to run Node.js under Cygwin. If you'd
2011-09-20 23:42:19 -04:00
just like to experiment, you can try the
2011-07-09 12:52:02 -04:00
< a href = "https://github.com/alisey/CoffeeScript-Compiler-for-Windows" > CoffeeScript Compiler For Windows< / a > .
2010-12-21 14:25:28 -05:00
< / p >
2010-06-28 01:06:53 -04:00
< p >
2010-12-23 16:44:12 -05:00
Once installed, you should have access to the < tt > coffee< / tt > command,
which can execute scripts, compile < tt > .coffee< / tt > files into < tt > .js< / tt > ,
2010-12-23 16:30:35 -05:00
and provide an interactive REPL. The < tt > coffee< / tt > command takes the
2010-11-21 19:53:31 -05:00
following options:
2009-12-23 19:42:18 -05:00
< / p >
< table >
< tr >
2010-02-27 00:38:04 -05:00
< td > < code > -c, --compile< / code > < / td >
2009-12-25 03:16:56 -05:00
< td >
2010-02-27 00:38:04 -05:00
Compile a < tt > .coffee< / tt > script into a < tt > .js< / tt > JavaScript file
of the same name.
2009-12-25 03:16:56 -05:00
< / td >
< / tr >
< tr >
2010-02-27 00:38:04 -05:00
< td width = "25%" > < code > -i, --interactive< / code > < / td >
2009-12-25 03:16:56 -05:00
< td >
2010-03-08 06:34:07 -05:00
Launch an interactive CoffeeScript session to try short snippets.
2011-11-08 18:01:45 -05:00
Identical to calling < tt > coffee< / tt > with no arguments.
2009-12-25 03:16:56 -05: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.
2010-02-27 00:38:04 -05:00
Use in conjunction with < tt > --compile< / tt > or < tt > --watch< / tt > .
2009-12-23 19:42:18 -05:00
< / td >
< / tr >
2010-12-23 01:43:13 -05:00
< tr >
2011-05-01 12:08:15 -04:00
< td > < code > -j, --join [FILE]< / code > < / td >
2010-12-23 01:43:13 -05:00
< td >
Before compiling, concatenate all scripts together in the order they
2011-09-20 23:42:19 -04:00
were passed, and write them into the specified file.
2011-05-01 12:08:15 -04:00
Useful for building large projects.
2010-12-23 01:43:13 -05:00
< / td >
< / tr >
2009-12-23 19:42:18 -05:00
< tr >
< td > < code > -w, --watch< / code > < / td >
< td >
2011-11-08 18:01:45 -05:00
Watch files for changes, rerunning the specified command when any
file is updated.
2009-12-23 19:42:18 -05:00
< / 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 >
2010-02-21 18:37:52 -05:00
If the < tt > jsl< / tt >
(< a href = "http://www.javascriptlint.com/" > JavaScript Lint< / a > )
2010-02-21 17:30:41 -05:00
command is installed, use it
2009-12-25 03:16:56 -05:00
to check the compilation of a CoffeeScript file. (Handy in
2010-08-03 01:13:31 -04:00
conjunction with < br / > < tt > --watch< / tt > )
2009-12-23 19:42:18 -05:00
< / td >
< / tr >
2010-02-24 18:27:10 -05:00
< tr >
< td > < code > -s, --stdio< / code > < / td >
< td >
Pipe in CoffeeScript to STDIN and get back JavaScript over STDOUT.
Good for use with processes written in other languages. An example:< br / >
2010-03-08 06:40:54 -05:00
< tt > cat src/cake.coffee | coffee -sc< / tt >
2010-02-24 18:27:10 -05:00
< / td >
< / tr >
2009-12-23 19:42:18 -05:00
< tr >
< td > < code > -e, --eval< / code > < / td >
< td >
Compile and print a little snippet of CoffeeScript directly from the
2011-07-09 01:26:30 -04:00
command line. For example:< br / > < tt > coffee -e "console.log num for num in [10..1]"< / tt >
2009-12-23 19:42:18 -05:00
< / td >
< / tr >
2010-08-11 00:40:15 -04:00
< tr >
< td > < code > -r, --require< / code > < / td >
< td >
Load a library before compiling or executing your script. Can be used
to hook in to the compiler (to add Growl notifications, for example).
< / td >
< / tr >
2009-12-24 20:37:24 -05:00
< tr >
2010-10-13 15:09:56 -04:00
< td > < code > -b, --bare< / code > < / td >
2009-12-24 20:37:24 -05:00
< td >
2011-11-08 18:01:45 -05:00
Compile the JavaScript without the
< a href = "#lexical_scope" > top-level function safety wrapper< / a > .
2010-01-10 17:14:20 -05:00
< / td >
< / tr >
< tr >
2010-02-21 17:30:41 -05:00
< td > < code > -t, --tokens< / code > < / td >
2010-01-10 17:14:20 -05:00
< td >
2010-02-21 17:30:41 -05:00
Instead of parsing the CoffeeScript, just lex it, and print out the
2010-08-04 23:14:34 -04:00
token stream: < tt > [IDENTIFIER square] [ASSIGN =] [PARAM_START (]< / tt > ...
2009-12-24 20:37:24 -05:00
< / td >
< / tr >
2009-12-24 02:01:39 -05:00
< tr >
2010-02-25 18:43:33 -05:00
< td > < code > -n, --nodes< / code > < / td >
2009-12-24 02:01:39 -05:00
< td >
2010-02-21 17:30:41 -05:00
Instead of compiling the CoffeeScript, just lex and parse it, and print
out the parse tree:
2010-02-22 17:14:51 -05:00
< pre class = "no_bar" >
Expressions
Assign
Value "square"
Code "x"
Op *
Value "x"
Value "x"< / pre >
2009-12-24 02:01:39 -05:00
< / td >
< / tr >
2010-12-18 11:54:40 -05:00
< tr >
< td > < code > --nodejs< / code > < / td >
< td >
The < tt > node< / tt > executable has some useful options you can set,
2011-07-09 01:26:30 -04:00
such as< br / > < tt > --debug< / tt > , < tt > --debug-brk< / tt > and < tt > --max-stack-size< / tt > . Use this
2010-12-18 11:54:40 -05:00
flag to forward options directly to Node.js.
< / td >
< / tr >
2009-12-23 19:42:18 -05:00
< / table >
< p >
< b > Examples:< / b >
< / p >
2010-12-23 16:44:12 -05:00
2010-12-23 16:30:35 -05:00
< ul >
< li >
2011-11-08 18:01:45 -05:00
Compile a directory tree of < tt > .coffee< / tt > files in < tt > src< / tt > into a parallel
tree of < tt > .js< / tt > files in < tt > lib< / tt > :< br / >
< tt > coffee --compile --output lib/ src/< / tt >
2010-12-23 16:30:35 -05:00
< / li >
< li >
Watch a file for changes, and recompile it every time the file is saved:< br / >
< tt > coffee --watch --compile experimental.coffee< / tt >
< / li >
< li >
Concatenate a list of files into a single script:< br / >
2011-05-01 12:08:15 -04:00
< tt > coffee --join project.js --compile src/*.coffee< / tt >
2010-12-23 16:30:35 -05:00
< / li >
< li >
Print out the compiled JS from a one-liner:< br / >
< tt > coffee -bpe "alert i for i in [0..10]"< / tt >
< / li >
2011-11-08 18:01:45 -05:00
< li >
All together now, watch and recompile an entire project as you work on it:< br / >
< tt > coffee -o lib/ -cw src/< / tt >
< / li >
2010-12-23 16:30:35 -05:00
< li >
Start the CoffeeScript REPL:< br / >
< tt > coffee< / tt >
< / li >
< / ul >
2009-12-23 19:42:18 -05:00
2010-02-21 14:30:21 -05:00
< h2 >
2010-02-21 16:13:09 -05:00
< span id = "language" class = "bookmark" > < / span >
Language Reference
2010-02-21 14:30:21 -05:00
< / h2 >
2010-02-13 16:23:03 -05:00
2009-12-21 11:41:45 -05:00
< p >
2009-12-23 19:42:18 -05:00
< i >
2009-12-24 12:56:44 -05: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-24 02:01:39 -05: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 >
2009-12-21 11:41:45 -05:00
< / p >
2010-02-21 18:37:52 -05:00
2010-02-21 17:30:41 -05:00
< p >
< i >
2010-12-23 16:44:12 -05:00
Many of the examples can be run (where it makes sense) by pressing the < b > run< / b >
button on the right, and can be loaded into the "Try CoffeeScript"
console by pressing the < b > load< / b > button on the left.
2010-02-21 17:30:41 -05:00
< / i >
2010-02-21 14:30:21 -05:00
< p >
2010-12-23 17:26:10 -05:00
First, the basics: CoffeeScript uses significant whitespace to delimit blocks of code.
You don't need to use semicolons < tt > ;< / tt > to terminate expressions,
2011-07-09 01:26:30 -04:00
ending the line will do just as well (although semicolons can still
be used to fit multiple expressions onto a single line).
2010-12-23 17:26:10 -05:00
Instead of using curly braces
< tt > { }< / tt > to surround blocks of code in < a href = "#functions" > functions< / a > ,
2009-12-25 03:16:56 -05:00
< a href = "#conditionals" > if-statements< / a > ,
2010-12-23 17:26:10 -05:00
< a href = "#switch" > switch< / a > , and < a href = "#try" > try/catch< / a > ,
2010-01-04 22:19:45 -05:00
use indentation.
2009-12-23 19:42:18 -05:00
< / p >
2010-01-26 23:23:59 -05:00
< p >
2010-02-21 17:30:41 -05:00
You don't need to use parentheses to invoke a function if you're passing
2010-12-23 17:26:10 -05:00
arguments. The implicit call wraps forward to the end of the line or block expression.< br / >
< tt > console.log sys.inspect object< / tt > → < tt > console.log(sys.inspect(object));< / tt >
2010-01-05 21:40:36 -05:00
< / p >
2010-02-21 14:30:21 -05:00
< p >
2010-12-21 22:24:24 -05:00
< span id = "literals" class = "bookmark" > < / span >
2010-11-21 19:53:31 -05:00
< b class = "header" > Functions< / b >
2010-12-23 17:26:10 -05:00
Functions are defined by an optional list of parameters in parentheses,
an arrow, and the function body. The empty function looks like this:
< tt > ->< / tt >
2009-12-23 19:42:18 -05:00
< / p >
2011-07-12 13:13:46 -04:00
< div class = 'code' > < pre class = "idle" > < span class = "FunctionName" > square < / span > < span class = "Keyword" > =< / span > < span class = "FunctionArgument" > (x)< / span > < span class = "Storage" > -> < / span > x < span class = "Keyword" > *< / span > x
< span class = "FunctionName" > cube < / span > < span class = "Keyword" > =< / span > < span class = "FunctionArgument" > (x)< / span > < span class = "Storage" > -> < / span > square(x) < span class = "Keyword" > *< / span > x
2009-12-26 02:17:34 -05:00
< / pre > < pre class = "idle" > < span class = "Storage" > var< / span > cube, square;
2011-09-20 23:42:19 -04:00
2010-05-15 01:18:05 -04:00
< span class = "FunctionName" > square< / span > = < span class = "Storage" > function< / span > (< span class = "FunctionArgument" > x< / span > ) {
2009-12-21 11:41:45 -05:00
< span class = "Keyword" > return< / span > x < span class = "Keyword" > *< / span > x;
};
2011-09-20 23:42:19 -04:00
2010-05-15 01:18:05 -04:00
< span class = "FunctionName" > cube< / span > = < span class = "Storage" > function< / span > (< span class = "FunctionArgument" > x< / span > ) {
2009-12-21 11:41:45 -05:00
< span class = "Keyword" > return< / span > square(x) < span class = "Keyword" > *< / span > x;
};
2010-12-23 16:44:12 -05:00
< / pre > < script > window . example2 = "square = (x) -> x * x\ncube = (x) -> square(x) * x\n\nalert cube(5)" < / script > < div class = 'minibutton load' onclick = 'javascript: loadConsole(example2);' > load< / div > < div class = 'minibutton ok' onclick = 'javascript: var cube , square ;
2011-09-20 23:42:19 -04:00
2010-05-15 01:18:05 -04:00
square = function(x) {
2009-12-21 11:41:45 -05:00
return x * x;
};
2011-09-20 23:42:19 -04:00
2010-05-15 01:18:05 -04:00
cube = function(x) {
2009-12-21 11:41:45 -05:00
return square(x) * x;
2011-09-20 23:42:19 -04:00
};
;alert(cube(5));'>run: cube(5)< / div > < br class = 'clear' / > < / div >
2010-11-21 21:12:59 -05:00
< p >
2010-12-23 17:26:10 -05:00
Functions may also have default values for arguments. Override the default
value by passing a non-null argument.
2010-11-21 21:12:59 -05:00
< / p >
2011-07-12 13:13:46 -04:00
< div class = 'code' > < pre class = "idle" > < span class = "FunctionName" > fill < / span > < span class = "Keyword" > =< / span > < span class = "FunctionArgument" > (container, liquid = " coffee" )< / span > < span class = "Storage" > -> < / span >
2010-11-21 21:12:59 -05:00
< span class = "String" > < span class = "String" > " < / span > Filling the < span class = "String" > < span class = "String" > #{< / span > container< span class = "String" > }< / span > < / span > with < span class = "String" > < span class = "String" > #{< / span > liquid< span class = "String" > }< / span > < / span > ...< span class = "String" > " < / span > < / span >
< / pre > < pre class = "idle" > < span class = "Storage" > var< / span > fill;
2011-09-20 23:42:19 -04:00
2010-11-21 21:12:59 -05:00
< span class = "FunctionName" > fill< / span > = < span class = "Storage" > function< / span > (< span class = "FunctionArgument" > container, liquid< / span > ) {
2011-08-14 16:24:26 -04:00
< span class = "Keyword" > if< / span > (liquid < span class = "Keyword" > ==< / span > < span class = "BuiltInConstant" > null< / span > ) liquid < span class = "Keyword" > =< / span > < span class = "String" > < span class = "String" > " < / span > coffee< span class = "String" > " < / span > < / span > ;
2010-11-21 21:12:59 -05:00
< span class = "Keyword" > return< / span > < span class = "String" > < span class = "String" > " < / span > Filling the < span class = "String" > " < / span > < / span > < span class = "Keyword" > +< / span > container < span class = "Keyword" > +< / span > < span class = "String" > < span class = "String" > " < / span > with < span class = "String" > " < / span > < / span > < span class = "Keyword" > +< / span > liquid < span class = "Keyword" > +< / span > < span class = "String" > < span class = "String" > " < / span > ...< span class = "String" > " < / span > < / span > ;
};
2010-12-23 16:44:12 -05:00
< / pre > < script > window . example3 = "fill = (container, liquid = \"coffee\") ->\n \"Filling the #{container} with #{liquid}...\"\n\nalert fill(\"cup\")" < / script > < div class = 'minibutton load' onclick = 'javascript: loadConsole(example3);' > load< / div > < div class = 'minibutton ok' onclick = 'javascript: var fill ;
2011-09-20 23:42:19 -04:00
2010-11-21 21:12:59 -05:00
fill = function(container, liquid) {
2011-08-14 16:24:26 -04:00
if (liquid == null) liquid = "coffee";
2010-11-21 21:12:59 -05:00
return "Filling the " + container + " with " + liquid + "...";
2011-09-20 23:42:19 -04:00
};
;alert(fill("cup"));'>run: fill("cup")< / div > < br class = 'clear' / > < / div >
2009-12-23 19:42:18 -05:00
2010-02-21 14:30:21 -05:00
< p >
< span id = "objects_and_arrays" class = "bookmark" > < / span >
2009-12-23 19:42:18 -05:00
< b class = "header" > Objects and Arrays< / b >
2010-12-23 17:26:10 -05:00
The CoffeeScript literals for objects and arrays look very similar to
their JavaScript cousins. 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 < a href = "http://yaml.org" > YAML< / a > .
2009-12-23 19:42:18 -05:00
< / p >
2010-07-29 00:51:35 -04:00
< div class = 'code' > < pre class = "idle" > song < span class = "Keyword" > =< / span > [< span class = "String" > < span class = "String" > " < / span > do< span class = "String" > " < / span > < / span > , < span class = "String" > < span class = "String" > " < / span > re< span class = "String" > " < / span > < / span > , < span class = "String" > < span class = "String" > " < / span > mi< span class = "String" > " < / span > < / span > , < span class = "String" > < span class = "String" > " < / span > fa< span class = "String" > " < / span > < / span > , < span class = "String" > < span class = "String" > " < / span > so< span class = "String" > " < / span > < / span > ]
2010-09-21 23:58:05 -04:00
singers < span class = "Keyword" > =< / span > {Jagger: < span class = "String" > < span class = "String" > " < / span > Rock< span class = "String" > " < / span > < / span > , Elvis: < span class = "String" > < span class = "String" > " < / span > Roll< span class = "String" > " < / span > < / span > }
2010-01-04 22:19:45 -05:00
2010-12-23 17:26:10 -05:00
bitlist < span class = "Keyword" > =< / span > [
2010-01-04 22:19:45 -05:00
< span class = "Number" > 1< / span > , < span class = "Number" > 0< / span > , < span class = "Number" > 1< / span >
< span class = "Number" > 0< / span > , < span class = "Number" > 0< / span > , < span class = "Number" > 1< / span >
< span class = "Number" > 1< / span > , < span class = "Number" > 1< / span > , < span class = "Number" > 0< / span >
]
2010-08-03 01:13:31 -04:00
kids < span class = "Keyword" > =< / span >
2010-09-21 23:58:05 -04:00
brother:
name: < span class = "String" > < span class = "String" > " < / span > Max< span class = "String" > " < / span > < / span >
age: < span class = "Number" > 11< / span >
sister:
name: < span class = "String" > < span class = "String" > " < / span > Ida< span class = "String" > " < / span > < / span >
age: < span class = "Number" > 9< / span >
2010-12-12 21:41:04 -05:00
2010-12-23 17:26:10 -05:00
< / pre > < pre class = "idle" > < span class = "Storage" > var< / span > bitlist, kids, singers, song;
2011-09-20 23:42:19 -04:00
2009-12-26 02:17:34 -05:00
song < span class = "Keyword" > =< / span > [< span class = "String" > < span class = "String" > " < / span > do< span class = "String" > " < / span > < / span > , < span class = "String" > < span class = "String" > " < / span > re< span class = "String" > " < / span > < / span > , < span class = "String" > < span class = "String" > " < / span > mi< span class = "String" > " < / span > < / span > , < span class = "String" > < span class = "String" > " < / span > fa< span class = "String" > " < / span > < / span > , < span class = "String" > < span class = "String" > " < / span > so< span class = "String" > " < / span > < / span > ];
2011-09-20 23:42:19 -04:00
2010-07-29 00:51:35 -04:00
singers < span class = "Keyword" > =< / span > {
2010-08-04 23:14:34 -04:00
Jagger: < span class = "String" > < span class = "String" > " < / span > Rock< span class = "String" > " < / span > < / span > ,
Elvis: < span class = "String" > < span class = "String" > " < / span > Roll< span class = "String" > " < / span > < / span >
2010-07-29 00:51:35 -04:00
};
2011-09-20 23:42:19 -04:00
2010-12-23 17:26:10 -05:00
bitlist < span class = "Keyword" > =< / span > [< span class = "Number" > 1< / span > , < span class = "Number" > 0< / span > , < span class = "Number" > 1< / span > , < span class = "Number" > 0< / span > , < span class = "Number" > 0< / span > , < span class = "Number" > 1< / span > , < span class = "Number" > 1< / span > , < span class = "Number" > 1< / span > , < span class = "Number" > 0< / span > ];
2011-09-20 23:42:19 -04:00
2010-08-03 01:13:31 -04:00
kids < span class = "Keyword" > =< / span > {
brother: {
2010-08-04 23:14:34 -04:00
name: < span class = "String" > < span class = "String" > " < / span > Max< span class = "String" > " < / span > < / span > ,
2010-08-03 01:13:31 -04:00
age: < span class = "Number" > 11< / span >
},
sister: {
2010-08-04 23:14:34 -04:00
name: < span class = "String" > < span class = "String" > " < / span > Ida< span class = "String" > " < / span > < / span > ,
2010-08-03 01:13:31 -04:00
age: < span class = "Number" > 9< / span >
}
};
2010-12-23 17:26:10 -05:00
< / pre > < script > window . example4 = "song = [\"do\", \"re\", \"mi\", \"fa\", \"so\"]\n\nsingers = {Jagger: \"Rock\", Elvis: \"Roll\"}\n\nbitlist = [\n 1, 0, 1\n 0, 0, 1\n 1, 1, 0\n]\n\nkids =\n brother:\n name: \"Max\"\n age: 11\n sister:\n name: \"Ida\"\n age: 9\n\nalert song.join(\" ... \")" < / script > < div class = 'minibutton load' onclick = 'javascript: loadConsole(example4);' > load< / div > < div class = 'minibutton ok' onclick = 'javascript: var bitlist , kids , singers , song ;
2011-09-20 23:42:19 -04:00
2009-12-26 02:17:34 -05:00
song = ["do", "re", "mi", "fa", "so"];
2011-09-20 23:42:19 -04:00
2010-07-29 00:51:35 -04:00
singers = {
2010-08-04 23:14:34 -04:00
Jagger: "Rock",
Elvis: "Roll"
2010-07-29 00:51:35 -04:00
};
2011-09-20 23:42:19 -04:00
2010-12-23 17:26:10 -05:00
bitlist = [1, 0, 1, 0, 0, 1, 1, 1, 0];
2011-09-20 23:42:19 -04:00
2010-08-03 01:13:31 -04:00
kids = {
brother: {
2010-08-04 23:14:34 -04:00
name: "Max",
2010-08-03 01:13:31 -04:00
age: 11
},
sister: {
2010-08-04 23:14:34 -04:00
name: "Ida",
2010-08-03 01:13:31 -04:00
age: 9
}
2011-09-20 23:42:19 -04:00
};
;alert(song.join(" ... "));'>run: song.join(" ... ")< / div > < br class = 'clear' / > < / div >
2010-06-28 01:00:53 -04:00
< p >
In JavaScript, you can't use reserved words, like < tt > class< / tt > , as properties
2010-11-21 19:53:31 -05:00
of an object, without quoting them as strings. CoffeeScript notices reserved words
2010-11-21 21:12:59 -05:00
used as keys in objects and quotes them for you, so you don't have to worry
2010-11-21 19:53:31 -05:00
about it (say, when using jQuery).
2010-06-28 01:00:53 -04:00
< / p >
2010-11-21 12:38:27 -05:00
< div class = 'code' > < pre class = "idle" > $(< span class = "String" > < span class = "String" > '< / span > .account< span class = "String" > '< / span > < / span > ).attr class: < span class = "String" > < span class = "String" > '< / span > active< span class = "String" > '< / span > < / span >
2010-11-21 19:53:31 -05:00
2010-12-12 21:41:04 -05:00
log object.< span class = "Storage" > class< / span >
2011-09-20 23:42:19 -04:00
< / pre > < pre class = "idle" >
< span class = "Keyword" > $< / span > (< span class = "String" > < span class = "String" > '< / span > .account< span class = "String" > '< / span > < / span > ).attr({
2010-08-03 00:06:34 -04:00
< span class = "String" > < span class = "String" > " < / span > class< span class = "String" > " < / span > < / span > : < span class = "String" > < span class = "String" > '< / span > active< span class = "String" > '< / span > < / span >
2010-06-28 01:00:53 -04:00
});
2011-09-20 23:42:19 -04:00
2010-11-21 19:53:31 -05:00
< span class = "LibraryFunction" > log< / span > (object[< span class = "String" > < span class = "String" > " < / span > class< span class = "String" > " < / span > < / span > ]);
2010-12-23 16:44:12 -05:00
< / pre > < script > window . example5 = "$('.account').attr class: 'active'\n\nlog object.class\n\n\n" < / script > < div class = 'minibutton load' onclick = 'javascript: loadConsole(example5);' > load< / div > < br class = 'clear' / > < / div >
2009-12-23 19:42:18 -05:00
2010-02-21 14:30:21 -05:00
< p >
< span id = "lexical_scope" class = "bookmark" > < / span >
2009-12-23 19:42:18 -05:00
< b class = "header" > Lexical Scoping and Variable Safety< / b >
The CoffeeScript compiler takes care to make sure that all of your variables
2009-12-24 02:01:39 -05: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 >
2010-07-29 00:51:35 -04:00
< div class = 'code' > < pre class = "idle" > outer < span class = "Keyword" > =< / span > < span class = "Number" > 1< / span >
2011-07-12 13:13:46 -04:00
< span class = "FunctionName" > changeNumbers < / span > < span class = "Keyword" > =< / span > < span class = "Keyword" > -< / span > < span class = "Keyword" > > < / span >
2010-07-29 00:51:35 -04:00
inner < span class = "Keyword" > =< / span > < span class = "Keyword" > -< / span > < span class = "Number" > 1< / span >
outer < span class = "Keyword" > =< / span > < span class = "Number" > 10< / span >
inner < span class = "Keyword" > =< / span > changeNumbers()
2010-06-28 00:19:58 -04:00
< / pre > < pre class = "idle" > < span class = "Storage" > var< / span > changeNumbers, inner, outer;
2011-09-20 23:42:19 -04:00
2010-06-28 00:19:58 -04:00
outer < span class = "Keyword" > =< / span > < span class = "Number" > 1< / span > ;
2011-09-20 23:42:19 -04:00
2010-06-28 00:19:58 -04:00
< span class = "FunctionName" > changeNumbers< / span > = < span class = "Storage" > function< / span > () {
< span class = "Storage" > var< / span > inner;
inner < span class = "Keyword" > =< / span > < span class = "Keyword" > -< / span > < span class = "Number" > 1< / span > ;
2010-10-24 21:50:34 -04:00
< span class = "Keyword" > return< / span > outer < span class = "Keyword" > =< / span > < span class = "Number" > 10< / span > ;
2009-12-21 11:41:45 -05:00
};
2011-09-20 23:42:19 -04:00
2010-06-28 00:19:58 -04:00
inner < span class = "Keyword" > =< / span > changeNumbers();
2010-12-23 16:44:12 -05:00
< / pre > < script > window . example6 = "outer = 1\nchangeNumbers = ->\n inner = -1\n outer = 10\ninner = changeNumbers()\n\nalert inner" < / script > < div class = 'minibutton load' onclick = 'javascript: loadConsole(example6);' > load< / div > < div class = 'minibutton ok' onclick = 'javascript: var changeNumbers , inner , outer ;
2011-09-20 23:42:19 -04:00
2010-06-28 00:19:58 -04:00
outer = 1;
2011-09-20 23:42:19 -04:00
2010-06-28 00:19:58 -04:00
changeNumbers = function() {
var inner;
inner = -1;
2010-10-24 21:50:34 -04:00
return outer = 10;
2009-12-21 11:41:45 -05:00
};
2011-09-20 23:42:19 -04:00
inner = changeNumbers();
;alert(inner);'>run: inner< / div > < br class = 'clear' / > < / div >
2009-12-23 19:42:18 -05:00
< p >
2010-08-04 23:14:34 -04:00
Notice how all of the variable declarations have been pushed up to
2010-01-04 22:19:45 -05:00
the top of the closest scope, the first time they appear.
2010-07-23 12:34:54 -04:00
< b > outer< / b > is not redeclared within the inner function, because it's
already in scope; < b > inner< / b > within the function, on the other hand,
2010-01-04 22:19:45 -05:00
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 >
2010-03-08 19:27:38 -05:00
< p >
This behavior is effectively identical to Ruby's scope for local variables.
2010-03-23 00:18:50 -04:00
Because you don't have direct access to the < tt > var< / tt > keyword,
it's impossible to shadow an outer variable on purpose, you may only refer
to it. So be careful that you're not reusing the name of an external
2010-03-08 19:27:38 -05:00
variable accidentally, if you're writing a deeply nested function.
< / p >
2009-12-23 19:42:18 -05:00
< p >
2009-12-24 02:01:39 -05: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 >
2010-08-03 01:13:31 -04:00
object in CommonJS. The < b > existential operator< / b > (covered below), gives you a
2011-07-09 01:26:30 -04:00
reliable way to figure out where to add them; if you're targeting both
2010-11-21 19:53:31 -05:00
CommonJS and the browser: < tt > exports ? this< / tt >
2009-12-23 19:42:18 -05:00
< / p >
2010-02-21 14:30:21 -05:00
< p >
< span id = "conditionals" class = "bookmark" > < / span >
2010-07-16 22:44:07 -04:00
< b class = "header" > If, Else, Unless, 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 04:38:32 -05:00
< p >
2010-07-16 22:44:07 -04:00
CoffeeScript can compile < b > if< / b > 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
2010-11-21 19:53:31 -05:00
a regular < b > if< / b > statement on a single line.
2009-12-24 04:38:32 -05:00
< / p >
2010-07-29 00:51:35 -04:00
< div class = 'code' > < pre class = "idle" > mood < span class = "Keyword" > =< / span > greatlyImproved < span class = "Keyword" > if< / span > singing
2009-12-21 11:41:45 -05:00
2010-06-28 00:19:58 -04:00
< span class = "Keyword" > if< / span > happy < span class = "Keyword" > and< / span > knowsIt
clapsHands()
chaChaCha()
2010-07-29 00:51:35 -04:00
< span class = "Keyword" > else< / span >
showIt()
2009-12-21 11:41:45 -05:00
2010-07-29 00:51:35 -04:00
date < span class = "Keyword" > =< / span > < span class = "Keyword" > if< / span > friday < span class = "Keyword" > then< / span > sue < span class = "Keyword" > else< / span > jill
2009-12-21 11:41:45 -05:00
2010-11-21 12:38:27 -05:00
options < span class = "Keyword" > or=< / span > defaults
2010-12-12 21:41:04 -05:00
2010-11-21 12:38:27 -05:00
< / pre > < pre class = "idle" > < span class = "Storage" > var< / span > date, mood;
2011-09-20 23:42:19 -04:00
2011-08-14 16:24:26 -04:00
< span class = "Keyword" > if< / span > (singing) mood < span class = "Keyword" > =< / span > greatlyImproved;
2011-09-20 23:42:19 -04:00
2010-06-28 00:19:58 -04:00
< span class = "Keyword" > if< / span > (happy < span class = "Keyword" > & < / span > < span class = "Keyword" > & < / span > knowsIt) {
clapsHands();
chaChaCha();
2010-07-29 00:51:35 -04:00
} < span class = "Keyword" > else< / span > {
showIt();
2009-12-21 11:41:45 -05:00
}
2011-09-20 23:42:19 -04:00
2009-12-26 02:17:34 -05:00
date < span class = "Keyword" > =< / span > friday ? sue : jill;
2011-09-20 23:42:19 -04:00
2010-08-23 22:08:33 -04:00
options < span class = "Keyword" > ||< / span > (options < span class = "Keyword" > =< / span > defaults);
2010-12-23 16:44:12 -05:00
< / pre > < script > window . example7 = "mood = greatlyImproved if singing\n\nif happy and knowsIt\n clapsHands()\n chaChaCha()\nelse\n showIt()\n\ndate = if friday then sue else jill\n\noptions or= defaults\n\n\n" < / script > < div class = 'minibutton load' onclick = 'javascript: loadConsole(example7);' > load< / div > < br class = 'clear' / > < / div >
2009-12-23 19:42:18 -05:00
2010-02-21 14:30:21 -05:00
< p >
< span id = "splats" class = "bookmark" > < / span >
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 >
2010-07-29 00:51:35 -04:00
< div class = 'code' > < pre class = "idle" > gold < span class = "Keyword" > =< / span > silver < span class = "Keyword" > =< / span > rest < span class = "Keyword" > =< / span > < span class = "String" > < span class = "String" > " < / span > unknown< span class = "String" > " < / span > < / span >
2010-01-04 23:26:27 -05:00
2011-07-12 13:13:46 -04:00
< span class = "FunctionName" > awardMedals < / span > < span class = "Keyword" > =< / span > < span class = "FunctionArgument" > (first, second, others...)< / span > < span class = "Storage" > -> < / span >
2010-07-29 00:51:35 -04:00
gold < span class = "Keyword" > =< / span > first
silver < span class = "Keyword" > =< / span > second
rest < span class = "Keyword" > =< / span > others
2010-01-04 23:26:27 -05:00
2010-07-29 00:51:35 -04:00
contenders < span class = "Keyword" > =< / span > [
2010-01-04 23:26:27 -05:00
< span class = "String" > < span class = "String" > " < / span > Michael Phelps< span class = "String" > " < / span > < / span >
< span class = "String" > < span class = "String" > " < / span > Liu Xiang< span class = "String" > " < / span > < / span >
< span class = "String" > < span class = "String" > " < / span > Yao Ming< span class = "String" > " < / span > < / span >
< span class = "String" > < span class = "String" > " < / span > Allyson Felix< span class = "String" > " < / span > < / span >
< span class = "String" > < span class = "String" > " < / span > Shawn Johnson< span class = "String" > " < / span > < / span >
< span class = "String" > < span class = "String" > " < / span > Roman Sebrle< span class = "String" > " < / span > < / span >
< span class = "String" > < span class = "String" > " < / span > Guo Jingjing< span class = "String" > " < / span > < / span >
< span class = "String" > < span class = "String" > " < / span > Tyson Gay< span class = "String" > " < / span > < / span >
< span class = "String" > < span class = "String" > " < / span > Asafa Powell< span class = "String" > " < / span > < / span >
< span class = "String" > < span class = "String" > " < / span > Usain Bolt< span class = "String" > " < / span > < / span >
]
2010-06-28 00:19:58 -04:00
awardMedals contenders...
2010-01-04 23:26:27 -05:00
2010-01-25 00:14:00 -05:00
alert < span class = "String" > < span class = "String" > " < / span > Gold: < span class = "String" > " < / span > < / span > < span class = "Keyword" > +< / span > gold
alert < span class = "String" > < span class = "String" > " < / span > Silver: < span class = "String" > " < / span > < / span > < span class = "Keyword" > +< / span > silver
2010-06-28 00:19:58 -04:00
alert < span class = "String" > < span class = "String" > " < / span > The Field: < span class = "String" > " < / span > < / span > < span class = "Keyword" > +< / span > rest
2010-12-12 21:41:04 -05:00
2010-06-28 00:19:58 -04:00
< / pre > < pre class = "idle" > < span class = "Storage" > var< / span > awardMedals, contenders, gold, rest, silver;
2010-04-03 20:43:50 -04:00
< span class = "Storage" > var< / span > __slice < span class = "Keyword" > =< / span > < span class = "LibraryClassType" > Array< / span > .< span class = "LibraryConstant" > prototype< / span > .slice;
2011-09-20 23:42:19 -04:00
2010-10-24 21:50:34 -04:00
gold < span class = "Keyword" > =< / span > silver < span class = "Keyword" > =< / span > rest < span class = "Keyword" > =< / span > < span class = "String" > < span class = "String" > " < / span > unknown< span class = "String" > " < / span > < / span > ;
2011-09-20 23:42:19 -04:00
2010-11-21 12:38:27 -05:00
< span class = "FunctionName" > awardMedals< / span > = < span class = "Storage" > function< / span > () {
< span class = "Storage" > var< / span > first, others, second;
first < span class = "Keyword" > =< / span > arguments[< span class = "Number" > 0< / span > ], second < span class = "Keyword" > =< / span > arguments[< span class = "Number" > 1< / span > ], others < span class = "Keyword" > =< / span > < span class = "Number" > 3< / span > < span class = "Keyword" > < =< / span > arguments.< span class = "LibraryConstant" > length< / span > ? __slice.< span class = "LibraryFunction" > call< / span > (arguments, < span class = "Number" > 2< / span > ) : [];
2010-01-04 23:26:27 -05:00
gold < span class = "Keyword" > =< / span > first;
silver < span class = "Keyword" > =< / span > second;
2010-10-24 21:50:34 -04:00
< span class = "Keyword" > return< / span > rest < span class = "Keyword" > =< / span > others;
2010-01-04 23:26:27 -05:00
};
2011-09-20 23:42:19 -04:00
2010-01-04 23:26:27 -05:00
contenders < span class = "Keyword" > =< / span > [< span class = "String" > < span class = "String" > " < / span > Michael Phelps< span class = "String" > " < / span > < / span > , < span class = "String" > < span class = "String" > " < / span > Liu Xiang< span class = "String" > " < / span > < / span > , < span class = "String" > < span class = "String" > " < / span > Yao Ming< span class = "String" > " < / span > < / span > , < span class = "String" > < span class = "String" > " < / span > Allyson Felix< span class = "String" > " < / span > < / span > , < span class = "String" > < span class = "String" > " < / span > Shawn Johnson< span class = "String" > " < / span > < / span > , < span class = "String" > < span class = "String" > " < / span > Roman Sebrle< span class = "String" > " < / span > < / span > , < span class = "String" > < span class = "String" > " < / span > Guo Jingjing< span class = "String" > " < / span > < / span > , < span class = "String" > < span class = "String" > " < / span > Tyson Gay< span class = "String" > " < / span > < / span > , < span class = "String" > < span class = "String" > " < / span > Asafa Powell< span class = "String" > " < / span > < / span > , < span class = "String" > < span class = "String" > " < / span > Usain Bolt< span class = "String" > " < / span > < / span > ];
2011-09-20 23:42:19 -04:00
2010-12-01 14:17:10 -05:00
awardMedals.< span class = "LibraryFunction" > apply< / span > (< span class = "BuiltInConstant" > null< / span > , contenders);
2011-09-20 23:42:19 -04:00
2010-01-04 23:26:27 -05:00
< span class = "LibraryFunction" > alert< / span > (< span class = "String" > < span class = "String" > " < / span > Gold: < span class = "String" > " < / span > < / span > < span class = "Keyword" > +< / span > gold);
2011-09-20 23:42:19 -04:00
2010-01-04 23:26:27 -05:00
< span class = "LibraryFunction" > alert< / span > (< span class = "String" > < span class = "String" > " < / span > Silver: < span class = "String" > " < / span > < / span > < span class = "Keyword" > +< / span > silver);
2011-09-20 23:42:19 -04:00
2010-06-28 00:19:58 -04:00
< span class = "LibraryFunction" > alert< / span > (< span class = "String" > < span class = "String" > " < / span > The Field: < span class = "String" > " < / span > < / span > < span class = "Keyword" > +< / span > rest);
2010-12-23 16:44:12 -05:00
< / pre > < script > window . example8 = "gold = silver = rest = \"unknown\"\n\nawardMedals = (first, second, others...) ->\n gold = first\n silver = second\n rest = others\n\ncontenders = [\n \"Michael Phelps\"\n \"Liu Xiang\"\n \"Yao Ming\"\n \"Allyson Felix\"\n \"Shawn Johnson\"\n \"Roman Sebrle\"\n \"Guo Jingjing\"\n \"Tyson Gay\"\n \"Asafa Powell\"\n \"Usain Bolt\"\n]\n\nawardMedals contenders...\n\nalert \"Gold: \" + gold\nalert \"Silver: \" + silver\nalert \"The Field: \" + rest\n\n\n" < / script > < div class = 'minibutton load' onclick = 'javascript: loadConsole(example8);' > load< / div > < div class = 'minibutton ok' onclick = 'javascript: var awardMedals , contenders , gold , rest , silver ;
2010-04-03 20:43:50 -04:00
var __slice = Array.prototype.slice;
2011-09-20 23:42:19 -04:00
2010-10-24 21:50:34 -04:00
gold = silver = rest = "unknown";
2011-09-20 23:42:19 -04:00
2010-11-21 12:38:27 -05:00
awardMedals = function() {
var first, others, second;
first = arguments[0], second = arguments[1], others = 3 < = arguments.length ? __slice.call(arguments, 2) : [];
2010-01-04 23:26:27 -05:00
gold = first;
silver = second;
2010-10-24 21:50:34 -04:00
return rest = others;
2010-01-04 23:26:27 -05:00
};
2011-09-20 23:42:19 -04:00
2010-01-04 23:26:27 -05:00
contenders = ["Michael Phelps", "Liu Xiang", "Yao Ming", "Allyson Felix", "Shawn Johnson", "Roman Sebrle", "Guo Jingjing", "Tyson Gay", "Asafa Powell", "Usain Bolt"];
2011-09-20 23:42:19 -04:00
2010-12-01 14:17:10 -05:00
awardMedals.apply(null, contenders);
2011-09-20 23:42:19 -04:00
2010-01-04 23:26:27 -05:00
alert("Gold: " + gold);
2011-09-20 23:42:19 -04:00
2010-01-04 23:26:27 -05:00
alert("Silver: " + silver);
2011-09-20 23:42:19 -04:00
alert("The Field: " + rest);
;'>run< / div > < br class = 'clear' / > < / div >
2010-01-04 23:26:27 -05:00
2010-02-21 14:30:21 -05:00
< p >
2010-12-21 22:24:24 -05:00
< span id = "loops" class = "bookmark" > < / span >
2010-12-24 14:02:10 -05:00
< b class = "header" > Loops and Comprehensions< / b >
Most of the loops you'll write in CoffeeScript will be < b > comprehensions< / b >
2010-12-23 01:43:13 -05:00
over arrays, objects, and ranges. Comprehensions replace (and compile into)
2010-12-21 22:24:24 -05:00
< b > for< / b > loops, with optional guard clauses and the value of the current array index.
2009-12-23 19:42:18 -05:00
Unlike for loops, array comprehensions are expressions, and can be returned
2011-01-30 17:10:50 -05:00
and assigned.
2009-12-23 19:42:18 -05:00
< / p >
2009-12-31 13:01:10 -05:00
< div class = 'code' > < pre class = "idle" > < span class = "Comment" > < span class = "Comment" > #< / span > Eat lunch.< / span >
2010-12-05 15:29:28 -05:00
eat food < span class = "Keyword" > for< / span > food < span class = "Keyword" > in< / span > [< span class = "String" > < span class = "String" > '< / span > toast< span class = "String" > '< / span > < / span > , < span class = "String" > < span class = "String" > '< / span > cheese< span class = "String" > '< / span > < / span > , < span class = "String" > < span class = "String" > '< / span > wine< span class = "String" > '< / span > < / span > ]
2011-09-20 23:42:19 -04:00
< span class = "Comment" > < span class = "Comment" > #< / span > Fine dining< / span >
courses < span class = "Keyword" > =< / span > [< span class = "String" > < span class = "String" > '< / span > salad< span class = "String" > '< / span > < / span > , < span class = "String" > < span class = "String" > '< / span > entree< span class = "String" > '< / span > < / span > , < span class = "String" > < span class = "String" > '< / span > dessert< span class = "String" > '< / span > < / span > ]
menu course < span class = "Keyword" > +< / span > < span class = "Number" > 1< / span > , dish < span class = "Keyword" > for< / span > dish, course < span class = "Keyword" > in< / span > courses
< span class = "Comment" > < span class = "Comment" > #< / span > Health conscious meal< / span >
foods < span class = "Keyword" > =< / span > [< span class = "String" > < span class = "String" > '< / span > broccoli< span class = "String" > '< / span > < / span > , < span class = "String" > < span class = "String" > '< / span > spinach< span class = "String" > '< / span > < / span > , < span class = "String" > < span class = "String" > '< / span > chocolate< span class = "String" > '< / span > < / span > ]
eat food < span class = "Keyword" > for< / span > food < span class = "Keyword" > in< / span > foods < span class = "Keyword" > when< / span > food < span class = "Keyword" > isnt< / span > < span class = "String" > < span class = "String" > '< / span > chocolate< span class = "String" > '< / span > < / span >
< / pre > < pre class = "idle" > < span class = "Storage" > var< / span > course, courses, dish, food, foods, _i, _j, _len, _len2, _len3, _ref;
2010-10-24 21:50:34 -04:00
_ref < span class = "Keyword" > =< / span > [< span class = "String" > < span class = "String" > '< / span > toast< span class = "String" > '< / span > < / span > , < span class = "String" > < span class = "String" > '< / span > cheese< span class = "String" > '< / span > < / span > , < span class = "String" > < span class = "String" > '< / span > wine< span class = "String" > '< / span > < / span > ];
< span class = "Keyword" > for< / span > (_i < span class = "Keyword" > =< / span > < span class = "Number" > 0< / span > , _len < span class = "Keyword" > =< / span > _ref.< span class = "LibraryConstant" > length< / span > ; _i < span class = "Keyword" > < < / span > _len; _i< span class = "Keyword" > ++< / span > ) {
food < span class = "Keyword" > =< / span > _ref[_i];
2010-12-05 15:29:28 -05:00
eat(food);
2009-12-21 11:41:45 -05:00
}
2011-09-20 23:42:19 -04:00
courses < span class = "Keyword" > =< / span > [< span class = "String" > < span class = "String" > '< / span > salad< span class = "String" > '< / span > < / span > , < span class = "String" > < span class = "String" > '< / span > entree< span class = "String" > '< / span > < / span > , < span class = "String" > < span class = "String" > '< / span > dessert< span class = "String" > '< / span > < / span > ];
< span class = "Keyword" > for< / span > (course < span class = "Keyword" > =< / span > < span class = "Number" > 0< / span > , _len2 < span class = "Keyword" > =< / span > courses.< span class = "LibraryConstant" > length< / span > ; course < span class = "Keyword" > < < / span > _len2; course< span class = "Keyword" > ++< / span > ) {
dish < span class = "Keyword" > =< / span > courses[course];
menu(course < span class = "Keyword" > +< / span > < span class = "Number" > 1< / span > , dish);
}
foods < span class = "Keyword" > =< / span > [< span class = "String" > < span class = "String" > '< / span > broccoli< span class = "String" > '< / span > < / span > , < span class = "String" > < span class = "String" > '< / span > spinach< span class = "String" > '< / span > < / span > , < span class = "String" > < span class = "String" > '< / span > chocolate< span class = "String" > '< / span > < / span > ];
< span class = "Keyword" > for< / span > (_j < span class = "Keyword" > =< / span > < span class = "Number" > 0< / span > , _len3 < span class = "Keyword" > =< / span > foods.< span class = "LibraryConstant" > length< / span > ; _j < span class = "Keyword" > < < / span > _len3; _j< span class = "Keyword" > ++< / span > ) {
food < span class = "Keyword" > =< / span > foods[_j];
< span class = "Keyword" > if< / span > (food < span class = "Keyword" > !< / span > < span class = "Keyword" > ==< / span > < span class = "String" > < span class = "String" > '< / span > chocolate< span class = "String" > '< / span > < / span > ) eat(food);
}
< / pre > < script > window . example9 = "# Eat lunch.\neat food for food in ['toast', 'cheese', 'wine']\n\n# Fine dining\ncourses = ['salad', 'entree', 'dessert']\nmenu course + 1, dish for dish, course in courses\n\n# Health conscious meal\nfoods = ['broccoli', 'spinach', 'chocolate']\neat food for food in foods when food isnt 'chocolate'\n" < / script > < div class = 'minibutton load' onclick = 'javascript: loadConsole(example9);' > load< / div > < br class = 'clear' / > < / div >
2009-12-26 23:46:31 -05:00
< p >
2011-01-30 17:10:50 -05:00
Comprehensions 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 > :
2011-01-30 19:25:36 -05:00
< tt > shortNames = (name for name in list when name.length < 5)< / tt > < br / >
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-06-28 00:19:58 -04:00
end of your comprehension.
2010-01-10 15:52:23 -05:00
< / p >
2010-11-21 12:38:27 -05:00
< div class = 'code' > < pre class = "idle" > countdown < span class = "Keyword" > =< / span > (num < span class = "Keyword" > for< / span > num < span class = "Keyword" > in< / span > [< span class = "Number" > 10< / span > ..< span class = "Number" > 1< / span > ])
2010-01-10 15:52:23 -05:00
2010-12-15 21:11:43 -05:00
< / pre > < pre class = "idle" > < span class = "Storage" > var< / span > countdown, num;
2011-09-20 23:42:19 -04:00
2010-11-21 21:12:59 -05:00
countdown < span class = "Keyword" > =< / span > (< span class = "Storage" > function< / span > () {
2010-12-15 21:11:43 -05:00
< span class = "Storage" > var< / span > _results;
2010-11-21 12:38:27 -05:00
_results < span class = "Keyword" > =< / span > [];
< span class = "Keyword" > for< / span > (num < span class = "Keyword" > =< / span > < span class = "Number" > 10< / span > ; num < span class = "Keyword" > > =< / span > < span class = "Number" > 1< / span > ; num< span class = "Keyword" > --< / span > ) {
_results.< span class = "LibraryFunction" > push< / span > (num);
2010-01-10 15:52:23 -05:00
}
2010-11-21 12:38:27 -05:00
< span class = "Keyword" > return< / span > _results;
2010-12-23 15:41:42 -05:00
})();
2010-12-24 14:02:10 -05:00
< / pre > < script > window . example10 = "countdown = (num for num in [10..1])\n\nalert countdown" < / script > < div class = 'minibutton load' onclick = 'javascript: loadConsole(example10);' > load< / div > < div class = 'minibutton ok' onclick = 'javascript: var countdown , num ;
2011-09-20 23:42:19 -04:00
2010-11-21 21:12:59 -05:00
countdown = (function() {
2010-12-15 21:11:43 -05:00
var _results;
2010-11-21 12:38:27 -05:00
_results = [];
for (num = 10; num >= 1; num--) {
_results.push(num);
2010-01-10 15:52:23 -05:00
}
2010-11-21 12:38:27 -05:00
return _results;
2011-09-20 23:42:19 -04:00
})();
;alert(countdown);'>run: countdown< / div > < br class = 'clear' / > < / div >
2010-12-05 15:29:28 -05:00
< p >
2010-12-11 19:44:11 -05:00
Note how because we are assigning the value of the comprehensions to a
variable in the example above, CoffeeScript is collecting the result of
2010-12-05 15:29:28 -05:00
each iteration into an array. Sometimes functions end with loops that are
intended to run only for their side-effects. Be careful that you're not
accidentally returning the results of the comprehension in these cases,
2011-07-09 01:26:30 -04:00
by adding a meaningful return value — like < tt > true< / tt > — or < tt > null< / tt > ,
2010-12-05 15:29:28 -05:00
to the bottom of your function.
< / p >
2010-12-18 11:54:40 -05:00
< p >
2010-12-21 20:43:13 -05:00
To step through a range comprehension in fixed-size chunks,
2010-12-18 11:54:40 -05:00
use < tt > by< / tt > , for example:< br / >
< tt > evens = (x for x in [0..10] by 2)< / tt >
< / p >
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 23:46:31 -05:00
< / p >
2010-09-21 23:58:05 -04:00
< div class = 'code' > < pre class = "idle" > yearsOld < span class = "Keyword" > =< / span > max: < span class = "Number" > 10< / span > , ida: < span class = "Number" > 9< / span > , tim: < span class = "Number" > 11< / span >
2010-01-10 15:52:23 -05:00
2010-07-29 00:51:35 -04:00
ages < span class = "Keyword" > =< / span > < span class = "Keyword" > for< / span > child, age < span class = "Keyword" > of< / span > yearsOld
2011-07-09 01:26:30 -04:00
< span class = "String" > < span class = "String" > " < / span > < span class = "String" > < span class = "String" > #{< / span > child< span class = "String" > }< / span > < / span > is < span class = "String" > < span class = "String" > #{< / span > age< span class = "String" > }< / span > < / span > < span class = "String" > " < / span > < / span >
2010-12-15 21:11:43 -05:00
< / pre > < pre class = "idle" > < span class = "Storage" > var< / span > age, ages, child, yearsOld;
2011-09-20 23:42:19 -04:00
2010-06-28 00:19:58 -04:00
yearsOld < span class = "Keyword" > =< / span > {
2010-01-10 15:52:23 -05:00
max: < span class = "Number" > 10< / span > ,
ida: < span class = "Number" > 9< / span > ,
tim: < span class = "Number" > 11< / span >
};
2011-09-20 23:42:19 -04:00
2010-12-23 15:41:42 -05:00
ages < span class = "Keyword" > =< / span > (< span class = "Storage" > function< / span > () {
2010-12-15 21:11:43 -05:00
< span class = "Storage" > var< / span > _results;
2010-11-21 12:38:27 -05:00
_results < span class = "Keyword" > =< / span > [];
2010-10-04 23:21:16 -04:00
< span class = "Keyword" > for< / span > (child < span class = "Keyword" > in< / span > yearsOld) {
age < span class = "Keyword" > =< / span > yearsOld[child];
2011-07-09 01:26:30 -04:00
_results.< span class = "LibraryFunction" > push< / span > (< span class = "String" > < span class = "String" > " < / span > < span class = "String" > " < / span > < / span > < span class = "Keyword" > +< / span > child < span class = "Keyword" > +< / span > < span class = "String" > < span class = "String" > " < / span > is < span class = "String" > " < / span > < / span > < span class = "Keyword" > +< / span > age);
2010-07-17 10:28:06 -04:00
}
2010-11-21 12:38:27 -05:00
< span class = "Keyword" > return< / span > _results;
2010-12-23 15:41:42 -05:00
})();
2011-07-09 01:26:30 -04:00
< / pre > < script > window . example11 = "yearsOld = max: 10, ida: 9, tim: 11\n\nages = for child, age of yearsOld\n \"#{child} is #{age}\"\n\nalert ages.join(\", \")" < / script > < div class = 'minibutton load' onclick = 'javascript: loadConsole(example11);' > load< / div > < div class = 'minibutton ok' onclick = 'javascript: var age , ages , child , yearsOld ;
2011-09-20 23:42:19 -04:00
2010-06-28 00:19:58 -04:00
yearsOld = {
2010-01-10 15:52:23 -05:00
max: 10,
ida: 9,
tim: 11
};
2011-09-20 23:42:19 -04:00
2010-12-23 15:41:42 -05:00
ages = (function() {
2010-12-15 21:11:43 -05:00
var _results;
2010-11-21 12:38:27 -05:00
_results = [];
2010-10-04 23:21:16 -04:00
for (child in yearsOld) {
age = yearsOld[child];
2011-07-09 01:26:30 -04:00
_results.push("" + child + " is " + age);
2010-07-17 10:28:06 -04:00
}
2010-11-21 12:38:27 -05:00
return _results;
2011-09-20 23:42:19 -04:00
})();
;alert(ages.join(", "));'>run: ages.join(", ")< / div > < br class = 'clear' / > < / div >
2010-08-04 23:14:34 -04:00
< p >
2010-12-21 20:43:13 -05:00
If you would like to iterate over just the keys that are defined on the
2010-12-21 18:57:23 -05:00
object itself, by adding a < tt > hasOwnProperty< / tt >
check to avoid properties that may be interited from the prototype, use< br / >
< tt > for own key, value of object< / tt >
2010-08-04 23:14:34 -04:00
< / p >
2010-12-24 14:02:10 -05:00
< p >
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 >
< div class = 'code' > < pre class = "idle" > < span class = "Comment" > < span class = "Comment" > #< / span > Econ 101< / span >
< span class = "Keyword" > if< / span > < span class = "Variable" > this< / span > .studyingEconomics
buy() < span class = "Keyword" > while< / span > supply < span class = "Keyword" > > < / span > demand
sell() < span class = "Keyword" > until< / span > supply < span class = "Keyword" > > < / span > demand
< span class = "Comment" > < span class = "Comment" > #< / span > Nursery Rhyme< / span >
num < span class = "Keyword" > =< / span > < span class = "Number" > 6< / span >
lyrics < span class = "Keyword" > =< / span > < span class = "Keyword" > while< / span > num < span class = "Keyword" > -< / span > < span class = "Keyword" > =< / span > < span class = "Number" > 1< / span >
2011-07-09 01:26:30 -04:00
< span class = "String" > < span class = "String" > " < / span > < span class = "String" > < span class = "String" > #{< / span > num< span class = "String" > }< / span > < / span > little monkeys, jumping on the bed.< / span >
2010-12-24 14:02:10 -05:00
< span class = "String" > One fell out and bumped his head.< span class = "String" > " < / span > < / span >
< / pre > < pre class = "idle" > < span class = "Storage" > var< / span > lyrics, num;
2011-09-20 23:42:19 -04:00
2010-12-24 14:02:10 -05:00
< span class = "Keyword" > if< / span > (< span class = "Variable" > this< / span > .studyingEconomics) {
< span class = "Keyword" > while< / span > (supply < span class = "Keyword" > > < / span > demand) {
buy();
}
< span class = "Keyword" > while< / span > (< span class = "Keyword" > !< / span > (supply < span class = "Keyword" > > < / span > demand)) {
sell();
}
}
2011-09-20 23:42:19 -04:00
2010-12-24 14:02:10 -05:00
num < span class = "Keyword" > =< / span > < span class = "Number" > 6< / span > ;
2011-09-20 23:42:19 -04:00
2010-12-24 14:02:10 -05:00
lyrics < span class = "Keyword" > =< / span > (< span class = "Storage" > function< / span > () {
< span class = "Storage" > var< / span > _results;
_results < span class = "Keyword" > =< / span > [];
< span class = "Keyword" > while< / span > (num < span class = "Keyword" > -< / span > < span class = "Keyword" > =< / span > < span class = "Number" > 1< / span > ) {
2011-07-09 01:26:30 -04:00
_results.< span class = "LibraryFunction" > push< / span > (< span class = "String" > < span class = "String" > " < / span > < span class = "String" > " < / span > < / span > < span class = "Keyword" > +< / span > num < span class = "Keyword" > +< / span > < span class = "String" > < span class = "String" > " < / span > little monkeys, jumping on the bed. One fell out and bumped his head.< span class = "String" > " < / span > < / span > );
2010-12-24 14:02:10 -05:00
}
< span class = "Keyword" > return< / span > _results;
})();
2011-07-09 01:26:30 -04:00
< / pre > < script > window . example12 = "# Econ 101\nif this.studyingEconomics\n buy() while supply > demand\n sell() until supply > demand\n\n# Nursery Rhyme\nnum = 6\nlyrics = while num -= 1\n \"#{num} little monkeys, jumping on the bed.\n One fell out and bumped his head.\"\n\nalert lyrics.join(\"\\n\")" < / script > < div class = 'minibutton load' onclick = 'javascript: loadConsole(example12);' > load< / div > < div class = 'minibutton ok' onclick = 'javascript: var lyrics , num ;
2011-09-20 23:42:19 -04:00
2010-12-24 14:02:10 -05:00
if (this.studyingEconomics) {
while (supply > demand) {
buy();
}
while (!(supply > demand)) {
sell();
}
}
2011-09-20 23:42:19 -04:00
2010-12-24 14:02:10 -05:00
num = 6;
2011-09-20 23:42:19 -04:00
2010-12-24 14:02:10 -05:00
lyrics = (function() {
var _results;
_results = [];
while (num -= 1) {
2011-07-09 01:26:30 -04:00
_results.push("" + num + " little monkeys, jumping on the bed. One fell out and bumped his head.");
2010-12-24 14:02:10 -05:00
}
return _results;
2011-09-20 23:42:19 -04:00
})();
;alert(lyrics.join("\n"));'>run: lyrics.join("\n")< / div > < br class = 'clear' / > < / div >
2010-12-24 14:02:10 -05:00
< p >
For readability, the < b > until< / b > keyword is equivalent to < tt > while not< / tt > ,
and the < b > loop< / b > keyword is equivalent to < tt > while true< / tt > .
< / p >
2010-12-23 01:43:13 -05:00
< p >
2010-12-24 12:22:27 -05:00
When using a JavaScript loop to generate functions, it's common to insert
a closure wrapper in order to ensure that loop variables are closed over,
and all the generated functions don't just share the final values. CoffeeScript
2010-12-24 14:02:10 -05:00
provides the < tt > do< / tt > keyword, which immediately invokes a passed function,
forwarding any arguments.
2010-12-24 12:22:27 -05:00
< / p >
2011-01-30 17:10:50 -05:00
< div class = 'code' > < pre class = "idle" > < span class = "Keyword" > for< / span > filename < span class = "Keyword" > in< / span > list
< span class = "Keyword" > do< / span > < span class = "FunctionArgument" > (filename)< / span > < span class = "Storage" > -> < / span >
fs.readFile filename, < span class = "FunctionArgument" > (err, contents)< / span > < span class = "Storage" > -> < / span >
compile filename, contents.toString()
< / pre > < pre class = "idle" > < span class = "Storage" > var< / span > filename, _fn, _i, _len;
2011-09-20 23:42:19 -04:00
2011-01-30 17:10:50 -05:00
< span class = "FunctionName" > _fn< / span > = < span class = "Storage" > function< / span > (< span class = "FunctionArgument" > filename< / span > ) {
< span class = "Keyword" > return< / span > fs.readFile(filename, < span class = "Storage" > function< / span > (err, contents) {
< span class = "Keyword" > return< / span > < span class = "LibraryFunction" > compile< / span > (filename, contents.< span class = "LibraryFunction" > toString< / span > ());
2010-12-23 01:43:13 -05:00
});
};
2010-12-24 12:22:27 -05:00
< span class = "Keyword" > for< / span > (_i < span class = "Keyword" > =< / span > < span class = "Number" > 0< / span > , _len < span class = "Keyword" > =< / span > list.< span class = "LibraryConstant" > length< / span > ; _i < span class = "Keyword" > < < / span > _len; _i< span class = "Keyword" > ++< / span > ) {
2011-01-30 17:10:50 -05:00
filename < span class = "Keyword" > =< / span > list[_i];
_fn(filename);
2010-12-23 01:43:13 -05:00
}
2011-01-30 17:10:50 -05:00
< / pre > < script > window . example13 = "for filename in list\n do (filename) ->\n fs.readFile filename, (err, contents) ->\n compile filename, contents.toString()" < / script > < div class = 'minibutton load' onclick = 'javascript: loadConsole(example13);' > load< / div > < br class = 'clear' / > < / div >
2010-12-23 01:43:13 -05:00
2010-12-04 12:52:51 -05:00
< p >
< span id = "slices" class = "bookmark" > < / span >
< b class = "header" > Array Slicing and Splicing with Ranges< / b >
Ranges can also be used to extract slices of arrays.
With two dots (< tt > 3..6< / tt > ), the range is inclusive (< tt > 3, 4, 5, 6< / tt > );
2011-05-01 12:08:15 -04:00
with three dots (< tt > 3...6< / tt > ), the range excludes the end (< tt > 3, 4, 5< / tt > ).
2010-12-04 12:52:51 -05:00
< / p >
< div class = 'code' > < pre class = "idle" > numbers < span class = "Keyword" > =< / span > [< span class = "Number" > 0< / span > , < span class = "Number" > 1< / span > , < span class = "Number" > 2< / span > , < span class = "Number" > 3< / span > , < span class = "Number" > 4< / span > , < span class = "Number" > 5< / span > , < span class = "Number" > 6< / span > , < span class = "Number" > 7< / span > , < span class = "Number" > 8< / span > , < span class = "Number" > 9< / span > ]
copy < span class = "Keyword" > =< / span > numbers[< span class = "Number" > 0< / span > ...numbers.length]
middle < span class = "Keyword" > =< / span > copy[< span class = "Number" > 3< / span > ..< span class = "Number" > 6< / span > ]
2010-12-12 21:41:04 -05:00
2010-12-04 12:52:51 -05:00
< / pre > < pre class = "idle" > < span class = "Storage" > var< / span > copy, middle, numbers;
2011-09-20 23:42:19 -04:00
2010-12-04 12:52:51 -05:00
numbers < span class = "Keyword" > =< / span > [< span class = "Number" > 0< / span > , < span class = "Number" > 1< / span > , < span class = "Number" > 2< / span > , < span class = "Number" > 3< / span > , < span class = "Number" > 4< / span > , < span class = "Number" > 5< / span > , < span class = "Number" > 6< / span > , < span class = "Number" > 7< / span > , < span class = "Number" > 8< / span > , < span class = "Number" > 9< / span > ];
2011-09-20 23:42:19 -04:00
2010-12-04 12:52:51 -05:00
copy < span class = "Keyword" > =< / span > numbers.< span class = "LibraryFunction" > slice< / span > (< span class = "Number" > 0< / span > , numbers.< span class = "LibraryConstant" > length< / span > );
2011-09-20 23:42:19 -04:00
2010-12-21 14:25:28 -05:00
middle < span class = "Keyword" > =< / span > copy.< span class = "LibraryFunction" > slice< / span > (< span class = "Number" > 3< / span > , < span class = "Number" > 7< / span > );
2010-12-23 16:44:12 -05:00
< / pre > < script > window . example14 = "numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n\ncopy = numbers[0...numbers.length]\n\nmiddle = copy[3..6]\n\nalert middle" < / script > < div class = 'minibutton load' onclick = 'javascript: loadConsole(example14);' > load< / div > < div class = 'minibutton ok' onclick = 'javascript: var copy , middle , numbers ;
2011-09-20 23:42:19 -04:00
2010-12-04 12:52:51 -05:00
numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
2011-09-20 23:42:19 -04:00
2010-12-04 12:52:51 -05:00
copy = numbers.slice(0, numbers.length);
2011-09-20 23:42:19 -04:00
middle = copy.slice(3, 7);
;alert(middle);'>run: middle< / div > < br class = 'clear' / > < / div >
2010-12-04 12:52:51 -05:00
< p >
The same syntax can be used with assignment to replace a segment of an array
with new values, splicing it.
< / p >
< div class = 'code' > < pre class = "idle" > numbers < span class = "Keyword" > =< / span > [< span class = "Number" > 0< / span > , < span class = "Number" > 1< / span > , < span class = "Number" > 2< / span > , < span class = "Number" > 3< / span > , < span class = "Number" > 4< / span > , < span class = "Number" > 5< / span > , < span class = "Number" > 6< / span > , < span class = "Number" > 7< / span > , < span class = "Number" > 8< / span > , < span class = "Number" > 9< / span > ]
numbers[< span class = "Number" > 3< / span > ..< span class = "Number" > 6< / span > ] < span class = "Keyword" > =< / span > [< span class = "Keyword" > -< / span > < span class = "Number" > 3< / span > , < span class = "Keyword" > -< / span > < span class = "Number" > 4< / span > , < span class = "Keyword" > -< / span > < span class = "Number" > 5< / span > , < span class = "Keyword" > -< / span > < span class = "Number" > 6< / span > ]
2010-12-24 12:22:27 -05:00
< / pre > < pre class = "idle" > < span class = "Storage" > var< / span > numbers, _ref;
2011-09-20 23:42:19 -04:00
2010-12-04 12:52:51 -05:00
numbers < span class = "Keyword" > =< / span > [< span class = "Number" > 0< / span > , < span class = "Number" > 1< / span > , < span class = "Number" > 2< / span > , < span class = "Number" > 3< / span > , < span class = "Number" > 4< / span > , < span class = "Number" > 5< / span > , < span class = "Number" > 6< / span > , < span class = "Number" > 7< / span > , < span class = "Number" > 8< / span > , < span class = "Number" > 9< / span > ];
2011-09-20 23:42:19 -04:00
2010-12-24 12:22:27 -05:00
[].splice.< span class = "LibraryFunction" > apply< / span > (numbers, [< span class = "Number" > 3< / span > , < span class = "Number" > 4< / span > ].< span class = "LibraryFunction" > concat< / span > (_ref < span class = "Keyword" > =< / span > [< span class = "Keyword" > -< / span > < span class = "Number" > 3< / span > , < span class = "Keyword" > -< / span > < span class = "Number" > 4< / span > , < span class = "Keyword" > -< / span > < span class = "Number" > 5< / span > , < span class = "Keyword" > -< / span > < span class = "Number" > 6< / span > ])), _ref;
< / pre > < script > window . example15 = "numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n\nnumbers[3..6] = [-3, -4, -5, -6]\n\nalert numbers" < / script > < div class = 'minibutton load' onclick = 'javascript: loadConsole(example15);' > load< / div > < div class = 'minibutton ok' onclick = 'javascript: var numbers , _ref ;
2011-09-20 23:42:19 -04:00
2010-12-04 12:52:51 -05:00
numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
2011-09-20 23:42:19 -04:00
[].splice.apply(numbers, [3, 4].concat(_ref = [-3, -4, -5, -6])), _ref;
;alert(numbers);'>run: numbers< / div > < br class = 'clear' / > < / div >
2010-12-04 12:52:51 -05:00
< p >
Note that JavaScript strings are immutable, and can't be spliced.
< / p >
2010-02-21 14:30:21 -05:00
< p >
< span id = "expressions" class = "bookmark" > < / span >
2010-01-04 22:51:02 -05:00
< 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
2011-07-09 01:26:30 -04:00
pushed down into each possible branch of execution in the function
2010-01-04 22:51:02 -05:00
below.
< / p >
2011-07-12 13:13:46 -04:00
< div class = 'code' > < pre class = "idle" > < span class = "FunctionName" > grade < / span > < span class = "Keyword" > =< / span > < span class = "FunctionArgument" > (student)< / span > < span class = "Storage" > -> < / span >
2010-06-28 00:19:58 -04:00
< span class = "Keyword" > if< / span > student.excellentWork
2010-01-04 22:51:02 -05:00
< span class = "String" > < span class = "String" > " < / span > A+< span class = "String" > " < / span > < / span >
2010-06-28 00:19:58 -04:00
< span class = "Keyword" > else< / span > < span class = "Keyword" > if< / span > student.okayStuff
< span class = "Keyword" > if< / span > student.triedHard < span class = "Keyword" > then< / span > < span class = "String" > < span class = "String" > " < / span > B< span class = "String" > " < / span > < / span > < span class = "Keyword" > else< / span > < span class = "String" > < span class = "String" > " < / span > B-< span class = "String" > " < / span > < / span >
2010-01-04 22:51:02 -05:00
< span class = "Keyword" > else< / span >
< span class = "String" > < span class = "String" > " < / span > C< span class = "String" > " < / span > < / span >
2010-07-29 00:51:35 -04:00
eldest < span class = "Keyword" > =< / span > < span class = "Keyword" > if< / span > < span class = "Number" > 24< / span > < span class = "Keyword" > > < / span > < span class = "Number" > 21< / span > < span class = "Keyword" > then< / span > < span class = "String" > < span class = "String" > " < / span > Liz< span class = "String" > " < / span > < / span > < span class = "Keyword" > else< / span > < span class = "String" > < span class = "String" > " < / span > Ike< span class = "String" > " < / span > < / span >
2010-01-04 22:51:02 -05:00
< / pre > < pre class = "idle" > < span class = "Storage" > var< / span > eldest, grade;
2011-09-20 23:42:19 -04:00
2010-05-15 01:18:05 -04:00
< span class = "FunctionName" > grade< / span > = < span class = "Storage" > function< / span > (< span class = "FunctionArgument" > student< / span > ) {
2010-11-21 12:38:27 -05:00
< span class = "Keyword" > if< / span > (student.excellentWork) {
< span class = "Keyword" > return< / span > < span class = "String" > < span class = "String" > " < / span > A+< span class = "String" > " < / span > < / span > ;
} < span class = "Keyword" > else< / span > < span class = "Keyword" > if< / span > (student.okayStuff) {
< span class = "Keyword" > if< / span > (student.triedHard) {
< span class = "Keyword" > return< / span > < span class = "String" > < span class = "String" > " < / span > B< span class = "String" > " < / span > < / span > ;
} < span class = "Keyword" > else< / span > {
< span class = "Keyword" > return< / span > < span class = "String" > < span class = "String" > " < / span > B-< span class = "String" > " < / span > < / span > ;
}
} < span class = "Keyword" > else< / span > {
< span class = "Keyword" > return< / span > < span class = "String" > < span class = "String" > " < / span > C< span class = "String" > " < / span > < / span > ;
}
2010-01-04 22:51:02 -05:00
};
2011-09-20 23:42:19 -04:00
2010-01-04 22:51:02 -05:00
eldest < span class = "Keyword" > =< / span > < span class = "Number" > 24< / span > < span class = "Keyword" > > < / span > < span class = "Number" > 21< / span > ? < span class = "String" > < span class = "String" > " < / span > Liz< span class = "String" > " < / span > < / span > : < span class = "String" > < span class = "String" > " < / span > Ike< span class = "String" > " < / span > < / span > ;
2010-12-23 16:44:12 -05:00
< / pre > < script > window . example16 = "grade = (student) ->\n if student.excellentWork\n \"A+\"\n else if student.okayStuff\n if student.triedHard then \"B\" else \"B-\"\n else\n \"C\"\n\neldest = if 24 > 21 then \"Liz\" else \"Ike\"\n\nalert eldest" < / script > < div class = 'minibutton load' onclick = 'javascript: loadConsole(example16);' > load< / div > < div class = 'minibutton ok' onclick = 'javascript: var eldest , grade ;
2011-09-20 23:42:19 -04:00
2010-05-15 01:18:05 -04:00
grade = function(student) {
2010-11-21 12:38:27 -05:00
if (student.excellentWork) {
return "A+";
} else if (student.okayStuff) {
if (student.triedHard) {
return "B";
} else {
return "B-";
}
} else {
return "C";
}
2010-01-04 22:51:02 -05:00
};
2011-09-20 23:42:19 -04:00
eldest = 24 > 21 ? "Liz" : "Ike";
;alert(eldest);'>run: eldest< / div > < br class = 'clear' / > < / div >
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 >
2010-07-29 00:51:35 -04:00
< div class = 'code' > < pre class = "idle" > six < span class = "Keyword" > =< / span > (one < span class = "Keyword" > =< / span > < span class = "Number" > 1< / span > ) < span class = "Keyword" > +< / span > (two < span class = "Keyword" > =< / span > < span class = "Number" > 2< / span > ) < span class = "Keyword" > +< / span > (three < span class = "Keyword" > =< / span > < span class = "Number" > 3< / span > )
2010-12-12 21:41:04 -05:00
2010-01-04 22:51:02 -05:00
< / pre > < pre class = "idle" > < span class = "Storage" > var< / span > one, six, three, two;
2011-09-20 23:42:19 -04:00
2010-03-23 00:18:50 -04:00
six < span class = "Keyword" > =< / span > (one < span class = "Keyword" > =< / span > < span class = "Number" > 1< / span > ) < span class = "Keyword" > +< / span > (two < span class = "Keyword" > =< / span > < span class = "Number" > 2< / span > ) < span class = "Keyword" > +< / span > (three < span class = "Keyword" > =< / span > < span class = "Number" > 3< / span > );
2010-12-23 16:44:12 -05:00
< / pre > < script > window . example17 = "six = (one = 1) + (two = 2) + (three = 3)\n\nalert six" < / script > < div class = 'minibutton load' onclick = 'javascript: loadConsole(example17);' > load< / div > < div class = 'minibutton ok' onclick = 'javascript: var one , six , three , two ;
2011-09-20 23:42:19 -04:00
six = (one = 1) + (two = 2) + (three = 3);
;alert(six);'>run: six< / div > < br class = 'clear' / > < / div >
2010-01-04 22:51:02 -05:00
< 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 >
< div class = 'code' > < pre class = "idle" > < span class = "Comment" > < span class = "Comment" > #< / span > The first ten global properties.< / span >
2010-07-29 00:51:35 -04:00
globals < span class = "Keyword" > =< / span > (name < span class = "Keyword" > for< / span > name < span class = "Keyword" > of< / span > window)[< span class = "Number" > 0< / span > ...< span class = "Number" > 10< / span > ]
2010-12-15 21:11:43 -05:00
< / pre > < pre class = "idle" > < span class = "Storage" > var< / span > globals, name;
2011-09-20 23:42:19 -04:00
2010-12-23 15:41:42 -05:00
globals < span class = "Keyword" > =< / span > ((< span class = "Storage" > function< / span > () {
2010-12-15 21:11:43 -05:00
< span class = "Storage" > var< / span > _results;
2010-11-21 12:38:27 -05:00
_results < span class = "Keyword" > =< / span > [];
2010-11-21 19:53:31 -05:00
< span class = "Keyword" > for< / span > (name < span class = "Keyword" > in< / span > < span class = "LibraryClassType" > window< / span > ) {
2010-11-21 12:38:27 -05:00
_results.< span class = "LibraryFunction" > push< / span > (name);
2010-07-17 10:28:06 -04:00
}
2010-11-21 12:38:27 -05:00
< span class = "Keyword" > return< / span > _results;
2010-12-23 15:41:42 -05:00
})()).< span class = "LibraryFunction" > slice< / span > (< span class = "Number" > 0< / span > , < span class = "Number" > 10< / span > );
2010-12-23 16:44:12 -05:00
< / pre > < script > window . example18 = "# The first ten global properties.\n\nglobals = (name for name of window)[0...10]\n\nalert globals" < / script > < div class = 'minibutton load' onclick = 'javascript: loadConsole(example18);' > load< / div > < div class = 'minibutton ok' onclick = 'javascript: var globals , name ;
2011-09-20 23:42:19 -04:00
2010-12-23 15:41:42 -05:00
globals = ((function() {
2010-12-15 21:11:43 -05:00
var _results;
2010-11-21 12:38:27 -05:00
_results = [];
2010-11-21 19:53:31 -05:00
for (name in window) {
2010-11-21 12:38:27 -05:00
_results.push(name);
2010-07-17 10:28:06 -04:00
}
2010-11-21 12:38:27 -05:00
return _results;
2011-09-20 23:42:19 -04:00
})()).slice(0, 10);
;alert(globals);'>run: globals< / div > < br class = 'clear' / > < / div >
2010-01-04 22:51:02 -05:00
< p >
As well as silly things, like passing a < b > try/catch< / b > statement directly
into a function call:
< / p >
< div class = 'code' > < pre class = "idle" > alert(
< span class = "Keyword" > try< / span >
nonexistent < span class = "Keyword" > /< / span > < span class = "BuiltInConstant" > undefined< / span >
< span class = "Keyword" > catch< / span > error
2011-07-09 01:26:30 -04:00
< span class = "String" > < span class = "String" > " < / span > And the error is ... < span class = "String" > < span class = "String" > #{< / span > error< span class = "String" > }< / span > < / span > < span class = "String" > " < / span > < / span >
2010-01-04 22:51:02 -05:00
)
2010-12-12 21:41:04 -05:00
2011-09-20 23:42:19 -04:00
< / pre > < pre class = "idle" >
< span class = "LibraryFunction" > alert< / span > ((< span class = "Storage" > function< / span > () {
2010-01-04 22:51:02 -05:00
< span class = "Keyword" > try< / span > {
2010-11-21 12:38:27 -05:00
< span class = "Keyword" > return< / span > nonexistent / < span class = "Storage" > void< / span > < span class = "Number" > 0< / span > ;
2010-01-04 22:51:02 -05:00
} < span class = "Keyword" > catch< / span > (error) {
2010-01-17 18:33:31 -05:00
< span class = "Keyword" > return< / span > < span class = "String" > < span class = "String" > " < / span > And the error is ... < span class = "String" > " < / span > < / span > < span class = "Keyword" > +< / span > error;
2010-01-04 22:51:02 -05:00
}
2010-12-23 15:41:42 -05:00
})());
2011-09-20 23:42:19 -04:00
< / pre > < script > window . example19 = "alert(\n try\n nonexistent / undefined\n catch error\n \"And the error is ... #{error}\"\n)\n\n" < / script > < div class = 'minibutton load' onclick = 'javascript: loadConsole(example19);' > load< / div > < div class = 'minibutton ok' onclick = 'javascript:
alert((function() {
2010-01-04 22:51:02 -05:00
try {
2010-11-21 12:38:27 -05:00
return nonexistent / void 0;
2010-01-04 22:51:02 -05:00
} catch (error) {
2010-01-17 18:33:31 -05:00
return "And the error is ... " + error;
2010-01-04 22:51:02 -05:00
}
2011-09-20 23:42:19 -04:00
})());
;'>run< / div > < br class = 'clear' / > < / div >
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-12-23 01:43:13 -05:00
2010-12-21 22:24:24 -05:00
< p >
< span id = "operators" class = "bookmark" > < / span >
< b class = "header" > Operators and 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 >
2011-11-08 18:01:45 -05:00
< tt > unless< / tt > can be used as the inverse of < tt > if< / tt > .
2010-12-21 22:24:24 -05:00
< / p >
< p >
As a shortcut for < tt > this.property< / tt > , you can use < tt > @property< / tt > .
< / p >
< p >
You can use < tt > in< / tt > to test for array presence, and < tt > of< / tt > to
test for JavaScript object-key presence.
< / p >
< p >
All together now:
< / p >
< table class = "definitions" >
< tr > < th > CoffeeScript< / th > < th > JavaScript< / th > < / tr >
< tr > < td > < tt > is< / tt > < / td > < td > < tt > ===< / tt > < / td > < / tr >
< tr > < td > < tt > isnt< / tt > < / td > < td > < tt > !==< / tt > < / td > < / tr >
< tr > < td > < tt > not< / tt > < / td > < td > < tt > !< / tt > < / td > < / tr >
< tr > < td > < tt > and< / tt > < / td > < td > < tt > & & < / tt > < / td > < / tr >
< tr > < td > < tt > or< / tt > < / td > < td > < tt > ||< / tt > < / td > < / tr >
< tr > < td > < tt > true, yes, on< / tt > < / td > < td > < tt > true< / tt > < / td > < / tr >
< tr > < td > < tt > false, no, off< / tt > < / td > < td > < tt > false< / tt > < / td > < / tr >
< tr > < td > < tt > @, this< / tt > < / td > < td > < tt > this< / tt > < / td > < / tr >
< tr > < td > < tt > of< / tt > < / td > < td > < tt > in< / tt > < / td > < / tr >
< tr > < td > < tt > in< / tt > < / td > < td > < i > < small > no JS equivalent< / small > < / i > < / td > < / tr >
< / table >
< div class = 'code' > < pre class = "idle" > launch() < span class = "Keyword" > if< / span > ignition < span class = "Keyword" > is< / span > < span class = "BuiltInConstant" > on< / span >
volume < span class = "Keyword" > =< / span > < span class = "Number" > 10< / span > < span class = "Keyword" > if< / span > band < span class = "Keyword" > isnt< / span > SpinalTap
letTheWildRumpusBegin() < span class = "Keyword" > unless< / span > answer < span class = "Keyword" > is< / span > < span class = "BuiltInConstant" > no< / span >
< span class = "Keyword" > if< / span > car.speed < span class = "Keyword" > < < / span > limit < span class = "Keyword" > then< / span > accelerate()
winner < span class = "Keyword" > =< / span > < span class = "BuiltInConstant" > yes< / span > < span class = "Keyword" > if< / span > pick < span class = "Keyword" > in< / span > [< span class = "Number" > 47< / span > , < span class = "Number" > 92< / span > , < span class = "Number" > 13< / span > ]
2011-07-09 01:26:30 -04:00
print inspect < span class = "String" > < span class = "String" > " < / span > My name is < span class = "String" > < span class = "String" > #{< / span > < span class = "Variable" > @name< / span > < span class = "String" > }< / span > < / span > < span class = "String" > " < / span > < / span >
2010-12-21 22:24:24 -05:00
< / pre > < pre class = "idle" > < span class = "Storage" > var< / span > volume, winner;
2011-09-20 23:42:19 -04:00
2011-08-14 16:24:26 -04:00
< span class = "Keyword" > if< / span > (ignition < span class = "Keyword" > ===< / span > < span class = "BuiltInConstant" > true< / span > ) launch();
2011-09-20 23:42:19 -04:00
2011-08-14 16:24:26 -04:00
< span class = "Keyword" > if< / span > (band < span class = "Keyword" > !< / span > < span class = "Keyword" > ==< / span > SpinalTap) volume < span class = "Keyword" > =< / span > < span class = "Number" > 10< / span > ;
2011-09-20 23:42:19 -04:00
2011-08-14 16:24:26 -04:00
< span class = "Keyword" > if< / span > (answer < span class = "Keyword" > !< / span > < span class = "Keyword" > ==< / span > < span class = "BuiltInConstant" > false< / span > ) letTheWildRumpusBegin();
2011-09-20 23:42:19 -04:00
2011-08-14 16:24:26 -04:00
< span class = "Keyword" > if< / span > (car.speed < span class = "Keyword" > < < / span > limit) accelerate();
2011-09-20 23:42:19 -04:00
2011-08-14 16:24:26 -04:00
< span class = "Keyword" > if< / span > (pick < span class = "Keyword" > ===< / span > < span class = "Number" > 47< / span > < span class = "Keyword" > ||< / span > pick < span class = "Keyword" > ===< / span > < span class = "Number" > 92< / span > < span class = "Keyword" > ||< / span > pick < span class = "Keyword" > ===< / span > < span class = "Number" > 13< / span > ) winner < span class = "Keyword" > =< / span > < span class = "BuiltInConstant" > true< / span > ;
2011-09-20 23:42:19 -04:00
2010-12-21 22:24:24 -05:00
< span class = "LibraryFunction" > print< / span > (inspect(< span class = "String" > < span class = "String" > " < / span > My name is < span class = "String" > " < / span > < / span > < span class = "Keyword" > +< / span > < span class = "Variable" > this< / span > .< span class = "LibraryConstant" > name< / span > ));
2011-07-09 01:26:30 -04:00
< / pre > < script > window . example20 = "launch() if ignition is on\n\nvolume = 10 if band isnt SpinalTap\n\nletTheWildRumpusBegin() unless answer is no\n\nif car.speed < limit then accelerate()\n\nwinner = yes if pick in [47, 92, 13]\n\nprint inspect \"My name is #{@name}\"\n" < / script > < div class = 'minibutton load' onclick = 'javascript: loadConsole(example20);' > load< / div > < br class = 'clear' / > < / div >
2010-02-13 15:25:04 -05:00
2010-02-21 14:30:21 -05:00
< p >
2010-01-26 23:23:59 -05:00
< 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 >
2010-07-29 00:51:35 -04:00
< div class = 'code' > < pre class = "idle" > solipsism < span class = "Keyword" > =< / span > < span class = "BuiltInConstant" > true< / span > < span class = "Keyword" > if< / span > mind< span class = "Keyword" > ?< / span > < span class = "Keyword" > and< / span > < span class = "Keyword" > not< / span > world< span class = "Keyword" > ?< / span >
2010-01-26 23:23:59 -05:00
2010-12-11 19:44:11 -05:00
speed < span class = "Keyword" > ?< / span > < span class = "Keyword" > =< / span > < span class = "Number" > 75< / span >
2010-01-26 23:23:59 -05:00
2010-12-11 19:44:11 -05:00
footprints < span class = "Keyword" > =< / span > yeti < span class = "Keyword" > ?< / span > < span class = "String" > < span class = "String" > " < / span > bear< span class = "String" > " < / span > < / span >
2010-01-26 23:23:59 -05:00
2010-12-11 19:44:11 -05:00
< / pre > < pre class = "idle" > < span class = "Storage" > var< / span > footprints, solipsism;
2011-09-20 23:42:19 -04:00
2011-05-01 12:08:15 -04:00
< span class = "Keyword" > if< / span > ((< span class = "Keyword" > typeof< / span > mind < span class = "Keyword" > !< / span > < span class = "Keyword" > ==< / span > < span class = "String" > < span class = "String" > " < / span > undefined< span class = "String" > " < / span > < / span > < span class = "Keyword" > & < / span > < span class = "Keyword" > & < / span > mind < span class = "Keyword" > !< / span > < span class = "Keyword" > ==< / span > < span class = "BuiltInConstant" > null< / span > ) < span class = "Keyword" > & < / span > < span class = "Keyword" > & < / span > < span class = "Keyword" > !< / span > (< span class = "Keyword" > typeof< / span > world < span class = "Keyword" > !< / span > < span class = "Keyword" > ==< / span > < span class = "String" > < span class = "String" > " < / span > undefined< span class = "String" > " < / span > < / span > < span class = "Keyword" > & < / span > < span class = "Keyword" > & < / span > world < span class = "Keyword" > !< / span > < span class = "Keyword" > ==< / span > < span class = "BuiltInConstant" > null< / span > )) {
2010-01-26 23:23:59 -05:00
solipsism < span class = "Keyword" > =< / span > < span class = "BuiltInConstant" > true< / span > ;
}
2011-09-20 23:42:19 -04:00
2011-08-14 16:24:26 -04:00
< span class = "Keyword" > if< / span > (< span class = "Keyword" > typeof< / span > speed < span class = "Keyword" > ===< / span > < span class = "String" > < span class = "String" > " < / span > undefined< span class = "String" > " < / span > < / span > < span class = "Keyword" > ||< / span > speed < span class = "Keyword" > ===< / span > < span class = "BuiltInConstant" > null< / span > ) speed < span class = "Keyword" > =< / span > < span class = "Number" > 75< / span > ;
2011-09-20 23:42:19 -04:00
2011-05-01 12:08:15 -04:00
footprints < span class = "Keyword" > =< / span > < span class = "Keyword" > typeof< / span > yeti < span class = "Keyword" > !< / span > < span class = "Keyword" > ==< / span > < span class = "String" > < span class = "String" > " < / span > undefined< span class = "String" > " < / span > < / span > < span class = "Keyword" > & < / span > < span class = "Keyword" > & < / span > yeti < span class = "Keyword" > !< / span > < span class = "Keyword" > ==< / span > < span class = "BuiltInConstant" > null< / span > ? yeti : < span class = "String" > < span class = "String" > " < / span > bear< span class = "String" > " < / span > < / span > ;
2010-12-23 16:44:12 -05:00
< / pre > < script > window . example21 = "solipsism = true if mind? and not world?\n\nspeed ?= 75\n\nfootprints = yeti ? \"bear\"\n\nalert footprints" < / script > < div class = 'minibutton load' onclick = 'javascript: loadConsole(example21);' > load< / div > < div class = 'minibutton ok' onclick = 'javascript: var footprints , solipsism ;
2011-09-20 23:42:19 -04:00
2011-05-01 12:08:15 -04:00
if ((typeof mind !== "undefined" & & mind !== null) & & !(typeof world !== "undefined" & & world !== null)) {
2010-01-26 23:23:59 -05:00
solipsism = true;
}
2011-09-20 23:42:19 -04:00
2011-08-14 16:24:26 -04:00
if (typeof speed === "undefined" || speed === null) speed = 75;
2011-09-20 23:42:19 -04:00
footprints = typeof yeti !== "undefined" & & yeti !== null ? yeti : "bear";
;alert(footprints);'>run: footprints< / div > < br class = 'clear' / > < / div >
2010-01-26 23:23:59 -05:00
< 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 >
2010-11-21 12:38:27 -05:00
< div class = 'code' > < pre class = "idle" > zip < span class = "Keyword" > =< / span > lottery.drawWinner< span class = "Keyword" > ?< / span > ().address< span class = "Keyword" > ?< / span > .zipcode
< / pre > < pre class = "idle" > < span class = "Storage" > var< / span > zip, _ref;
2011-09-20 23:42:19 -04:00
2011-05-01 12:08:15 -04:00
zip < span class = "Keyword" > =< / span > < span class = "Keyword" > typeof< / span > lottery.drawWinner < span class = "Keyword" > ===< / span > < span class = "String" > < span class = "String" > " < / span > function< span class = "String" > " < / span > < / span > ? (_ref < span class = "Keyword" > =< / span > lottery.drawWinner().address) < span class = "Keyword" > !< / span > < span class = "Keyword" > =< / span > < span class = "BuiltInConstant" > null< / span > ? _ref.zipcode : < span class = "Storage" > void< / span > < span class = "Number" > 0< / span > : < span class = "Storage" > void< / span > < span class = "Number" > 0< / span > ;
2010-12-23 16:44:12 -05:00
< / pre > < script > window . example22 = "zip = lottery.drawWinner?().address?.zipcode\n" < / script > < div class = 'minibutton load' onclick = 'javascript: loadConsole(example22);' > load< / div > < br class = 'clear' / > < / div >
2010-01-26 23:23:59 -05:00
< 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-21 11:41:45 -05:00
2010-02-21 14:30:21 -05:00
< p >
2010-02-27 20:12:47 -05:00
< span id = "classes" class = "bookmark" > < / span >
< b class = "header" > Classes, Inheritance, and Super< / 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 19:49:23 -05: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 >
2010-02-27 20:12:47 -05:00
Instead of repetitively attaching functions to a prototype, CoffeeScript
provides a basic < tt > class< / tt > structure that allows you to name your class,
set the superclass, assign prototypal properties, and define the constructor,
in a single assignable expression.
2009-12-23 19:42:18 -05:00
< / p >
2010-11-21 21:12:59 -05:00
< p >
2011-05-01 12:08:15 -04:00
Constructor functions are named, to better support helpful stack traces.
2010-11-21 21:12:59 -05:00
< / p >
2010-08-23 22:08:33 -04:00
< div class = 'code' > < pre class = "idle" > < span class = "Storage" > class< / span > < span class = "TypeName" > Animal< / span >
2011-07-12 13:13:46 -04:00
< span class = "FunctionName" > constructor< / span > < span class = "Keyword" > :< / span > < span class = "FunctionArgument" > (@name)< / span > < span class = "Storage" > -> < / span >
2010-07-29 00:51:35 -04:00
2011-07-12 13:13:46 -04:00
< span class = "FunctionName" > move< / span > < span class = "Keyword" > :< / span > < span class = "FunctionArgument" > (meters)< / span > < span class = "Storage" > -> < / span >
2011-07-09 01:26:30 -04:00
alert < span class = "Variable" > @name< / span > < span class = "Keyword" > +< / span > < span class = "String" > < span class = "String" > " < / span > moved < span class = "String" > < span class = "String" > #{< / span > meters< span class = "String" > }< / span > < / span > m.< span class = "String" > " < / span > < / span >
2010-02-13 15:25:04 -05:00
2011-07-12 13:13:46 -04:00
< span class = "Storage" > class< / span > < span class = "TypeName" > Snake< / span > < span class = "InheritedClass" > < span class = "Keyword" > extends< / span > Animal< / span >
< span class = "FunctionName" > move< / span > < span class = "Keyword" > :< / span > < span class = "Keyword" > -< / span > < span class = "Keyword" > > < / span >
2010-02-27 20:12:47 -05:00
alert < span class = "String" > < span class = "String" > " < / span > Slithering...< span class = "String" > " < / span > < / span >
< span class = "Variable" > super< / span > < span class = "Number" > 5< / span >
2010-02-13 15:25:04 -05:00
2011-07-12 13:13:46 -04:00
< span class = "Storage" > class< / span > < span class = "TypeName" > Horse< / span > < span class = "InheritedClass" > < span class = "Keyword" > extends< / span > Animal< / span >
< span class = "FunctionName" > move< / span > < span class = "Keyword" > :< / span > < span class = "Keyword" > -< / span > < span class = "Keyword" > > < / span >
2010-02-27 20:12:47 -05:00
alert < span class = "String" > < span class = "String" > " < / span > Galloping...< span class = "String" > " < / span > < / span >
< span class = "Variable" > super< / span > < span class = "Number" > 45< / span >
2009-12-21 11:41:45 -05:00
2010-07-29 00:51:35 -04:00
sam < span class = "Keyword" > =< / span > < span class = "Keyword" > new< / span > < span class = "TypeName" > Snake< / span > < span class = "String" > < span class = "String" > " < / span > Sammy the Python< span class = "String" > " < / span > < / span >
tom < span class = "Keyword" > =< / span > < span class = "Keyword" > new< / span > < span class = "TypeName" > Horse< / span > < span class = "String" > < span class = "String" > " < / span > Tommy the Palomino< span class = "String" > " < / span > < / span >
2009-12-21 11:41:45 -05:00
sam.move()
tom.move()
2009-12-24 02:01:39 -05:00
2010-02-27 20:12:47 -05:00
< / pre > < pre class = "idle" > < span class = "Storage" > var< / span > Animal, Horse, Snake, sam, tom;
2011-09-20 23:42:19 -04:00
< span class = "Storage" > var< / span > __hasProp < span class = "Keyword" > =< / span > < span class = "LibraryClassType" > Object< / span > .< span class = "LibraryConstant" > prototype< / span > .hasOwnProperty, < span class = "FunctionName" > __extends< / span > = < span class = "Storage" > function< / span > (< span class = "FunctionArgument" > child, parent< / span > ) { < span class = "Keyword" > for< / span > (< span class = "Storage" > var< / span > key < span class = "Keyword" > in< / span > parent) { < span class = "Keyword" > if< / span > (__hasProp.< span class = "LibraryFunction" > call< / span > (parent, key)) child[key] < span class = "Keyword" > =< / span > parent[key]; } < span class = "Storage" > function< / span > < span class = "FunctionName" > ctor< / span > () { < span class = "Variable" > this< / span > .< span class = "LibraryConstant" > constructor< / span > < span class = "Keyword" > =< / span > child; } < span class = "LibraryClassType" > ctor< / span > .< span class = "LibraryConstant" > prototype< / span > = parent.< span class = "LibraryConstant" > prototype< / span > ; < span class = "LibraryClassType" > child< / span > .< span class = "LibraryConstant" > prototype< / span > = < span class = "Keyword" > new< / span > < span class = "TypeName" > ctor< / span > ; child.__super__ < span class = "Keyword" > =< / span > parent.< span class = "LibraryConstant" > prototype< / span > ; < span class = "Keyword" > return< / span > child; };
2010-12-23 15:41:42 -05:00
Animal < span class = "Keyword" > =< / span > (< span class = "Storage" > function< / span > () {
2011-09-20 23:42:19 -04:00
2010-11-21 12:38:27 -05:00
< span class = "Storage" > function< / span > < span class = "FunctionName" > Animal< / span > (< span class = "FunctionArgument" > name< / span > ) {
< span class = "Variable" > this< / span > .< span class = "LibraryConstant" > name< / span > < span class = "Keyword" > =< / span > name;
2010-10-24 21:50:34 -04:00
}
2011-09-20 23:42:19 -04:00
2010-11-21 12:38:27 -05:00
< span class = "LibraryClassType" > Animal< / span > .< span class = "LibraryConstant" > prototype< / span > .< span class = "FunctionName" > move< / span > = < span class = "Storage" > function< / span > (< span class = "FunctionArgument" > meters< / span > ) {
2011-07-09 01:26:30 -04:00
< span class = "Keyword" > return< / span > < span class = "LibraryFunction" > alert< / span > (< span class = "Variable" > this< / span > .< span class = "LibraryConstant" > name< / span > < span class = "Keyword" > +< / span > (< span class = "String" > < span class = "String" > " < / span > moved < span class = "String" > " < / span > < / span > < span class = "Keyword" > +< / span > meters < span class = "Keyword" > +< / span > < span class = "String" > < span class = "String" > " < / span > m.< span class = "String" > " < / span > < / span > ));
2010-11-21 12:38:27 -05:00
};
2011-09-20 23:42:19 -04:00
2010-10-19 21:59:13 -04:00
< span class = "Keyword" > return< / span > Animal;
2011-09-20 23:42:19 -04:00
2010-12-23 15:41:42 -05:00
})();
2011-09-20 23:42:19 -04:00
2010-12-23 15:41:42 -05:00
Snake < span class = "Keyword" > =< / span > (< span class = "Storage" > function< / span > () {
2011-09-20 23:42:19 -04:00
2011-05-10 09:27:19 -04:00
__extends(Snake, Animal);
2011-09-20 23:42:19 -04:00
2010-10-19 21:59:13 -04:00
< span class = "Storage" > function< / span > < span class = "FunctionName" > Snake< / span > () {
2010-11-21 12:38:27 -05:00
Snake.__super__.< span class = "LibraryConstant" > constructor< / span > .< span class = "LibraryFunction" > apply< / span > (< span class = "Variable" > this< / span > , arguments);
2010-10-24 21:50:34 -04:00
}
2011-09-20 23:42:19 -04:00
2010-11-21 12:38:27 -05:00
< span class = "LibraryClassType" > Snake< / span > .< span class = "LibraryConstant" > prototype< / span > .< span class = "FunctionName" > move< / span > = < span class = "Storage" > function< / span > () {
< span class = "LibraryFunction" > alert< / span > (< span class = "String" > < span class = "String" > " < / span > Slithering...< span class = "String" > " < / span > < / span > );
< span class = "Keyword" > return< / span > Snake.__super__.move.< span class = "LibraryFunction" > call< / span > (< span class = "Variable" > this< / span > , < span class = "Number" > 5< / span > );
};
2011-09-20 23:42:19 -04:00
2010-10-19 21:59:13 -04:00
< span class = "Keyword" > return< / span > Snake;
2011-09-20 23:42:19 -04:00
2010-12-23 15:41:42 -05:00
})();
2011-09-20 23:42:19 -04:00
2010-12-23 15:41:42 -05:00
Horse < span class = "Keyword" > =< / span > (< span class = "Storage" > function< / span > () {
2011-09-20 23:42:19 -04:00
2011-05-10 09:27:19 -04:00
__extends(Horse, Animal);
2011-09-20 23:42:19 -04:00
2010-10-19 21:59:13 -04:00
< span class = "Storage" > function< / span > < span class = "FunctionName" > Horse< / span > () {
2010-11-21 12:38:27 -05:00
Horse.__super__.< span class = "LibraryConstant" > constructor< / span > .< span class = "LibraryFunction" > apply< / span > (< span class = "Variable" > this< / span > , arguments);
2010-10-24 21:50:34 -04:00
}
2011-09-20 23:42:19 -04:00
2010-11-21 12:38:27 -05:00
< span class = "LibraryClassType" > Horse< / span > .< span class = "LibraryConstant" > prototype< / span > .< span class = "FunctionName" > move< / span > = < span class = "Storage" > function< / span > () {
< span class = "LibraryFunction" > alert< / span > (< span class = "String" > < span class = "String" > " < / span > Galloping...< span class = "String" > " < / span > < / span > );
< span class = "Keyword" > return< / span > Horse.__super__.move.< span class = "LibraryFunction" > call< / span > (< span class = "Variable" > this< / span > , < span class = "Number" > 45< / span > );
};
2011-09-20 23:42:19 -04:00
2010-10-19 21:59:13 -04:00
< span class = "Keyword" > return< / span > Horse;
2011-09-20 23:42:19 -04:00
2010-12-23 15:41:42 -05:00
})();
2011-09-20 23:42:19 -04:00
2009-12-26 02:17:34 -05:00
sam < span class = "Keyword" > =< / span > < span class = "Keyword" > new< / span > < span class = "TypeName" > Snake< / span > (< span class = "String" > < span class = "String" > " < / span > Sammy the Python< span class = "String" > " < / span > < / span > );
2011-09-20 23:42:19 -04:00
2009-12-26 02:17:34 -05:00
tom < span class = "Keyword" > =< / span > < span class = "Keyword" > new< / span > < span class = "TypeName" > Horse< / span > (< span class = "String" > < span class = "String" > " < / span > Tommy the Palomino< span class = "String" > " < / span > < / span > );
2011-09-20 23:42:19 -04:00
2009-12-21 11:41:45 -05:00
sam.move();
2011-09-20 23:42:19 -04:00
2009-12-21 11:41:45 -05:00
tom.move();
2011-07-09 01:26:30 -04:00
< / pre > < script > window . example23 = "class Animal\n constructor: (@name) ->\n\n move: (meters) ->\n alert @name + \" moved #{meters}m.\"\n\nclass Snake extends Animal\n move: ->\n alert \"Slithering...\"\n super 5\n\nclass Horse extends Animal\n move: ->\n alert \"Galloping...\"\n super 45\n\nsam = new Snake \"Sammy the Python\"\ntom = new Horse \"Tommy the Palomino\"\n\nsam.move()\ntom.move()\n\n\n\n\n" < / script > < div class = 'minibutton load' onclick = 'javascript: loadConsole(example23);' > load< / div > < div class = 'minibutton ok' onclick = 'javascript: var Animal , Horse , Snake , sam , tom ;
2011-09-20 23:42:19 -04:00
var __hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor; child.__super__ = parent.prototype; return child; };
2010-12-23 15:41:42 -05:00
Animal = (function() {
2011-09-20 23:42:19 -04:00
2010-11-21 12:38:27 -05:00
function Animal(name) {
this.name = name;
2010-10-24 21:50:34 -04:00
}
2011-09-20 23:42:19 -04:00
2010-11-21 12:38:27 -05:00
Animal.prototype.move = function(meters) {
2011-07-09 01:26:30 -04:00
return alert(this.name + (" moved " + meters + "m."));
2010-11-21 12:38:27 -05:00
};
2011-09-20 23:42:19 -04:00
2010-10-19 21:59:13 -04:00
return Animal;
2011-09-20 23:42:19 -04:00
2010-12-23 15:41:42 -05:00
})();
2011-09-20 23:42:19 -04:00
2010-12-23 15:41:42 -05:00
Snake = (function() {
2011-09-20 23:42:19 -04:00
2011-05-10 09:27:19 -04:00
__extends(Snake, Animal);
2011-09-20 23:42:19 -04:00
2010-10-19 21:59:13 -04:00
function Snake() {
2010-11-21 12:38:27 -05:00
Snake.__super__.constructor.apply(this, arguments);
2010-10-24 21:50:34 -04:00
}
2011-09-20 23:42:19 -04:00
2010-11-21 12:38:27 -05:00
Snake.prototype.move = function() {
alert("Slithering...");
return Snake.__super__.move.call(this, 5);
};
2011-09-20 23:42:19 -04:00
2010-10-19 21:59:13 -04:00
return Snake;
2011-09-20 23:42:19 -04:00
2010-12-23 15:41:42 -05:00
})();
2011-09-20 23:42:19 -04:00
2010-12-23 15:41:42 -05:00
Horse = (function() {
2011-09-20 23:42:19 -04:00
2011-05-10 09:27:19 -04:00
__extends(Horse, Animal);
2011-09-20 23:42:19 -04:00
2010-10-19 21:59:13 -04:00
function Horse() {
2010-11-21 12:38:27 -05:00
Horse.__super__.constructor.apply(this, arguments);
2010-10-24 21:50:34 -04:00
}
2011-09-20 23:42:19 -04:00
2010-11-21 12:38:27 -05:00
Horse.prototype.move = function() {
alert("Galloping...");
return Horse.__super__.move.call(this, 45);
};
2011-09-20 23:42:19 -04:00
2010-10-19 21:59:13 -04:00
return Horse;
2011-09-20 23:42:19 -04:00
2010-12-23 15:41:42 -05:00
})();
2011-09-20 23:42:19 -04:00
2009-12-26 02:17:34 -05:00
sam = new Snake("Sammy the Python");
2011-09-20 23:42:19 -04:00
2009-12-26 02:17:34 -05:00
tom = new Horse("Tommy the Palomino");
2011-09-20 23:42:19 -04:00
2009-12-21 11:41:45 -05:00
sam.move();
2011-09-20 23:42:19 -04:00
tom.move();
;'>run< / div > < br class = 'clear' / > < / div >
2010-02-26 20:09:49 -05:00
< p >
2010-02-27 20:12:47 -05:00
If structuring your prototypes classically isn't your cup of tea, CoffeeScript
provides a couple of lower-level conveniences. The < tt > extends< / tt > operator
2011-05-01 12:08:15 -04:00
helps with proper prototype setup, and can be used to create an inheritance
chain between any pair of constructor functions; < tt > ::< / tt > gives you
quick access to an object's prototype; and < tt > super()< / tt >
2010-02-27 20:12:47 -05:00
is converted into a call against the immediate ancestor's method of the same name.
2010-02-26 20:09:49 -05:00
< / p >
2011-07-12 13:13:46 -04:00
< div class = 'code' > < pre class = "idle" > < span class = "FunctionName" > String::dasherize < / span > < span class = "Keyword" > =< / span > < span class = "Keyword" > -< / span > < span class = "Keyword" > > < / span >
2010-10-05 21:05:57 -04:00
< span class = "Variable" > this< / span > .replace < span class = "String" > /_/g< / span > , < span class = "String" > < span class = "String" > " < / span > -< span class = "String" > " < / span > < / span >
2010-12-12 21:41:04 -05:00
2011-09-20 23:42:19 -04:00
< / pre > < pre class = "idle" >
< span class = "LibraryClassType" > String< / span > .< span class = "LibraryConstant" > prototype< / span > .< span class = "FunctionName" > dasherize< / span > = < span class = "Storage" > function< / span > () {
2010-02-27 20:12:47 -05:00
< span class = "Keyword" > return< / span > < span class = "Variable" > this< / span > .< span class = "LibraryFunction" > replace< / span > (< span class = "String" > < span class = "String" > /< / span > _< span class = "String" > /< / span > g< / span > , < span class = "String" > < span class = "String" > " < / span > -< span class = "String" > " < / span > < / span > );
};
2011-09-20 23:42:19 -04:00
< / pre > < script > window . example24 = "String::dasherize = ->\n this.replace /_/g, \"-\"\n\nalert \"one_two\".dasherize()" < / script > < div class = 'minibutton load' onclick = 'javascript: loadConsole(example24);' > load< / div > < div class = 'minibutton ok' onclick = 'javascript:
String.prototype.dasherize = function() {
2010-02-27 20:12:47 -05:00
return this.replace(/_/g, "-");
2011-09-20 23:42:19 -04:00
};
;alert("one_two".dasherize());'>run: "one_two".dasherize()< / div > < br class = 'clear' / > < / div >
2010-04-03 20:43:50 -04:00
< p >
2011-07-09 01:26:30 -04:00
Finally, class definitions are blocks of executable code, which make for interesting
2010-11-21 21:12:59 -05:00
metaprogramming possibilities. Because in the context of a class definition,
< tt > this< / tt > is the class object itself (the constructor function), you
can assign static properties by using < br / > < tt > @property: value< / tt > , and call
functions defined in parent classes: < tt > @attr 'title', type: 'text'< / tt >
2010-04-03 20:43:50 -04:00
< / p >
2009-12-21 11:41:45 -05:00
2010-02-21 14:30:21 -05:00
< p >
2010-12-23 16:30:35 -05:00
< span id = "destructuring" class = "bookmark" > < / span >
< b class = "header" > 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 >
2010-07-29 00:51:35 -04:00
< div class = 'code' > < pre class = "idle" > theBait < span class = "Keyword" > =< / span > < span class = "Number" > 1000< / span >
theSwitch < span class = "Keyword" > =< / span > < span class = "Number" > 0< / span >
2010-06-28 00:19:58 -04:00
2010-08-04 23:14:34 -04:00
< span class = "Keyword" > [< / span > theBait, theSwitch< span class = "Keyword" > ] =< / span > [theSwitch, theBait]
2010-12-12 21:41:04 -05:00
2010-12-24 14:02:10 -05:00
2010-11-21 12:38:27 -05:00
< / pre > < pre class = "idle" > < span class = "Storage" > var< / span > theBait, theSwitch, _ref;
2011-09-20 23:42:19 -04:00
2010-06-28 00:19:58 -04:00
theBait < span class = "Keyword" > =< / span > < span class = "Number" > 1000< / span > ;
2011-09-20 23:42:19 -04:00
2010-06-28 00:19:58 -04:00
theSwitch < span class = "Keyword" > =< / span > < span class = "Number" > 0< / span > ;
2011-09-20 23:42:19 -04:00
2010-10-04 23:21:16 -04:00
_ref < span class = "Keyword" > =< / span > [theSwitch, theBait], theBait < span class = "Keyword" > =< / span > _ref[< span class = "Number" > 0< / span > ], theSwitch < span class = "Keyword" > =< / span > _ref[< span class = "Number" > 1< / span > ];
2010-12-23 16:44:12 -05:00
< / pre > < script > window . example25 = "theBait = 1000\ntheSwitch = 0\n\n[theBait, theSwitch] = [theSwitch, theBait]\n\nalert theBait" < / script > < div class = 'minibutton load' onclick = 'javascript: loadConsole(example25);' > load< / div > < div class = 'minibutton ok' onclick = 'javascript: var theBait , theSwitch , _ref ;
2011-09-20 23:42:19 -04:00
2010-06-28 00:19:58 -04:00
theBait = 1000;
2011-09-20 23:42:19 -04:00
2010-06-28 00:19:58 -04:00
theSwitch = 0;
2011-09-20 23:42:19 -04:00
_ref = [theSwitch, theBait], theBait = _ref[0], theSwitch = _ref[1];
;alert(theBait);'>run: theBait< / div > < br class = 'clear' / > < / div >
2010-01-11 22:55:01 -05:00
< p >
But it's also helpful for dealing with functions that return multiple
values.
< / p >
2011-07-12 13:13:46 -04:00
< div class = 'code' > < pre class = "idle" > < span class = "FunctionName" > weatherReport < / span > < span class = "Keyword" > =< / span > < span class = "FunctionArgument" > (location)< / span > < span class = "Storage" > -> < / span >
2010-01-11 22:55:01 -05:00
< span class = "Comment" > < span class = "Comment" > #< / span > Make an Ajax request to fetch the weather...< / span >
[location, < span class = "Number" > 72< / span > , < span class = "String" > < span class = "String" > " < / span > Mostly Sunny< span class = "String" > " < / span > < / span > ]
2010-08-04 23:14:34 -04:00
< span class = "Keyword" > [< / span > city, temp, forecast< span class = "Keyword" > ] =< / span > weatherReport < span class = "String" > < span class = "String" > " < / span > Berkeley, CA< span class = "String" > " < / span > < / span >
2010-10-24 21:50:34 -04:00
2010-11-21 12:38:27 -05:00
< / pre > < pre class = "idle" > < span class = "Storage" > var< / span > city, forecast, temp, weatherReport, _ref;
2011-09-20 23:42:19 -04:00
2010-06-28 00:19:58 -04:00
< span class = "FunctionName" > weatherReport< / span > = < span class = "Storage" > function< / span > (< span class = "FunctionArgument" > location< / span > ) {
2010-01-11 22:55:01 -05:00
< span class = "Keyword" > return< / span > [location, < span class = "Number" > 72< / span > , < span class = "String" > < span class = "String" > " < / span > Mostly Sunny< span class = "String" > " < / span > < / span > ];
};
2011-09-20 23:42:19 -04:00
2010-10-04 23:21:16 -04:00
_ref < span class = "Keyword" > =< / span > weatherReport(< span class = "String" > < span class = "String" > " < / span > Berkeley, CA< span class = "String" > " < / span > < / span > ), city < span class = "Keyword" > =< / span > _ref[< span class = "Number" > 0< / span > ], temp < span class = "Keyword" > =< / span > _ref[< span class = "Number" > 1< / span > ], forecast < span class = "Keyword" > =< / span > _ref[< span class = "Number" > 2< / span > ];
2010-12-23 16:44:12 -05:00
< / pre > < script > window . example26 = "weatherReport = (location) ->\n # Make an Ajax request to fetch the weather...\n [location, 72, \"Mostly Sunny\"]\n\n[city, temp, forecast] = weatherReport \"Berkeley, CA\"\n\nalert forecast" < / script > < div class = 'minibutton load' onclick = 'javascript: loadConsole(example26);' > load< / div > < div class = 'minibutton ok' onclick = 'javascript: var city , forecast , temp , weatherReport , _ref ;
2011-09-20 23:42:19 -04:00
2010-06-28 00:19:58 -04:00
weatherReport = function(location) {
2010-01-11 22:55:01 -05:00
return [location, 72, "Mostly Sunny"];
};
2011-09-20 23:42:19 -04:00
_ref = weatherReport("Berkeley, CA"), city = _ref[0], temp = _ref[1], forecast = _ref[2];
;alert(forecast);'>run: forecast< / div > < br class = 'clear' / > < / div >
2010-01-11 22:55:01 -05:00
< p >
2010-12-23 16:30:35 -05:00
Destructuring assignment 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 >
2010-07-29 00:51:35 -04:00
< div class = 'code' > < pre class = "idle" > futurists < span class = "Keyword" > =< / span >
2010-09-21 23:58:05 -04:00
sculptor: < span class = "String" > < span class = "String" > " < / span > Umberto Boccioni< span class = "String" > " < / span > < / span >
painter: < span class = "String" > < span class = "String" > " < / span > Vladimir Burliuk< span class = "String" > " < / span > < / span >
poet:
name: < span class = "String" > < span class = "String" > " < / span > F.T. Marinetti< span class = "String" > " < / span > < / span >
address: [
2010-01-11 22:55:01 -05:00
< span class = "String" > < span class = "String" > " < / span > Via Roma 42R< span class = "String" > " < / span > < / span >
< span class = "String" > < span class = "String" > " < / span > Bellagio, Italy 22021< span class = "String" > " < / span > < / span >
]
2010-10-05 21:05:57 -04:00
< span class = "Keyword" > {< / span > poet: {name, address: [street, city]}< span class = "Keyword" > } =< / span > futurists
2010-10-24 21:50:34 -04:00
2010-11-21 12:38:27 -05:00
< / pre > < pre class = "idle" > < span class = "Storage" > var< / span > city, futurists, name, street, _ref, _ref2;
2011-09-20 23:42:19 -04:00
2010-01-11 22:55:01 -05:00
futurists < span class = "Keyword" > =< / span > {
sculptor: < span class = "String" > < span class = "String" > " < / span > Umberto Boccioni< span class = "String" > " < / span > < / span > ,
painter: < span class = "String" > < span class = "String" > " < / span > Vladimir Burliuk< span class = "String" > " < / span > < / span > ,
poet: {
name: < span class = "String" > < span class = "String" > " < / span > F.T. Marinetti< span class = "String" > " < / span > < / span > ,
address: [< span class = "String" > < span class = "String" > " < / span > Via Roma 42R< span class = "String" > " < / span > < / span > , < span class = "String" > < span class = "String" > " < / span > Bellagio, Italy 22021< span class = "String" > " < / span > < / span > ]
}
};
2011-09-20 23:42:19 -04:00
_ref < span class = "Keyword" > =< / span > futurists.poet, name < span class = "Keyword" > =< / span > _ref.< span class = "LibraryConstant" > name< / span > , (_ref2 < span class = "Keyword" > =< / span > _ref.address, street < span class = "Keyword" > =< / span > _ref2[< span class = "Number" > 0< / span > ], city < span class = "Keyword" > =< / span > _ref2[< span class = "Number" > 1< / span > ]);
2011-07-12 13:13:46 -04:00
< / pre > < script > window . example27 = "futurists =\n sculptor: \"Umberto Boccioni\"\n painter: \"Vladimir Burliuk\"\n poet:\n name: \"F.T. Marinetti\"\n address: [\n \"Via Roma 42R\"\n \"Bellagio, Italy 22021\"\n ]\n\n{poet: {name, address: [street, city]}} = futurists\n\nalert name + \" — \" + street" < / script > < div class = 'minibutton load' onclick = 'javascript: loadConsole(example27);' > load< / div > < div class = 'minibutton ok' onclick = 'javascript: var city , futurists , name , street , _ref , _ref2 ;
2011-09-20 23:42:19 -04:00
2010-01-11 22:55:01 -05:00
futurists = {
sculptor: "Umberto Boccioni",
painter: "Vladimir Burliuk",
poet: {
name: "F.T. Marinetti",
address: ["Via Roma 42R", "Bellagio, Italy 22021"]
}
};
2011-09-20 23:42:19 -04:00
_ref = futurists.poet, name = _ref.name, (_ref2 = _ref.address, street = _ref2[0], city = _ref2[1]);
;alert(name + " — " + street);'>run: name + " — " + street< / div > < br class = 'clear' / > < / div >
2010-03-23 00:18:50 -04:00
< p >
2010-12-23 16:30:35 -05:00
Destructuring assignment can even be combined with splats.
2010-03-23 00:18:50 -04:00
< / p >
2010-07-29 00:51:35 -04:00
< div class = 'code' > < pre class = "idle" > tag < span class = "Keyword" > =< / span > < span class = "String" > < span class = "String" > " < / span > < impossible> < span class = "String" > " < / span > < / span >
2010-03-23 00:18:50 -04:00
2010-08-04 23:14:34 -04:00
< span class = "Keyword" > [< / span > open, contents..., close< span class = "Keyword" > ] =< / span > tag.split(< span class = "String" > < span class = "String" > " < / span > < span class = "String" > " < / span > < / span > )
2010-03-23 00:18:50 -04:00
2010-11-21 12:38:27 -05:00
< / pre > < pre class = "idle" > < span class = "Storage" > var< / span > close, contents, open, tag, _i, _ref;
2010-04-03 20:43:50 -04:00
< span class = "Storage" > var< / span > __slice < span class = "Keyword" > =< / span > < span class = "LibraryClassType" > Array< / span > .< span class = "LibraryConstant" > prototype< / span > .slice;
2011-09-20 23:42:19 -04:00
2010-03-23 00:18:50 -04:00
tag < span class = "Keyword" > =< / span > < span class = "String" > < span class = "String" > " < / span > < impossible> < span class = "String" > " < / span > < / span > ;
2011-09-20 23:42:19 -04:00
2010-11-21 12:38:27 -05:00
_ref < span class = "Keyword" > =< / span > tag.< span class = "LibraryFunction" > split< / span > (< span class = "String" > < span class = "String" > " < / span > < span class = "String" > " < / span > < / span > ), open < span class = "Keyword" > =< / span > _ref[< span class = "Number" > 0< / span > ], contents < span class = "Keyword" > =< / span > < span class = "Number" > 3< / span > < span class = "Keyword" > < =< / span > _ref.< span class = "LibraryConstant" > length< / span > ? __slice.< span class = "LibraryFunction" > call< / span > (_ref, < span class = "Number" > 1< / span > , _i < span class = "Keyword" > =< / span > _ref.< span class = "LibraryConstant" > length< / span > < span class = "Keyword" > -< / span > < span class = "Number" > 1< / span > ) : (_i < span class = "Keyword" > =< / span > < span class = "Number" > 1< / span > , []), close < span class = "Keyword" > =< / span > _ref[_i< span class = "Keyword" > ++< / span > ];
2010-12-23 16:44:12 -05:00
< / pre > < script > window . example28 = "tag = \"<impossible>\"\n\n[open, contents..., close] = tag.split(\"\")\n\nalert contents.join(\"\")" < / script > < div class = 'minibutton load' onclick = 'javascript: loadConsole(example28);' > load< / div > < div class = 'minibutton ok' onclick = 'javascript: var close , contents , open , tag , _i , _ref ;
2010-04-03 20:43:50 -04:00
var __slice = Array.prototype.slice;
2011-09-20 23:42:19 -04:00
2010-03-23 00:18:50 -04:00
tag = "< impossible > ";
2011-09-20 23:42:19 -04:00
_ref = tag.split(""), open = _ref[0], contents = 3 < = _ref.length ? __slice.call(_ref, 1, _i = _ref.length - 1) : (_i = 1, []), close = _ref[_i++];
;alert(contents.join(""));'>run: contents.join("")< / div > < br class = 'clear' / > < / div >
2010-01-11 22:55:01 -05:00
2010-02-21 14:30:21 -05:00
< p >
< span id = "fat_arrow" class = "bookmark" > < / span >
2010-01-13 23:24:45 -05:00
< b class = "header" > Function binding< / b >
2010-03-06 19:29:41 -05:00
In JavaScript, the < tt > this< / tt > keyword is dynamically scoped to mean the
object that the current function is attached to. If you pass a function as
2011-07-09 11:03:04 -04:00
a callback or attach it to a different object, the original value of < tt > this< / tt >
2010-03-08 06:34:07 -05:00
will be lost. If you're not familiar with this behavior,
2010-03-06 19:29:41 -05:00
< a href = "http://www.digital-web.com/articles/scope_in_javascript/" > this Digital Web article< / a >
gives a good overview of the quirks.
< / p >
< p >
2010-03-23 00:18:50 -04: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 >
2011-07-12 13:13:46 -04:00
< div class = 'code' > < pre class = "idle" > < span class = "FunctionName" > Account < / span > < span class = "Keyword" > =< / span > < span class = "FunctionArgument" > (customer, cart)< / span > < span class = "Storage" > -> < / span >
2010-07-29 00:51:35 -04:00
< span class = "Variable" > @customer< / span > < span class = "Keyword" > =< / span > customer
< span class = "Variable" > @cart< / span > < span class = "Keyword" > =< / span > cart
2010-01-13 23:24:45 -05:00
2010-11-21 12:38:27 -05:00
$(< span class = "String" > < span class = "String" > '< / span > .shopping_cart< span class = "String" > '< / span > < / span > ).bind < span class = "String" > < span class = "String" > '< / span > click< span class = "String" > '< / span > < / span > , < span class = "FunctionArgument" > (event)< / span > < span class = "Storage" > => < / span >
2010-02-08 10:58:49 -05:00
< span class = "Variable" > @customer< / span > .purchase < span class = "Variable" > @cart< / span >
2010-01-13 23:24:45 -05:00
< / pre > < pre class = "idle" > < span class = "Storage" > var< / span > Account;
2011-09-20 23:42:19 -04:00
2010-05-15 01:18:05 -04:00
< span class = "FunctionName" > Account< / span > = < span class = "Storage" > function< / span > (< span class = "FunctionArgument" > customer, cart< / span > ) {
2011-11-08 18:01:45 -05:00
< span class = "Storage" > var< / span > _this < span class = "Keyword" > =< / span > < span class = "Variable" > this< / span > ;
2010-01-13 23:24:45 -05:00
< span class = "Variable" > this< / span > .customer < span class = "Keyword" > =< / span > customer;
< span class = "Variable" > this< / span > .cart < span class = "Keyword" > =< / span > cart;
2011-11-08 18:01:45 -05:00
< span class = "Keyword" > return< / span > < span class = "Keyword" > $< / span > (< span class = "String" > < span class = "String" > '< / span > .shopping_cart< span class = "String" > '< / span > < / span > ).bind(< span class = "String" > < span class = "String" > '< / span > click< span class = "String" > '< / span > < / span > , < span class = "Storage" > function< / span > (< span class = "LibraryClassType" > event< / span > ) {
< span class = "Keyword" > return< / span > _this.customer.purchase(_this.cart);
});
2010-01-13 23:24:45 -05:00
};
2010-12-23 16:44:12 -05:00
< / pre > < script > window . example29 = "Account = (customer, cart) ->\n @customer = customer\n @cart = cart\n\n $('.shopping_cart').bind 'click', (event) =>\n @customer.purchase @cart" < / script > < div class = 'minibutton load' onclick = 'javascript: loadConsole(example29);' > load< / div > < br class = 'clear' / > < / div >
2010-03-06 19:29:41 -05:00
< p >
If we had used < tt > ->< / tt > in the callback above, < tt > @customer< / tt > would
2010-03-08 06:34:07 -05:00
have referred to the undefined "customer" property of the DOM element,
and trying to call < tt > purchase()< / tt > on it would have raised an exception.
2010-03-06 19:29:41 -05:00
< / p >
2010-01-13 23:24:45 -05:00
2010-02-21 14:30:21 -05:00
< p >
< span id = "embedded" class = "bookmark" > < / span >
2009-12-23 19:42:18 -05:00
< 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 >
2010-07-29 00:51:35 -04:00
< div class = 'code' > < pre class = "idle" > hi < span class = "Keyword" > =< / span > < span class = "String" > < span class = "String" > `< / span > function() {< / span >
2009-12-31 13:01:10 -05:00
< span class = "String" > return [document.title, " Hello JavaScript" ].join(" : " );< / span >
< span class = "String" > }< span class = "String" > `< / span > < / span >
2009-12-21 11:41:45 -05:00
2009-12-26 02:17:34 -05:00
< / pre > < pre class = "idle" > < span class = "Storage" > var< / span > hi;
2011-09-20 23:42:19 -04:00
2009-12-26 02:17:34 -05:00
< span class = "FunctionName" > hi< / span > = < span class = "Storage" > function< / span > () {
2010-07-29 00:51:35 -04:00
< span class = "Keyword" > return< / span > [< span class = "LibraryClassType" > document< / span > .< span class = "LibraryConstant" > title< / span > , < span class = "String" > < span class = "String" > " < / span > Hello JavaScript< span class = "String" > " < / span > < / span > ].< span class = "LibraryFunction" > join< / span > (< span class = "String" > < span class = "String" > " < / span > : < span class = "String" > " < / span > < / span > );
2009-12-21 11:41:45 -05:00
};
2010-12-23 16:44:12 -05:00
< / pre > < script > window . example30 = "hi = `function() {\n return [document.title, \"Hello JavaScript\"].join(\": \");\n}`\n\nalert hi()" < / script > < div class = 'minibutton load' onclick = 'javascript: loadConsole(example30);' > load< / div > < div class = 'minibutton ok' onclick = 'javascript: var hi ;
2011-09-20 23:42:19 -04:00
2009-12-26 02:17:34 -05:00
hi = function() {
2010-07-29 00:51:35 -04:00
return [document.title, "Hello JavaScript"].join(": ");
2011-09-20 23:42:19 -04:00
};
;alert(hi());'>run: hi()< / div > < br class = 'clear' / > < / div >
2009-12-21 11:41:45 -05:00
2010-02-21 14:30:21 -05:00
< p >
< span id = "switch" class = "bookmark" > < / span >
2009-12-24 04:33:59 -05:00
< b class = "header" > Switch/When/Else< / b >
2010-09-27 15:21:45 -04:00
< b > Switch< / b > statements in JavaScript are a bit awkward. You 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.
2010-09-16 00:32:57 -04:00
CoffeeScript prevents accidental fall-through, and can convert the < tt > switch< / tt >
into a returnable, assignable expression. The format is: < tt > switch< / tt > condition,
2009-12-24 04:33:59 -05:00
< 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
< div class = 'code' > < pre class = "idle" > < span class = "Keyword" > switch< / span > day
2010-09-16 00:32:57 -04:00
< span class = "Keyword" > when< / span > < span class = "String" > < span class = "String" > " < / span > Mon< span class = "String" > " < / span > < / span > < span class = "Keyword" > then< / span > go work
< span class = "Keyword" > when< / span > < span class = "String" > < span class = "String" > " < / span > Tue< span class = "String" > " < / span > < / span > < span class = "Keyword" > then< / span > go relax
< span class = "Keyword" > when< / span > < span class = "String" > < span class = "String" > " < / span > Thu< span class = "String" > " < / span > < / span > < span class = "Keyword" > then< / span > go iceFishing
2010-01-13 23:24:45 -05:00
< span class = "Keyword" > when< / span > < span class = "String" > < span class = "String" > " < / span > Fri< span class = "String" > " < / span > < / span > , < span class = "String" > < span class = "String" > " < / span > Sat< span class = "String" > " < / span > < / span >
2010-06-28 00:19:58 -04:00
< span class = "Keyword" > if< / span > day < span class = "Keyword" > is< / span > bingoDay
2010-09-16 00:32:57 -04:00
go bingo
go dancing
< span class = "Keyword" > when< / span > < span class = "String" > < span class = "String" > " < / span > Sun< span class = "String" > " < / span > < / span > < span class = "Keyword" > then< / span > go church
< span class = "Keyword" > else< / span > go work
2011-09-20 23:42:19 -04:00
< / pre > < pre class = "idle" >
< span class = "Keyword" > switch< / span > (day) {
2010-09-21 23:58:05 -04:00
< span class = "Keyword" > case< / span > < span class = "String" > < span class = "String" > " < / span > Mon< span class = "String" > " < / span > < / span > :
< span class = "LibraryFunction" > go< / span > (work);
< span class = "Keyword" > break< / span > ;
< span class = "Keyword" > case< / span > < span class = "String" > < span class = "String" > " < / span > Tue< span class = "String" > " < / span > < / span > :
< span class = "LibraryFunction" > go< / span > (relax);
< span class = "Keyword" > break< / span > ;
< span class = "Keyword" > case< / span > < span class = "String" > < span class = "String" > " < / span > Thu< span class = "String" > " < / span > < / span > :
< span class = "LibraryFunction" > go< / span > (iceFishing);
< span class = "Keyword" > break< / span > ;
< span class = "Keyword" > case< / span > < span class = "String" > < span class = "String" > " < / span > Fri< span class = "String" > " < / span > < / span > :
< span class = "Keyword" > case< / span > < span class = "String" > < span class = "String" > " < / span > Sat< span class = "String" > " < / span > < / span > :
< span class = "Keyword" > if< / span > (day < span class = "Keyword" > ===< / span > bingoDay) {
< span class = "LibraryFunction" > go< / span > (bingo);
< span class = "LibraryFunction" > go< / span > (dancing);
}
< span class = "Keyword" > break< / span > ;
< span class = "Keyword" > case< / span > < span class = "String" > < span class = "String" > " < / span > Sun< span class = "String" > " < / span > < / span > :
< span class = "LibraryFunction" > go< / span > (church);
< span class = "Keyword" > break< / span > ;
< span class = "Keyword" > default< / span > :
< span class = "LibraryFunction" > go< / span > (work);
2009-12-21 11:41:45 -05:00
}
2010-12-23 16:44:12 -05:00
< / pre > < script > window . example31 = "switch day\n when \"Mon\" then go work\n when \"Tue\" then go relax\n when \"Thu\" then go iceFishing\n when \"Fri\", \"Sat\"\n if day is bingoDay\n go bingo\n go dancing\n when \"Sun\" then go church\n else go work" < / script > < div class = 'minibutton load' onclick = 'javascript: loadConsole(example31);' > load< / div > < br class = 'clear' / > < / div >
2009-12-21 11:41:45 -05:00
2010-02-21 14:30:21 -05:00
< p >
< span id = "try" class = "bookmark" > < / span >
2009-12-23 19:42:18 -05:00
< b class = "header" > Try/Catch/Finally< / b >
2009-12-24 02:01:39 -05:00
Try/catch statements are just about the same as JavaScript (although
2009-12-24 03:12:07 -05:00
they work as expressions).
2009-12-23 19:42:18 -05:00
< / p >
< div class = 'code' > < pre class = "idle" > < span class = "Keyword" > try< / span >
2010-06-28 00:19:58 -04:00
allHellBreaksLoose()
catsAndDogsLivingTogether()
2009-12-21 11:41:45 -05:00
< span class = "Keyword" > catch< / span > error
2010-01-25 00:14:00 -05:00
print error
2009-12-21 11:41:45 -05:00
< span class = "Keyword" > finally< / span >
2010-06-28 00:19:58 -04:00
cleanUp()
2010-12-12 21:41:04 -05:00
2011-09-20 23:42:19 -04:00
< / pre > < pre class = "idle" >
< span class = "Keyword" > try< / span > {
2010-06-28 00:19:58 -04:00
allHellBreaksLoose();
catsAndDogsLivingTogether();
2009-12-21 11:41:45 -05:00
} < span class = "Keyword" > catch< / span > (error) {
< span class = "LibraryFunction" > print< / span > (error);
} < span class = "Keyword" > finally< / span > {
2010-06-28 00:19:58 -04:00
cleanUp();
2009-12-21 11:41:45 -05:00
}
2010-12-23 16:44:12 -05:00
< / pre > < script > window . example32 = "try\n allHellBreaksLoose()\n catsAndDogsLivingTogether()\ncatch error\n print error\nfinally\n cleanUp()\n\n" < / script > < div class = 'minibutton load' onclick = 'javascript: loadConsole(example32);' > load< / div > < br class = 'clear' / > < / div >
2009-12-21 11:41:45 -05:00
2010-02-21 14:30:21 -05:00
< p >
< span id = "comparisons" class = "bookmark" > < / span >
2010-01-17 18:12:59 -05:00
< 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 >
2010-07-29 00:51:35 -04:00
< div class = 'code' > < pre class = "idle" > cholesterol < span class = "Keyword" > =< / span > < span class = "Number" > 127< / span >
2010-01-17 18:12:59 -05:00
2010-07-29 00:51:35 -04:00
healthy < span class = "Keyword" > =< / span > < span class = "Number" > 200< / span > < span class = "Keyword" > > < / span > cholesterol < span class = "Keyword" > > < / span > < span class = "Number" > 60< / span >
2010-01-17 18:12:59 -05:00
< / pre > < pre class = "idle" > < span class = "Storage" > var< / span > cholesterol, healthy;
2011-09-20 23:42:19 -04:00
2010-01-17 18:12:59 -05:00
cholesterol < span class = "Keyword" > =< / span > < span class = "Number" > 127< / span > ;
2011-09-20 23:42:19 -04:00
2010-12-04 12:52:51 -05:00
healthy < span class = "Keyword" > =< / span > (< span class = "Number" > 200< / span > < span class = "Keyword" > > < / span > cholesterol < span class = "Keyword" > & < / span > < span class = "Keyword" > & < / span > cholesterol < span class = "Keyword" > > < / span > < span class = "Number" > 60< / span > );
2010-12-23 16:44:12 -05:00
< / pre > < script > window . example33 = "cholesterol = 127\n\nhealthy = 200 > cholesterol > 60\n\nalert healthy" < / script > < div class = 'minibutton load' onclick = 'javascript: loadConsole(example33);' > load< / div > < div class = 'minibutton ok' onclick = 'javascript: var cholesterol , healthy ;
2011-09-20 23:42:19 -04:00
2010-01-17 18:12:59 -05:00
cholesterol = 127;
2011-09-20 23:42:19 -04:00
healthy = (200 > cholesterol & & cholesterol > 60);
;alert(healthy);'>run: healthy< / div > < br class = 'clear' / > < / div >
2010-01-17 18:12:59 -05:00
2010-03-23 00:18:50 -04:00
< p >
2010-11-21 21:12:59 -05:00
< span id = "strings" class = "bookmark" > < / span >
< b class = "header" > String Interpolation, Heredocs, and Block Comments< / b >
2010-08-23 22:08:33 -04:00
Ruby-style string interpolation is included in CoffeeScript. Double-quoted
2010-11-21 21:12:59 -05:00
strings allow for interpolated values, using < tt > #{ ... }< / tt > ,
and single-quoted strings are literal.
2010-03-23 00:18:50 -04:00
< / p >
2010-07-29 00:51:35 -04:00
< div class = 'code' > < pre class = "idle" > author < span class = "Keyword" > =< / span > < span class = "String" > < span class = "String" > " < / span > Wittgenstein< span class = "String" > " < / span > < / span >
2010-11-21 21:12:59 -05:00
quote < span class = "Keyword" > =< / span > < span class = "String" > < span class = "String" > " < / span > A picture is a fact. -- < span class = "String" > < span class = "String" > #{< / span > author < span class = "String" > }< / span > < / span > < span class = "String" > " < / span > < / span >
2010-03-23 00:18:50 -04:00
2010-11-21 21:12:59 -05:00
sentence < span class = "Keyword" > =< / span > < span class = "String" > < span class = "String" > " < / span > < span class = "String" > < span class = "String" > #{< / span > < span class = "Number" > 22< / span > < span class = "Keyword" > /< / span > < span class = "Number" > 7< / span > < span class = "String" > }< / span > < / span > is a decent approximation of π< span class = "String" > " < / span > < / span >
2010-03-23 00:18:50 -04:00
2010-11-21 21:12:59 -05:00
< / pre > < pre class = "idle" > < span class = "Storage" > var< / span > author, quote, sentence;
2011-09-20 23:42:19 -04:00
2010-11-21 21:12:59 -05:00
author < span class = "Keyword" > =< / span > < span class = "String" > < span class = "String" > " < / span > Wittgenstein< span class = "String" > " < / span > < / span > ;
2011-09-20 23:42:19 -04:00
2010-11-21 21:12:59 -05:00
quote < span class = "Keyword" > =< / span > < span class = "String" > < span class = "String" > " < / span > A picture is a fact. -- < span class = "String" > " < / span > < / span > < span class = "Keyword" > +< / span > author;
2011-09-20 23:42:19 -04:00
2010-10-24 21:50:34 -04:00
sentence < span class = "Keyword" > =< / span > < span class = "String" > < span class = "String" > " < / span > < span class = "String" > " < / span > < / span > < span class = "Keyword" > +< / span > (< span class = "Number" > 22< / span > / < span class = "Number" > 7< / span > ) < span class = "Keyword" > +< / span > < span class = "String" > < span class = "String" > " < / span > is a decent approximation of π< span class = "String" > " < / span > < / span > ;
2011-07-12 13:13:46 -04:00
< / pre > < script > window . example34 = "author = \"Wittgenstein\"\nquote = \"A picture is a fact. -- #{ author }\"\n\nsentence = \"#{ 22 / 7 } is a decent approximation of π\"\n\nalert sentence" < / script > < div class = 'minibutton load' onclick = 'javascript: loadConsole(example34);' > load< / div > < div class = 'minibutton ok' onclick = 'javascript: var author , quote , sentence ;
2011-09-20 23:42:19 -04:00
2010-11-21 21:12:59 -05:00
author = "Wittgenstein";
2011-09-20 23:42:19 -04:00
2010-11-21 21:12:59 -05:00
quote = "A picture is a fact. -- " + author;
2011-09-20 23:42:19 -04:00
sentence = "" + (22 / 7) + " is a decent approximation of π";
;alert(sentence);'>run: sentence< / div > < br class = 'clear' / > < / div >
2010-02-21 14:30:21 -05:00
< p >
2009-12-23 19:42:18 -05:00
Multiline strings are allowed in CoffeeScript.
< / p >
2010-07-29 00:51:35 -04:00
< div class = 'code' > < pre class = "idle" > mobyDick < span class = "Keyword" > =< / span > < span class = "String" > < span class = "String" > " < / span > Call me Ishmael. Some years ago --< / span >
2010-08-23 22:08:33 -04:00
< span class = "String" > never mind how long precisely -- having little< / span >
< span class = "String" > or no money in my purse, and nothing particular< / span >
< span class = "String" > to interest me on shore, I thought I would sail< / span >
< span class = "String" > about a little and see the watery part of the< / span >
< span class = "String" > world...< span class = "String" > " < / span > < / span >
2009-12-24 01:22:41 -05:00
2009-12-26 02:17:34 -05:00
2010-06-28 00:19:58 -04:00
< / pre > < pre class = "idle" > < span class = "Storage" > var< / span > mobyDick;
2011-09-20 23:42:19 -04:00
2010-09-27 15:21:45 -04:00
mobyDick < span class = "Keyword" > =< / span > < span class = "String" > < span class = "String" > " < / span > Call me Ishmael. Some years ago -- never mind how long precisely -- having little or no money in my purse, and nothing particular to interest me on shore, I thought I would sail about a little and see the watery part of the world...< span class = "String" > " < / span > < / span > ;
2010-12-23 16:44:12 -05:00
< / pre > < script > window . example35 = "mobyDick = \"Call me Ishmael. Some years ago --\n never mind how long precisely -- having little\n or no money in my purse, and nothing particular\n to interest me on shore, I thought I would sail\n about a little and see the watery part of the\n world...\"\n\nalert mobyDick" < / script > < div class = 'minibutton load' onclick = 'javascript: loadConsole(example35);' > load< / div > < div class = 'minibutton ok' onclick = 'javascript: var mobyDick ;
2011-09-20 23:42:19 -04:00
mobyDick = "Call me Ishmael. Some years ago -- never mind how long precisely -- having little or no money in my purse, and nothing particular to interest me on shore, I thought I would sail about a little and see the watery part of the world...";
;alert(mobyDick);'>run: mobyDick< / div > < br class = 'clear' / > < / div >
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 >
2010-07-29 00:51:35 -04:00
< div class = 'code' > < pre class = "idle" > html < span class = "Keyword" > =< / span > < span class = "String" > < span class = "String" > '''< / span > < / span >
< span class = "String" > < strong> < / span >
< span class = "String" > cup of coffeescript< / span >
< span class = "String" > < /strong> < / span >
< span class = "String" > < span class = "String" > '''< / span > < / span >
2010-12-12 21:41:04 -05:00
2010-01-12 00:00:38 -05:00
< / pre > < pre class = "idle" > < span class = "Storage" > var< / span > html;
2011-09-20 23:42:19 -04:00
2010-03-23 00:18:50 -04:00
html < span class = "Keyword" > =< / span > < span class = "String" > < span class = "String" > '< / span > < strong> < span class = "UserDefinedConstant" > \n< / span > cup of coffeescript< span class = "UserDefinedConstant" > \n< / span > < /strong> < span class = "String" > '< / span > < / span > ;
2011-09-20 23:42:19 -04:00
< / pre > < script > window . example36 = "html = '''\n <strong>\n cup of coffeescript\n </strong>\n '''\n\nalert html" < / script > < div class = 'minibutton load' onclick = 'javascript: loadConsole(example36);' > load< / div > < div class = 'minibutton ok' onclick = 'javascript: var html ;
html = '< strong > \n cup of coffeescript\n< / strong > ';
;alert(html);'>run: html< / div > < br class = 'clear' / > < / div >
2010-03-08 06:34:07 -05:00
< p >
2010-03-23 00:18:50 -04:00
Double-quoted heredocs, like double-quoted strings, allow interpolation.
2010-03-08 06:34:07 -05:00
< / p >
2010-07-17 10:28:06 -04:00
< p >
2010-07-29 00:51:35 -04:00
Sometimes you'd like to pass a block comment through to the generated
JavaScript. For example, when you need to embed a licensing header at
2011-05-01 12:08:15 -04:00
the top of a file. Block comments, which mirror the syntax for heredocs,
2010-07-17 10:28:06 -04:00
are preserved in the generated code.
< / p >
< div class = 'code' > < pre class = "idle" > < span class = "Comment" > < span class = "Comment" > ###< / span > < / span >
2011-11-08 18:01:45 -05:00
< span class = "Comment" > CoffeeScript Compiler v1.1.3< / span >
2010-07-17 10:28:06 -04:00
< span class = "Comment" > Released under the MIT License< / span >
2010-12-12 21:41:04 -05:00
< span class = "Comment" > < span class = "Comment" > ###< / span > < / span >
2011-09-20 23:42:19 -04:00
< / pre > < pre class = "idle" >
< span class = "Comment" > < span class = "Comment" > /*< / span > < / span >
2011-11-08 18:01:45 -05:00
< span class = "Comment" > CoffeeScript Compiler v1.1.3< / span >
2010-07-17 10:28:06 -04:00
< span class = "Comment" > Released under the MIT License< / span >
2010-10-24 21:53:48 -04:00
< span class = "Comment" > < span class = "Comment" > */< / span > < / span >
2011-09-20 23:42:19 -04:00
2011-11-08 18:01:45 -05:00
< / pre > < script > window . example37 = "###\nCoffeeScript Compiler v1.1.3\nReleased under the MIT License\n###\n\n\n" < / script > < div class = 'minibutton load' onclick = 'javascript: loadConsole(example37);' > load< / div > < br class = 'clear' / > < / div >
2010-03-08 06:34:07 -05:00
2010-11-21 21:12:59 -05:00
< p >
< span id = "regexes" class = "bookmark" > < / span >
< b class = "header" > Extended Regular Expressions< / b >
Similar to "heredocs" and "herecomments", CoffeeScript supports "heregexes" —
extended regular expressions that ignore internal whitespace and can contain
comments, after Perl's < tt > /x< / tt > modifier, but delimited by < tt > ///< / tt > .
They go a long way towards making complex regular expressions readable.
To quote from the CoffeeScript source:
< / p >
< div class = 'code' > < pre class = "idle" > OPERATOR < span class = "Keyword" > =< / span > < span class = "String" > /// ^ (< / span >
< span class = "String" > ?: [-=]> < span class = "Comment" > < span class = "Comment" > #< / span > function< / span > < / span >
< span class = "String" > | [-+*/%< > & |^!?=]= < span class = "Comment" > < span class = "Comment" > #< / span > compound assign / compare< / span > < / span >
< span class = "String" > | > > > =? < span class = "Comment" > < span class = "Comment" > #< / span > zero-fill right shift< / span > < / span >
< span class = "String" > | ([-+:])\1 < span class = "Comment" > < span class = "Comment" > #< / span > doubles< / span > < / span >
< span class = "String" > | ([& |< > ])\2=? < span class = "Comment" > < span class = "Comment" > #< / span > logic / shift< / span > < / span >
< span class = "String" > | \?\. < span class = "Comment" > < span class = "Comment" > #< / span > soak access< / span > < / span >
< span class = "String" > | \.{2,3} < span class = "Comment" > < span class = "Comment" > #< / span > range or splat< / span > < / span >
< span class = "String" > ) ///< / span >
2010-12-12 21:41:04 -05:00
2010-11-21 21:12:59 -05:00
< / pre > < pre class = "idle" > < span class = "Storage" > var< / span > OPERATOR;
2011-09-20 23:42:19 -04:00
2010-11-21 21:12:59 -05:00
OPERATOR < span class = "Keyword" > =< / span > < span class = "String" > < span class = "String" > /< / span > ^(?:[-=]> |[-+*< span class = "UserDefinedConstant" > \/< / span > %< > & |^!?=]=|> > > =?|([-+:])< span class = "UserDefinedConstant" > \1< / span > |([& |< > ])< span class = "UserDefinedConstant" > \2< / span > =?|< span class = "UserDefinedConstant" > \?< / span > < span class = "UserDefinedConstant" > \.< / span > |< span class = "UserDefinedConstant" > \.< / span > {2,3})< span class = "String" > /< / span > < / span > ;
2010-12-23 16:44:12 -05:00
< / pre > < script > window . example38 = "OPERATOR = /// ^ (\n ?: [-=]> # function\n | [-+*/%<>&|^!?=]= # compound assign / compare\n | >>>=? # zero-fill right shift\n | ([-+:])\\1 # doubles\n | ([&|<>])\\2=? # logic / shift\n | \\?\\. # soak access\n | \\.{2,3} # range or splat\n) ///\n\n\n" < / script > < div class = 'minibutton load' onclick = 'javascript: loadConsole(example38);' > load< / div > < br class = 'clear' / > < / div >
2010-11-21 21:12:59 -05:00
2010-02-21 18:37:52 -05:00
< h2 >
< span id = "cake" class = "bookmark" > < / span >
Cake, and Cakefiles
< / h2 >
< p >
2011-08-14 16:24:26 -04:00
CoffeeScript includes a (very) simple build system similar to
2010-07-29 00:51:35 -04:00
< a href = "http://www.gnu.org/software/make/" > Make< / a > and
2010-07-17 10:28:06 -04:00
< a href = "http://rake.rubyforge.org/" > Rake< / a > . Naturally,
2011-08-14 16:24:26 -04:00
it's called Cake, and is used for the tasks that build and test the CoffeeScript
2010-02-21 18:37:52 -05:00
language itself. Tasks are defined in a file named < tt > Cakefile< / tt > , and
2011-08-14 16:24:26 -04:00
can be invoked by running < tt > cake [task]< / tt > from within the directory.
To print a list of all the tasks and options, just type < tt > cake< / tt > .
2010-02-21 18:37:52 -05:00
< / p >
< p >
Task definitions are written in CoffeeScript, so you can put arbitrary code
in your Cakefile. Define a task with a name, a long description, and the
2010-07-17 10:28:06 -04:00
function to invoke when the task is run. If your task takes a command-line
option, you can define the option with short and long flags, and it will
be made available in the < tt > options< / tt > object. Here's a task that uses
the Node.js API to rebuild CoffeeScript's parser:
< / p >
2010-07-29 00:51:35 -04:00
< div class = 'code' > < pre class = "idle" > fs < span class = "Keyword" > =< / span > require < span class = "String" > < span class = "String" > '< / span > fs< span class = "String" > '< / span > < / span >
2010-07-17 10:28:06 -04:00
option < span class = "String" > < span class = "String" > '< / span > -o< span class = "String" > '< / span > < / span > , < span class = "String" > < span class = "String" > '< / span > --output [DIR]< span class = "String" > '< / span > < / span > , < span class = "String" > < span class = "String" > '< / span > directory for compiled code< span class = "String" > '< / span > < / span >
2010-11-21 12:38:27 -05:00
task < span class = "String" > < span class = "String" > '< / span > build:parser< span class = "String" > '< / span > < / span > , < span class = "String" > < span class = "String" > '< / span > rebuild the Jison parser< span class = "String" > '< / span > < / span > , < span class = "FunctionArgument" > (options)< / span > < span class = "Storage" > -> < / span >
2010-07-17 10:28:06 -04:00
require < span class = "String" > < span class = "String" > '< / span > jison< span class = "String" > '< / span > < / span >
2010-07-29 00:51:35 -04:00
code < span class = "Keyword" > =< / span > require(< span class = "String" > < span class = "String" > '< / span > ./lib/grammar< span class = "String" > '< / span > < / span > ).parser.generate()
dir < span class = "Keyword" > =< / span > options.output < span class = "Keyword" > or< / span > < span class = "String" > < span class = "String" > '< / span > lib< span class = "String" > '< / span > < / span >
2010-08-11 00:40:15 -04:00
fs.writeFile < span class = "String" > < span class = "String" > " < / span > < span class = "String" > < span class = "String" > #{< / span > dir< span class = "String" > }< / span > < / span > /parser.js< span class = "String" > " < / span > < / span > , code
2010-07-17 10:28:06 -04:00
< / pre > < pre class = "idle" > < span class = "Storage" > var< / span > fs;
2011-09-20 23:42:19 -04:00
2010-07-17 10:28:06 -04:00
fs < span class = "Keyword" > =< / span > require(< span class = "String" > < span class = "String" > '< / span > fs< span class = "String" > '< / span > < / span > );
2011-09-20 23:42:19 -04:00
2010-07-17 10:28:06 -04:00
option(< span class = "String" > < span class = "String" > '< / span > -o< span class = "String" > '< / span > < / span > , < span class = "String" > < span class = "String" > '< / span > --output [DIR]< span class = "String" > '< / span > < / span > , < span class = "String" > < span class = "String" > '< / span > directory for compiled code< span class = "String" > '< / span > < / span > );
2011-09-20 23:42:19 -04:00
2010-08-11 00:40:15 -04:00
task(< span class = "String" > < span class = "String" > '< / span > build:parser< span class = "String" > '< / span > < / span > , < span class = "String" > < span class = "String" > '< / span > rebuild the Jison parser< span class = "String" > '< / span > < / span > , < span class = "Storage" > function< / span > (options) {
2010-07-17 10:28:06 -04:00
< span class = "Storage" > var< / span > code, dir;
require(< span class = "String" > < span class = "String" > '< / span > jison< span class = "String" > '< / span > < / span > );
code < span class = "Keyword" > =< / span > require(< span class = "String" > < span class = "String" > '< / span > ./lib/grammar< span class = "String" > '< / span > < / span > ).parser.generate();
dir < span class = "Keyword" > =< / span > options.output < span class = "Keyword" > ||< / span > < span class = "String" > < span class = "String" > '< / span > lib< span class = "String" > '< / span > < / span > ;
2010-10-05 21:05:57 -04:00
< span class = "Keyword" > return< / span > fs.writeFile(< span class = "String" > < span class = "String" > " < / span > < span class = "String" > " < / span > < / span > < span class = "Keyword" > +< / span > dir < span class = "Keyword" > +< / span > < span class = "String" > < span class = "String" > " < / span > /parser.js< span class = "String" > " < / span > < / span > , code);
2010-02-21 18:37:52 -05:00
});
2010-12-23 16:44:12 -05:00
< / pre > < script > window . example39 = "fs = require 'fs'\n\noption '-o', '--output [DIR]', 'directory for compiled code'\n\ntask 'build:parser', 'rebuild the Jison parser', (options) ->\n require 'jison'\n code = require('./lib/grammar').parser.generate()\n dir = options.output or 'lib'\n fs.writeFile \"#{dir}/parser.js\", code" < / script > < div class = 'minibutton load' onclick = 'javascript: loadConsole(example39);' > load< / div > < br class = 'clear' / > < / div >
2010-09-17 20:24:10 -04:00
< p >
If you need to invoke one task before another — for example, running
2010-09-27 15:21:45 -04:00
< tt > build< / tt > before < tt > test< / tt > , you can use the < tt > invoke< / tt > function:
2011-08-14 16:24:26 -04:00
< tt > invoke 'build'< / tt > . Cake tasks are a minimal way to expose your
CoffeeScript functions to the command line, so
< a href = "documentation/docs/cake.html" > don't expect any fanciness built-in< / a > .
If you need dependencies, or async callbacks, it's best to put them in your
code itself — not the cake task.
2010-09-17 20:24:10 -04:00
< / p >
2009-12-31 13:01:10 -05:00
2010-02-25 00:26:59 -05:00
< h2 >
< span id = "scripts" class = "bookmark" > < / span >
"text/coffeescript" Script Tags
< / h2 >
< p >
While it's not recommended for serious use, CoffeeScripts may be included
directly within the browser using < tt > < script type="text/coffeescript"> < / tt >
2010-02-25 06:26:27 -05:00
tags. The source includes a compressed and minified version of the compiler
2010-11-21 21:12:59 -05:00
(< a href = "extras/coffee-script.js" > Download current version here, 39k when gzipped< / a > )
2010-02-25 18:20:15 -05:00
as < tt > extras/coffee-script.js< / tt > . Include this file on a page with
2010-02-25 06:26:27 -05:00
inline CoffeeScript tags, and it will compile and evaluate them in order.
2010-02-25 00:26:59 -05:00
< / p >
< p >
In fact, the little bit of glue script that runs "Try CoffeeScript" above,
2011-07-09 01:26:30 -04:00
as well as the jQuery for the menu, is implemented in just this way.
2010-02-25 00:26:59 -05:00
View source and look at the bottom of the page to see the example.
Including the script also gives you access to < tt > CoffeeScript.compile()< / tt >
so you can pop open Firebug and try compiling some strings.
< / p >
2010-02-25 18:20:15 -05:00
2010-02-25 00:26:59 -05:00
< p >
The usual caveats about CoffeeScript apply — your inline scripts will
run within a closure wrapper, so if you want to expose global variables or
functions, attach them to the < tt > window< / tt > object.
< / p >
2010-11-21 21:12:59 -05:00
2010-11-21 19:53:31 -05:00
< h2 >
2010-12-21 22:24:24 -05:00
< span id = "resources" class = "bookmark" > < / span >
2011-08-04 23:45:38 -04:00
Books and Screencasts
< / h2 >
2011-09-20 23:42:19 -04:00
2011-08-04 23:45:38 -04:00
< p >
2011-09-20 23:42:19 -04:00
There are a number of excellent books and screencasts to help you get
2011-08-04 23:45:38 -04:00
started with CoffeeScript, some of which are freely available online.
< / p >
2011-09-20 23:42:19 -04:00
2011-08-04 23:45:38 -04:00
< ul >
< li >
< a href = "http://arcturo.github.com/library/coffeescript/" > The Little Book on CoffeeScript< / a >
is a brief 5-chapter introduction to CoffeeScript, written with great
2011-09-20 23:42:19 -04:00
clarity and precision by
2011-08-04 23:45:38 -04:00
< a href = "http://alexmaccaw.co.uk/" > Alex MacCaw< / a > .
< / li >
< li >
< a href = "http://autotelicum.github.com/Smooth-CoffeeScript/" > Smooth CoffeeScript< / a >
is a reimagination of the excellent book
< a href = "http://eloquentjavascript.net/" > Eloquent JavaScript< / a > , as if
it had been written in CoffeeScript instead. Covers language features
as well a the functional and object oriented programming styles. By
< a href = "https://github.com/autotelicum" > E. Hoigaard< / a > .
< / li >
< li >
< a href = "http://pragprog.com/book/tbcoffee/coffeescript" > CoffeeScript: Accelerated JavaScript Development< / a >
2011-09-20 23:42:19 -04:00
is < a href = "http://trevorburnham.com/" > Trevor Burnham< / a > 's thorough
2011-08-04 23:45:38 -04:00
introduction to the language. By the end of the book, you'll have built
a fast-paced multiplayer word game, writing both the client-side and Node.js
portions in CoffeeScript.
< / li >
< li >
< a href = "http://peepcode.com/products/coffeescript" > Meet CoffeeScript< / a >
is a 75-minute long screencast by < a href = "http://peepcode.com/" > PeepCode< / a > .
Highly memorable for its animations which demonstrate transforming CoffeeScript
into the equivalent JS.
< / li >
< li >
If you're looking for less of a time commitment, RailsCasts'
< a href = "http://railscasts.com/episodes/267-coffeescript-basics" > CoffeeScript Basics< / a >
should have you covered, hitting all of the important notes about CoffeeScript
in 11 minutes.
< / li >
< / ul >
2011-09-20 23:42:19 -04:00
2011-08-04 23:45:38 -04:00
< h2 >
2010-11-21 19:53:31 -05:00
Examples
< / h2 >
2011-11-08 18:01:45 -05:00
< p >
The < a href = "https://github.com/languages/coffeescript" > best list of
open-source CoffeeScript examples< / a > can be found on GitHub. But just
to throw out few more:
< / p >
2010-11-21 21:12:59 -05:00
2010-11-21 19:53:31 -05:00
< ul >
2011-11-08 18:01:45 -05:00
< li >
< b > github< / b > 's < a href = "http://hubot.github.com/" > Hubot< / a > ,
a friendly IRC robot that can perform any number of useful and useless tasks.
< / li >
2011-04-20 23:08:21 -04:00
< li >
< b > sstephenson< / b > 's < a href = "http://pow.cx/" > Pow< / a > ,
a zero-configuration Rack server, with comprehensive annotated source.
< / li >
2010-11-21 19:53:31 -05:00
< li >
2010-11-21 21:12:59 -05:00
< b > frank06< / b > 's < a href = "http://riakjs.org/" > riak-js< / a > , a Node.js client for
2010-11-21 19:53:31 -05:00
< a href = "http://www.basho.com/Riak.html" > Riak< / a > , with support for HTTP
and Protocol Buffers.
< / li >
< li >
< b > technoweenie< / b > 's < a href = "https://github.com/technoweenie/coffee-resque" > Coffee-Resque< / a > ,
a port of < a href = "https://github.com/defunkt/resque" > Resque< / a > for Node.js.
< / li >
2011-04-20 23:08:21 -04:00
< li >
< b > assaf< / b > 's < a href = "http://zombie.labnotes.org/" > Zombie.js< / a > ,
2011-11-08 18:01:45 -05:00
a headless, full-stack, faux-browser testing library for Node.js.
2011-04-20 23:08:21 -04:00
< / li >
2010-11-21 19:53:31 -05:00
< li >
< b > jashkenas< / b > ' < a href = "documentation/docs/underscore.html" > Underscore.coffee< / a > , a port
of the < a href = "http://documentcloud.github.com/underscore/" > Underscore.js< / a >
library of helper functions.
< / li >
< li >
< b > stephank< / b > 's < a href = "https://github.com/stephank/orona" > Orona< / a > , a remake of
the Bolo tank game for modern browsers.
< / li >
< li >
2010-11-21 21:12:59 -05:00
< b > josh< / b > 's < a href = "http://josh.github.com/nack/" > nack< / a > , a Node.js-powered
2010-11-21 19:53:31 -05:00
< a href = "http://rack.rubyforge.org/" > Rack< / a > server.
< / li >
< / ul >
2010-02-25 00:26:59 -05:00
2010-02-21 14:30:21 -05:00
< h2 >
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-02-21 18:37:52 -05:00
Use < tt > bin/coffee< / tt > to test your changes,< br / >
< tt > bin/cake test< / tt > to run the test suite,< br / >
< tt > bin/cake build< / tt > to rebuild the CoffeeScript compiler, and < br / >
< tt > bin/cake build:parser< / tt > to regenerate the Jison parser if you're
2010-03-07 21:57:08 -05:00
working on the grammar. < br / > < br / >
2010-03-23 00:25:37 -04:00
< tt > git checkout lib & & bin/cake build:full< / tt > is a good command to run when you're working
2010-03-08 06:34:07 -05:00
on the core language. It'll refresh the lib directory
(in case you broke something), build your altered compiler, use that to
2010-03-07 21:57:08 -05:00
rebuild itself (a good sanity test) and then run all of the tests. If
they pass, there's a good chance you've made a successful change.
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 / >
2011-04-20 23:08:21 -04:00
Bug reports, feature proposals, and ideas for changes to the language belong here.
2011-03-20 17:40:20 -04:00
< / li >
2010-07-11 10:40:44 -04:00
< li >
2011-04-20 23:08:21 -04:00
< a href = "https://groups.google.com/forum/#!forum/coffeescript" > CoffeeScript Google Group< / a > < br / >
If you'd like to ask a question, the mailing list is a good place to get help.
2010-07-11 10:40:44 -04:00
< / li >
2010-04-13 23:41:18 -04:00
< li >
2011-04-20 23:08:21 -04:00
< a href = "http://github.com/jashkenas/coffee-script/wiki" > The CoffeeScript Wiki< / a > < br / >
If you've ever learned a neat CoffeeScript tip or trick, or ran into a gotcha — share it on the wiki.
2011-09-20 23:42:19 -04:00
The wiki also serves as a directory of handy
2011-04-20 23:08:21 -04:00
< a href = "http://github.com/jashkenas/coffee-script/wiki/Text-editor-plugins" > text editor extensions< / a > ,
< a href = "http://github.com/jashkenas/coffee-script/wiki/Web-framework-plugins" > web framework plugins< / a > ,
and general < a href = "http://github.com/jashkenas/coffee-script/wiki/Build-tools" > CoffeeScript build tools< / a > .
2010-04-13 23:41:18 -04:00
< / li >
2011-01-30 17:10:50 -05:00
< li >
2011-04-20 23:08:21 -04:00
< a href = "http://github.com/jashkenas/coffee-script/wiki/FAQ" > The FAQ< / a > < br / >
Perhaps your CoffeeScript-related question has been asked before. Check the FAQ first.
2010-10-24 21:50:34 -04:00
< / li >
2009-12-31 18:22:51 -05:00
< / ul >
2009-12-25 03:16:56 -05:00
2010-04-10 13:42:39 -04:00
< h2 >
< span id = "webchat" class = "bookmark" > < / span >
Web Chat (IRC)
< / h2 >
< p >
Quick help and advice can usually be found in the CoffeeScript IRC room.
Join < tt > #coffeescript< / tt > on < tt > irc.freenode.net< / tt > , or click the
button below to open a webchat session on this page.
< / p >
< p >
< button id = "open_webchat" > click to open #coffeescript< / button >
< / p >
2010-02-21 14:30:21 -05:00
< h2 >
< span id = "change_log" class = "bookmark" > < / span >
Change Log
< / h2 >
2011-11-08 18:01:45 -05:00
< p >
< b class = "header" style = "margin-top: 20px;" >
< a href = "https://github.com/jashkenas/coffee-script/compare/1.1.2...1.1.3" > 1.1.3< / a >
< span class = "timestamp" > – < small > Nov. 8, 2011< / small > < / span >
< / b >
< ul >
< li >
Ahh, whitespace. CoffeeScript's compiled JS now tries to space things
out and keep it readable, as you can see in the examples on this page.
< / li >
< li >
You can now call < tt > super< / tt > in class level methods in class bodies,
and bound class methods now preserve their correct context.
< / li >
< li >
JavaScript has always supported octal numbers < tt > 010 is 8< / tt > ,
and hexadecimal numbers < tt > 0xf is 15< / tt > , but CoffeeScript now
also supports binary numbers: < tt > 0b10 is 2< / tt > .
< / li >
< li >
The CoffeeScript module has been nested under a subdirectory to make
it easier to < tt > require< / tt > individual components separately, without
having to use < b > npm< / b > . For example, after adding the CoffeeScript
folder to your path: < tt > require('coffee-script/lexer')< / tt >
< / li >
< li >
There's a new "link" feature in Try CoffeeScript on this webpage. Use
it to get a shareable permalink for your example script.
< / li >
< li >
The < tt > coffee --watch< / tt > feature now only works on Node.js 0.6.0
and higher, but now also works properly on Windows.
< / li >
< li >
Lots of small bug fixes from
< b > < a href = "https://github.com/michaelficarra" > @michaelficarra< / a > < / b > ,
< b > < a href = "https://github.com/geraldalewis" > @geraldalewis< / a > < / b > ,
< b > < a href = "https://github.com/satyr" > @satyr< / a > < / b > , and
< b > < a href = "https://github.com/trevorburnham" > @trevorburnham< / a > < / b > .
< / li >
< / ul >
< / p >
2011-09-20 23:42:19 -04:00
2011-08-04 23:17:23 -04:00
< p >
< b class = "header" style = "margin-top: 20px;" >
< a href = "https://github.com/jashkenas/coffee-script/compare/1.1.1...1.1.2" > 1.1.2< / a >
< span class = "timestamp" > – < small > August 4, 2011< / small > < / span >
< / b >
2011-08-08 10:03:41 -04:00
Fixes for block comment formatting, < tt > ?=< / tt > compilation, implicit calls
2011-08-04 23:17:23 -04:00
against control structures, implicit invocation of a try/catch block,
variadic arguments leaking from local scope, line numbers in syntax errors
following heregexes, property access on parenthesized number literals,
2011-09-20 23:42:19 -04:00
bound class methods and super with reserved names, a REPL overhaul,
2011-08-04 23:17:23 -04:00
consecutive compiled semicolons, block comments in implicitly called objects,
and a Chrome bug.
< / p >
2011-09-20 23:42:19 -04:00
2011-05-10 09:27:19 -04:00
< p >
< b class = "header" style = "margin-top: 20px;" > 1.1.1
< span class = "timestamp" > – < small > May 10, 2011< / small > < / span >
< / b >
2011-09-20 23:42:19 -04:00
Bugfix release for classes with external constructor functions, see
2011-05-10 09:27:19 -04:00
issue #1182.
< / p >
2011-09-20 23:42:19 -04:00
2011-05-01 12:08:15 -04:00
< p >
< b class = "header" style = "margin-top: 20px;" > 1.1.0
< span class = "timestamp" > – < small > May 1, 2011< / small > < / span >
< / b >
2011-09-20 23:42:19 -04:00
When running via the < tt > coffee< / tt > executable, < tt > process.argv< / tt > and
2011-05-01 12:08:15 -04:00
friends now report < tt > coffee< / tt > instead of < tt > node< / tt > .
Better compatibility with < b > Node.js 0.4.x< / b > module lookup changes.
The output in the REPL is now colorized, like Node's is.
Giving your concatenated CoffeeScripts a name when using < tt > --join< / tt > is now mandatory.
Fix for lexing compound division < tt > /=< / tt > as a regex accidentally.
All < tt > text/coffeescript< / tt > tags should now execute in the order they're included.
Fixed an issue with extended subclasses using external constructor functions.
Fixed an edge-case infinite loop in < tt > addImplicitParentheses< / tt > .
Fixed exponential slowdown with long chains of function calls.
Globals no longer leak into the CoffeeScript REPL.
Splatted parameters are declared local to the function.
< / p >
2011-04-20 23:08:21 -04:00
2011-01-31 22:39:12 -05:00
< p >
< b class = "header" style = "margin-top: 20px;" > 1.0.1
< span class = "timestamp" > – < small > Jan 31, 2011< / small > < / span >
< / b >
Fixed a lexer bug with Unicode identifiers. Updated REPL for compatibility
with Node.js 0.3.7. Fixed requiring relative paths in the REPL. Trailing
2011-04-20 23:08:21 -04:00
< tt > return< / tt > and < tt > return undefined< / tt > are now optimized away.
Stopped requiring the core Node.js < tt > "util"< / tt > module for
2011-01-31 22:39:12 -05:00
back-compatibility with Node.js 0.2.5. Fixed a case where a
2011-04-20 23:08:21 -04:00
conditional < tt > return< / tt > would cause fallthrough in a < tt > switch< / tt >
2011-01-31 22:39:12 -05:00
statement. Optimized empty objects in destructuring assignment.
< / p >
2010-12-11 19:44:11 -05:00
2010-12-24 14:02:10 -05:00
< p >
< b class = "header" style = "margin-top: 20px;" > 1.0.0
< span class = "timestamp" > – < small > Dec 24, 2010< / small > < / span >
< / b >
CoffeeScript loops no longer try to preserve block scope when functions
are being generated within the loop body. Instead, you can use the
< tt > do< / tt > keyword to create a convenient closure wrapper.
Added a < tt > --nodejs< / tt > flag for passing through options directly
to the < tt > node< / tt > executable.
Better behavior around the use of pure statements within expressions.
Fixed inclusive slicing through < tt > -1< / tt > , for all browsers, and splicing
with arbitrary expressions as endpoints.
< / p >
2010-12-07 00:21:34 -05:00
< p >
2010-12-11 19:44:11 -05:00
< b class = "header" style = "margin-top: 20px;" > 0.9.6
2010-12-07 00:21:34 -05:00
< span class = "timestamp" > – < small > Dec 6, 2010< / small > < / span >
< / b >
The REPL now properly formats stacktraces, and stays alive through
asynchronous exceptions. Using < tt > --watch< / tt > now prints timestamps as
files are compiled. Fixed some accidentally-leaking variables within
2010-12-11 19:44:11 -05:00
plucked closure-loops. Constructors now maintain their declaration
location within a class body. Dynamic object keys were removed.
Nested classes are now supported. Fixes execution context for naked
2010-12-07 00:21:34 -05:00
splatted functions. Bugfix for inversion of chained comparisons.
Chained class instantiation now works properly with splats.
< / p >
2010-09-27 15:21:45 -04:00
2010-11-21 21:12:59 -05:00
< p >
2010-12-11 19:44:11 -05:00
< b class = "header" style = "margin-top: 20px;" > 0.9.5
2010-12-07 00:21:34 -05:00
< span class = "timestamp" > – < small > Nov 21, 2010< / small > < / span >
< / b >
2010-11-21 21:12:59 -05:00
0.9.5 should be considered the first release candidate for CoffeeScript 1.0.
There have been a large number of internal changes since the previous release,
many contributed from < b > satyr< / b > 's < a href = "http://github.com/satyr/coco" > Coco< / a >
dialect of CoffeeScript. Heregexes (extended regexes) were added. Functions
can now have default arguments. Class bodies are now executable code.
2010-12-01 14:17:10 -05:00
Improved syntax errors for invalid CoffeeScript. < tt > undefined< / tt > now
2010-11-21 21:19:49 -05:00
works like < tt > null< / tt > , and cannot be assigned a new value.
2010-12-01 14:17:10 -05:00
There was a precedence change with respect to single-line comprehensions:
2010-12-04 12:52:51 -05:00
< tt > result = i for i in list< / tt > < br / > used to parse as < tt > result = (i for i in list)< / tt >
2010-12-05 15:29:28 -05:00
by default ... it now parses as < br / > < tt > (result = i) for i in list< / tt > .
2010-11-21 21:12:59 -05:00
< / p >
2010-09-21 23:58:05 -04:00
< p >
2010-12-11 19:44:11 -05:00
< b class = "header" style = "margin-top: 20px;" > 0.9.4
2010-12-07 00:21:34 -05:00
< span class = "timestamp" > – < small > Sep 21, 2010< / small > < / span >
< / b >
2010-09-21 23:58:05 -04:00
CoffeeScript now uses appropriately-named temporary variables, and recycles
their references after use. Added < tt > require.extensions< / tt > support for
< b > Node.js 0.3< / b > . Loading CoffeeScript in the browser now adds just a
single < tt > CoffeeScript< / tt > object to global scope.
Fixes for implicit object and block comment edge cases.
< / p >
2010-09-27 15:21:45 -04:00
2010-09-16 00:32:57 -04:00
< p >
2010-12-11 19:44:11 -05:00
< b class = "header" style = "margin-top: 20px;" > 0.9.3
2010-12-07 00:21:34 -05:00
< span class = "timestamp" > – < small > Sep 16, 2010< / small > < / span >
< / b >
2010-09-16 00:32:57 -04:00
CoffeeScript < tt > switch< / tt > statements now compile into JS < tt > switch< / tt >
2010-09-27 15:21:45 -04:00
statements — they previously compiled into < tt > if/else< / tt > chains
2010-09-16 00:32:57 -04:00
for JavaScript 1.3 compatibility.
Soaking a function invocation is now supported. Users of the RubyMine
editor should now be able to use < tt > --watch< / tt > mode.
< / p >
2010-08-23 22:08:33 -04:00
< p >
2010-12-11 19:44:11 -05:00
< b class = "header" style = "margin-top: 20px;" > 0.9.2
2010-12-07 00:21:34 -05:00
< span class = "timestamp" > – < small > Aug 23, 2010< / small > < / span >
< / b >
2010-08-23 22:08:33 -04:00
Specifying the start and end of a range literal is now optional, eg. < tt > array[3..]< / tt > .
You can now say < tt > a not instanceof b< / tt > .
Fixed important bugs with nested significant and non-significant indentation (Issue #637).
Added a < tt > --require< / tt > flag that allows you to hook into the < tt > coffee< / tt > command.
Added a custom < tt > jsl.conf< / tt > file for our preferred JavaScriptLint setup.
Sped up Jison grammar compilation time by flattening rules for operations.
Block comments can now be used with JavaScript-minifier-friendly syntax.
Added JavaScript's compound assignment bitwise operators. Bugfixes to
implicit object literals with leading number and string keys, as the subject
of implicit calls, and as part of compound assignment.
< / p >
2010-08-11 00:40:15 -04:00
< p >
2010-12-11 19:44:11 -05:00
< b class = "header" style = "margin-top: 20px;" > 0.9.1
2010-12-07 00:21:34 -05:00
< span class = "timestamp" > – < small > Aug 11, 2010< / small > < / span >
< / b >
2010-08-11 00:40:15 -04:00
Bugfix release for < b > 0.9.1< / b > . Greatly improves the handling of mixed
2010-08-23 22:08:33 -04:00
implicit objects, implicit function calls, and implicit indentation.
2010-08-11 00:40:15 -04:00
String and regex interpolation is now strictly < tt > #{ ... }< / tt > (Ruby style).
The compiler now takes a < tt > --require< / tt > flag, which specifies scripts
to run before compilation.
< / p >
2010-07-17 10:28:06 -04:00
2010-08-03 01:13:31 -04:00
< p >
2010-12-11 19:44:11 -05:00
< b class = "header" style = "margin-top: 20px;" > 0.9.0
2010-12-07 00:21:34 -05:00
< span class = "timestamp" > – < small > Aug 4, 2010< / small > < / span >
< / b >
2010-08-03 01:13:31 -04:00
The CoffeeScript < b > 0.9< / b > series is considered to be a release candidate
for < b > 1.0< / b > ; let's give her a shakedown cruise. < b > 0.9.0< / b > introduces a massive
backwards-incompatible change: Assignment now uses < tt > =< / tt > , and object
literals use < tt > :< / tt > , as in JavaScript. This allows us to have implicit
object literals, and YAML-style object definitions. Half assignments are
removed, in favor of < tt > +=< / tt > , < tt > or=< / tt > , and friends.
2010-08-23 22:08:33 -04:00
Interpolation now uses a hash mark < tt > #< / tt > instead of the dollar sign
2010-08-04 23:14:34 -04:00
< tt > $< / tt > — because dollar signs may be part of a valid JS identifier.
2010-08-03 01:13:31 -04:00
Downwards range comprehensions are now safe again, and are optimized to
straight for loops when created with integer endpoints.
A fast, unguarded form of object comprehension was added:
< tt > for all key, value of object< / tt > . Mentioning the < tt > super< / tt > keyword
with no arguments now forwards all arguments passed to the function,
as in Ruby. If you extend class < tt > B< / tt > from parent class < tt > A< / tt > , if
< tt > A< / tt > has an < tt > extended< / tt > method defined, it will be called, passing in < tt > B< / tt > —
this enables static inheritance, among other things. Cleaner output for
functions bound with the fat arrow. < tt > @variables< / tt > can now be used
in parameter lists, with the parameter being automatically set as a property
on the object — useful in constructors and setter functions.
Constructor functions can now take splats.
< / p >
2010-08-23 22:08:33 -04:00
2010-07-13 23:39:58 -04:00
< p >
2010-12-11 19:44:11 -05:00
< b class = "header" style = "margin-top: 20px;" > 0.7.2
2010-12-07 00:21:34 -05:00
< span class = "timestamp" > – < small > Jul 12, 2010< / small > < / span >
< / b >
2010-07-13 23:39:58 -04:00
Quick bugfix (right after 0.7.1) for a problem that prevented < tt > coffee< / tt >
command-line options from being parsed in some circumstances.
< / p >
2010-07-17 10:28:06 -04:00
2010-07-11 10:40:44 -04:00
< p >
2010-12-11 19:44:11 -05:00
< b class = "header" style = "margin-top: 20px;" > 0.7.1
2010-12-07 00:21:34 -05:00
< span class = "timestamp" > – < small > Jul 11, 2010< / small > < / span >
< / b >
2010-07-11 10:40:44 -04:00
Block-style comments are now passed through and printed as JavaScript block
comments -- making them useful for licenses and copyright headers. Better
support for running coffee scripts standalone via hashbangs.
Improved syntax errors for tokens that are not in the grammar.
< / p >
2010-07-17 10:28:06 -04:00
2010-06-28 01:00:53 -04:00
< p >
2010-12-11 19:44:11 -05:00
< b class = "header" style = "margin-top: 20px;" > 0.7.0
2010-12-07 00:21:34 -05:00
< span class = "timestamp" > – < small > Jun 28, 2010< / small > < / span >
< / b >
2010-06-28 01:00:53 -04:00
Official CoffeeScript variable style is now camelCase, as in JavaScript.
Reserved words are now allowed as object keys, and will be quoted for you.
Range comprehensions now generate cleaner code, but you have to specify < tt > by -1< / tt >
2010-07-17 10:28:06 -04:00
if you'd like to iterate downward. Reporting of syntax errors is greatly
2010-06-28 01:00:53 -04:00
improved from the previous release. Running < tt > coffee< / tt > with no arguments
2010-06-30 19:51:49 -04:00
now launches the REPL, with Readline support. The < tt > < -< / tt > bind operator
2010-06-28 01:00:53 -04:00
has been removed from CoffeeScript. The < tt > loop< / tt > keyword was added,
which is equivalent to a < tt > while true< / tt > loop. Comprehensions that contain
closures will now close over their variables, like the semantics of a < tt > forEach< / tt > .
You can now use bound function in class definitions (bound to the instance).
For consistency, < tt > a in b< / tt > is now an array presence check, and < tt > a of b< / tt >
is an object-key check. Comments are no longer passed through to the generated
JavaScript.
< / p >
2010-05-15 01:18:05 -04:00
< p >
2010-12-11 19:44:11 -05:00
< b class = "header" style = "margin-top: 20px;" > 0.6.2
2010-12-07 00:21:34 -05:00
< span class = "timestamp" > – < small > May 15, 2010< / small > < / span >
< / b >
2010-05-15 01:18:05 -04:00
The < tt > coffee< / tt > command will now preserve directory structure when
compiling a directory full of scripts. Fixed two omissions that were preventing
the CoffeeScript compiler from running live within Internet Explorer.
There's now a syntax for block comments, similar in spirit to CoffeeScript's heredocs.
ECMA Harmony DRY-style pattern matching is now supported, where the name
of the property is the same as the name of the value: < tt > {name, length}: func< / tt > .
Pattern matching is now allowed within comprehension variables. < tt > unless< / tt >
is now allowed in block form. < tt > until< / tt > loops were added, as the inverse
of < tt > while< / tt > loops. < tt > switch< / tt > statements are now allowed without
switch object clauses. Compatible
with Node.js < b > v0.1.95< / b > .
< / p >
2010-04-12 21:20:00 -04:00
< p >
2010-12-11 19:44:11 -05:00
< b class = "header" style = "margin-top: 20px;" > 0.6.1
2010-12-07 00:21:34 -05:00
< span class = "timestamp" > – < small > Apr 12, 2010< / small > < / span >
< / b >
2010-04-12 21:20:00 -04:00
Upgraded CoffeeScript for compatibility with the new Node.js < b > v0.1.90< / b >
series.
< / p >
2010-04-10 13:42:39 -04:00
2010-04-03 20:43:50 -04:00
< p >
2010-12-11 19:44:11 -05:00
< b class = "header" style = "margin-top: 20px;" > 0.6.0
2010-12-07 00:21:34 -05:00
< span class = "timestamp" > – < small > Apr 3, 2010< / small > < / span >
< / b >
2010-04-10 13:42:39 -04:00
Trailing commas are now allowed, a-la Python. Static
properties may be assigned directly within class definitions,
2010-04-03 20:43:50 -04:00
using < tt > @property< / tt > notation.
< / p >
2010-03-08 06:34:07 -05:00
2010-03-23 00:18:50 -04:00
< p >
2010-12-11 19:44:11 -05:00
< b class = "header" style = "margin-top: 20px;" > 0.5.6
2010-12-07 00:21:34 -05:00
< span class = "timestamp" > – < small > Mar 23, 2010< / small > < / span >
< / b >
2010-03-23 00:18:50 -04:00
Interpolation can now be used within regular expressions and heredocs, as well as
strings. Added the < tt > < -< / tt > bind operator.
Allowing assignment to half-expressions instead of special < tt > ||=< / tt > -style
operators. The arguments object is no longer automatically converted into
an array. After requiring < tt > coffee-script< / tt > , Node.js can now directly
load < tt > .coffee< / tt > files, thanks to < b > registerExtension< / b > . Multiple
splats can now be used in function calls, arrays, and pattern matching.
< / p >
2010-03-08 06:34:07 -05:00
< p >
2010-12-11 19:44:11 -05:00
< b class = "header" style = "margin-top: 20px;" > 0.5.5
2010-12-07 00:21:34 -05:00
< span class = "timestamp" > – < small > Mar 8, 2010< / small > < / span >
< / b >
2010-03-08 06:34:07 -05:00
String interpolation, contributed by
< a href = "http://github.com/StanAngeloff" > Stan Angeloff< / a > .
Since < tt > --run< / tt > has been the default since < b > 0.5.3< / b > , updating
< tt > --stdio< / tt > and < tt > --eval< / tt > to run by default, pass < tt > --compile< / tt >
as well if you'd like to print the result.
< / p >
2010-03-03 23:01:53 -05:00
< p >
2010-12-11 19:44:11 -05:00
< b class = "header" style = "margin-top: 20px;" > 0.5.4
2010-12-07 00:21:34 -05:00
< span class = "timestamp" > – < small > Mar 3, 2010< / small > < / span >
< / b >
2010-03-03 23:01:53 -05:00
Bugfix that corrects the Node.js global constants < tt > __filename< / tt > and
< tt > __dirname< / tt > . Tweaks for more flexible parsing of nested function
literals and improperly-indented comments. Updates for the latest Node.js API.
< / p >
2010-03-08 06:34:07 -05:00
2010-02-27 20:21:46 -05:00
< p >
2010-12-11 19:44:11 -05:00
< b class = "header" style = "margin-top: 20px;" > 0.5.3
2010-12-07 00:21:34 -05:00
< span class = "timestamp" > – < small > Feb 27, 2010< / small > < / span >
< / b >
2010-03-08 06:34:07 -05:00
CoffeeScript now has a syntax for defining classes. Many of the core
2010-02-27 20:21:46 -05:00
components (Nodes, Lexer, Rewriter, Scope, Optparse) are using them.
Cakefiles can use < tt > optparse.coffee< / tt > to define options for tasks.
< tt > --run< / tt > is now the default flag for the < tt > coffee< / tt > command,
use < tt > --compile< / tt > to save JavaScripts. Bugfix for an ambiguity between
RegExp literals and chained divisions.
< / p >
2010-02-25 18:20:15 -05:00
2010-02-25 00:26:59 -05:00
< p >
2010-12-11 19:44:11 -05:00
< b class = "header" style = "margin-top: 20px;" > 0.5.2
2010-12-07 00:21:34 -05:00
< span class = "timestamp" > – < small > Feb 25, 2010< / small > < / span >
< / b >
2010-02-25 00:26:59 -05:00
Added a compressed version of the compiler for inclusion in web pages as
< br / > < tt > extras/coffee-script.js< / tt > . It'll automatically run any script tags
with type < tt > text/coffeescript< / tt > for you. Added a < tt > --stdio< / tt > option
to the < tt > coffee< / tt > command, for piped-in compiles.
< / p >
2010-02-25 18:20:15 -05:00
2010-02-24 18:27:10 -05:00
2010-02-24 00:54:07 -05:00
< p >
2010-12-11 19:44:11 -05:00
< b class = "header" style = "margin-top: 20px;" > 0.5.1
2010-12-07 00:21:34 -05:00
< span class = "timestamp" > – < small > Feb 24, 2010< / small > < / span >
< / b >
2010-02-24 00:54:07 -05:00
Improvements to null soaking with the existential operator, including
soaks on indexed properties. Added conditions to < tt > while< / tt > loops,
so you can use them as filters with < tt > when< / tt > , in the same manner as
comprehensions.
< / p >
2010-02-21 23:29:31 -05:00
2010-02-21 18:37:52 -05:00
< p >
2010-12-11 19:44:11 -05:00
< b class = "header" style = "margin-top: 20px;" > 0.5.0
2010-12-07 00:21:34 -05:00
< span class = "timestamp" > – < small > Feb 21, 2010< / small > < / span >
< / b >
2010-02-21 19:19:58 -05:00
CoffeeScript 0.5.0 is a major release, While there are no language changes,
the Ruby compiler has been removed in favor of a self-hosting
compiler written in pure CoffeeScript.
2010-02-21 18:37:52 -05:00
< / p >
2010-02-13 15:25:04 -05:00
2010-02-08 10:58:49 -05:00
< p >
2010-12-11 19:44:11 -05:00
< b class = "header" style = "margin-top: 20px;" > 0.3.2
2010-12-07 00:21:34 -05:00
< span class = "timestamp" > – < small > Feb 8, 2010< / small > < / span >
< / b >
2010-02-08 10:58:49 -05:00
< 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-25 03:16:56 -05:00
2009-12-24 12:54:12 -05:00
< p >
2010-12-11 19:44:11 -05:00
< b class = "header" style = "margin-top: 20px;" > 0.3.0
2010-12-07 00:21:34 -05:00
< span class = "timestamp" > – < small > Jan 26, 2010< / small > < / span >
< / b >
2010-01-26 23:23:59 -05:00
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 12:54:12 -05:00
< / p >
2009-12-25 03:16:56 -05:00
2010-01-17 18:12:59 -05:00
< p >
2010-12-11 19:44:11 -05:00
< b class = "header" style = "margin-top: 20px;" > 0.2.6
2010-12-07 00:21:34 -05:00
< span class = "timestamp" > – < small > Jan 17, 2010< / small > < / span >
< / b >
2010-01-17 18:12:59 -05:00
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 >
2010-12-11 19:44:11 -05:00
< b class = "header" style = "margin-top: 20px;" > 0.2.5
2010-12-07 00:21:34 -05:00
< span class = "timestamp" > – < small > Jan 13, 2010< / small > < / span >
< / b >
2010-01-13 23:24:45 -05:00
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 >
2010-12-11 19:44:11 -05:00
< b class = "header" style = "margin-top: 20px;" > 0.2.4
2010-12-07 00:21:34 -05:00
< span class = "timestamp" > – < small > Jan 12, 2010< / small > < / span >
< / b >
2010-01-12 00:09:23 -05:00
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 >
2010-12-11 19:44:11 -05:00
< b class = "header" style = "margin-top: 20px;" > 0.2.3
2010-12-07 00:21:34 -05:00
< span class = "timestamp" > – < small > Jan 11, 2010< / small > < / span >
< / b >
2010-01-11 00:01:16 -05:00
Axed the unsatisfactory < tt > ino< / tt > keyword, replacing it with < tt > of< / tt > for
2010-01-11 09:09:06 -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 >
2010-12-11 19:44:11 -05:00
< b class = "header" style = "margin-top: 20px;" > 0.2.2
2010-12-07 00:21:34 -05:00
< span class = "timestamp" > – < small > Jan 10, 2010< / small > < / span >
< / 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 >
2010-12-11 19:44:11 -05:00
< b class = "header" style = "margin-top: 20px;" > 0.2.1
2010-12-07 00:21:34 -05:00
< span class = "timestamp" > – < small > Jan 5, 2010< / small > < / span >
< / b >
2010-01-05 09:29:10 -05:00
Arguments objects are now converted into real arrays when referenced.
< / p >
2010-01-05 00:19:22 -05:00
< p >
2010-12-11 19:44:11 -05:00
< b class = "header" style = "margin-top: 20px;" > 0.2.0
2010-12-07 00:21:34 -05:00
< span class = "timestamp" > – < small > Jan 5, 2010< / small > < / span >
< / b >
2010-01-05 00:19:22 -05:00
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-09-16 00:32:57 -04:00
< a href = "http://github.com/liamoc" > Liam O'Connor-Davis< / a > for whitespace
2010-01-05 00:19:22 -05:00
and expression help.
< / p >
2009-12-27 15:49:11 -05:00
< p >
2010-12-11 19:44:11 -05:00
< b class = "header" style = "margin-top: 20px;" > 0.1.6
2010-12-07 00:21:34 -05:00
< span class = "timestamp" > – < small > Dec 27, 2009< / small > < / span >
< / b >
2009-12-27 15:49:11 -05:00
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 15:49:11 -05:00
< / p >
2009-12-31 13:01:10 -05:00
2009-12-27 00:25:37 -05:00
< p >
2010-12-11 19:44:11 -05:00
< b class = "header" style = "margin-top: 20px;" > 0.1.5
2010-12-07 00:21:34 -05:00
< span class = "timestamp" > – < small > Dec 26, 2009< / small > < / span >
< / b >
2009-12-27 00:25:37 -05:00
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-27 00:25:37 -05: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 19:35:57 -05:00
2009-12-25 17:43:24 -05:00
< p >
2010-12-11 19:44:11 -05:00
< b class = "header" style = "margin-top: 20px;" > 0.1.4
2010-12-07 00:21:34 -05:00
< span class = "timestamp" > – < small > Dec 25, 2009< / small > < / span >
< / b >
2009-12-25 17:43:24 -05:00
The official CoffeeScript extension is now < tt > .coffee< / tt > instead of
2009-12-25 19:35:57 -05:00
< tt > .cs< / tt > , which properly belongs to
2009-12-25 17:43:24 -05: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 19:35:57 -05: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 17:43:24 -05:00
< / p >
2009-12-25 17:18:05 -05:00
2009-12-25 03:16:56 -05:00
< p >
2010-12-11 19:44:11 -05:00
< b class = "header" style = "margin-top: 20px;" > 0.1.3
2010-12-07 00:21:34 -05:00
< span class = "timestamp" > – < small > Dec 25, 2009< / small > < / span >
< / b >
2009-12-26 11:57:13 -05:00
The < tt > coffee< / tt > command now includes < tt > --interactive< / tt > ,
2009-12-25 03:16:56 -05: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 17:18:05 -05:00
The < tt > aint< / tt > keyword has been replaced by < tt > isnt< / tt > , which goes
2009-12-25 03:16:56 -05: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 17:18:05 -05:00
All assignment operators now use a colon: < tt > +:< / tt > , < tt > -:< / tt > ,
2009-12-25 03:16:56 -05:00
< tt > *:< / tt > , etc.
< / p >
2009-12-24 15:02:28 -05:00
< p >
2010-12-11 19:44:11 -05:00
< b class = "header" style = "margin-top: 20px;" > 0.1.2
2010-12-07 00:21:34 -05:00
< span class = "timestamp" > – < small > Dec 24, 2009< / small > < / span >
< / b >
2009-12-25 03:16:56 -05:00
Fixed a bug with calling < tt > super()< / tt > through more than one level of
2009-12-24 20:05:55 -05:00
inheritance, with the re-addition of the < tt > extends< / tt > keyword.
2009-12-25 03:16:56 -05:00
Added experimental < a href = "http://narwhaljs.org/" > Narwhal< / a >
support (as a Tusk package), contributed by
2009-12-24 20:05:55 -05: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-25 03:16:56 -05:00
2009-12-24 20:05:55 -05:00
< p >
2010-12-11 19:44:11 -05:00
< b class = "header" style = "margin-top: 20px;" > 0.1.1
2010-12-07 00:21:34 -05:00
< span class = "timestamp" > – < small > Dec 24, 2009< / small > < / span >
< / b >
2009-12-24 15:02:28 -05:00
Added < tt > instanceof< / tt > and < tt > typeof< / tt > as operators.
< / p >
2009-12-25 03:16:56 -05:00
2009-12-24 03:12:07 -05:00
< p >
2010-12-11 19:44:11 -05:00
< b class = "header" style = "margin-top: 20px;" > 0.1.0
2010-12-07 00:21:34 -05:00
< span class = "timestamp" > – < small > Dec 24, 2009< / small > < / span >
< / b >
2009-12-24 03:12:07 -05:00
Initial CoffeeScript release.
< / p >
2009-12-21 11:41:45 -05:00
2009-12-19 22:55:58 -05:00
< / div >
2010-02-24 20:41:56 -05:00
< script type = "text/coffeescript" >
2011-09-20 23:42:19 -04:00
sourceFragment = "try:"
2010-02-24 20:41:56 -05:00
2010-02-25 20:39:34 -05:00
# Set up the compilation function, to run when you stop typing.
2010-12-12 21:41:04 -05:00
compileSource = ->
2010-08-03 00:06:34 -04:00
source = $('#repl_source').val()
2010-12-12 21:41:04 -05:00
window.compiledJS = ''
2010-02-24 20:41:56 -05:00
try
2010-12-12 21:41:04 -05:00
window.compiledJS = CoffeeScript.compile source, bare: on
2010-12-24 14:02:10 -05:00
el = $('#repl_results')[0]
if el.innerText
el.innerText = window.compiledJS
else
$(el).text window.compiledJS
2010-08-03 00:06:34 -04:00
$('#error').hide()
2010-02-25 20:39:34 -05:00
catch error
2010-08-03 00:06:34 -04:00
$('#error').text(error.message).show()
2010-02-25 20:39:34 -05:00
2011-09-18 17:56:09 -04:00
# Update permalink
2011-09-20 23:42:19 -04:00
$('#repl_permalink').attr 'href', "##{sourceFragment}#{encodeURIComponent source}"
2011-09-18 17:56:09 -04:00
2010-02-25 20:39:34 -05:00
# Listen for keypresses and recompile.
2010-12-12 21:41:04 -05:00
$('#repl_source').keyup -> compileSource()
2010-02-24 20:41:56 -05:00
2010-02-25 20:39:34 -05:00
# Eval the compiled js.
2011-02-10 11:28:02 -05:00
evalJS = ->
2010-02-24 20:41:56 -05:00
try
2010-12-12 21:41:04 -05:00
eval window.compiledJS
2010-02-24 20:41:56 -05:00
catch error then alert error
2010-12-12 21:41:04 -05:00
# Load the console with a string of CoffeeScript.
window.loadConsole = (coffee) ->
$('#repl_source').val coffee
compileSource()
2010-12-12 21:53:17 -05:00
$('.navigation.try').addClass('active')
false
2010-02-24 20:41:56 -05:00
2010-02-25 20:39:34 -05:00
# Helper to hide the menus.
2010-12-12 21:41:04 -05:00
closeMenus = ->
2010-12-12 21:53:17 -05:00
$('.navigation.active').removeClass 'active'
2010-02-24 20:41:56 -05:00
2011-03-19 18:51:40 -04:00
$('.minibutton.run').click evalJS
2011-02-10 11:28:02 -05:00
2010-02-25 20:39:34 -05:00
# Bind navigation buttons to open the menus.
$('.navigation').click (e) ->
2010-02-24 20:41:56 -05:00
return if e.target.tagName.toLowerCase() is 'a'
2010-12-12 21:41:04 -05:00
return false if $(e.target).closest('.repl_wrapper').length
2010-12-12 21:53:17 -05:00
if $(this).hasClass('active')
2010-12-12 21:41:04 -05:00
closeMenus()
else
closeMenus()
2010-12-12 21:53:17 -05:00
$(this).addClass 'active'
2010-02-24 20:41:56 -05:00
false
2011-02-10 11:28:02 -05:00
# Dismiss console if Escape pressed or click falls outside console
# Trigger Run button on Ctrl-Enter
$(document.body)
.keydown (e) ->
closeMenus() if e.which == 27
evalJS() if e.which == 13 and (e.metaKey or e.ctrlKey) and $('.minibutton.run:visible').length
.click (e) ->
return false if $(e.target).hasClass('minibutton')
closeMenus()
2010-02-27 02:21:26 -05:00
2010-04-10 13:42:39 -04:00
$('#open_webchat').click ->
$(this).replaceWith $('< iframe src = "http://webchat.freenode.net/?channels=coffeescript" width = "625" height = "400" > < / iframe > ')
2011-09-18 17:56:09 -04:00
$("#repl_permalink").click (e) ->
window.location = $(this).attr("href")
2011-09-18 21:43:10 -04:00
false
2011-09-18 17:56:09 -04:00
# If source code is included in location.hash, display it.
hash = decodeURIComponent location.hash.replace(/^#/, '')
2011-09-20 23:42:19 -04:00
if hash.indexOf(sourceFragment) == 0
src = hash.substr sourceFragment.length
2011-09-18 17:56:09 -04:00
loadConsole src
2010-12-12 21:41:04 -05:00
compileSource()
2010-02-25 20:39:34 -05:00
2010-02-24 20:41:56 -05:00
< / script >
2010-02-21 23:29:31 -05:00
2011-09-21 00:27:14 -04:00
< script src = "documentation/vendor/jquery-1.6.4.js" > < / script >
2010-02-24 23:57:39 -05:00
< script src = "extras/coffee-script.js" > < / script >
2010-02-13 16:23:03 -05:00
2009-12-19 22:55:58 -05:00
< / body >
< / html >