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-02-27 02:21:26 -05:00
< body class = "minimized" >
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 >
< div class = "contents" >
< a href = "#overview" > Mini Overview< / a >
< a href = "#installation" > Installation and Usage< / a >
2010-02-21 16:13:09 -05:00
< a href = "#language" > Language Reference< / a >
2010-02-21 14:30:21 -05:00
< a href = "#whitespace" > Significant Whitespace< / a >
< a href = "#functions" > Functions and Invocation< / a >
< a href = "#assignment" > Assignment< / a >
< a href = "#objects_and_arrays" > Objects and Arrays< / a >
< a href = "#lexical_scope" > Lexical Scoping and Variable Safety< / a >
< a href = "#conditionals" > Conditionals, Ternaries, and Conditional Assignment< / a >
< a href = "#aliases" > Aliases< / a >
< a href = "#splats" > Splats...< / a >
2010-06-28 01:00:53 -04:00
< a href = "#while" > While, Until, and Loop< / a >
2010-02-21 14:30:21 -05:00
< a href = "#comprehensions" > Comprehensions (Arrays, Objects, and Ranges)< / a >
< a href = "#slice_splice" > Array Slicing and Splicing with Ranges< / a >
< a href = "#expressions" > Everything is an Expression< / a >
< a href = "#existence" > The Existential Operator< / a >
2010-02-27 20:12:47 -05:00
< a href = "#classes" > Classes, Inheritance, and Super< / a >
2010-02-21 14:30:21 -05:00
< a href = "#pattern_matching" > Pattern Matching< / a >
< a href = "#fat_arrow" > Function Binding< / a >
< a href = "#embedded" > Embedded JavaScript< / a >
< a href = "#switch" > Switch/When/Else< / a >
< a href = "#try" > Try/Catch/Finally< / a >
< a href = "#comparisons" > Chained Comparisons< / a >
2010-03-23 00:18:50 -04:00
< a href = "#interpolation" > String and RegExp Interpolation< / a >
2010-02-21 14:30:21 -05:00
< a href = "#strings" > Multiline Strings and Heredocs< / 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 >
2010-02-21 14:30:21 -05:00
< a href = "#resources" > Resources< / a >
2010-04-10 13:42:39 -04:00
< a href = "#webchat" > Web Chat (IRC)< / 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
< / div >
< div class = "contents repl_wrapper" >
< div class = "code" >
2010-02-27 02:21:26 -05:00
< div id = "repl_source_wrap" > < textarea id = "repl_source" > reverse: (string) ->
2010-02-21 16:36:34 -05:00
string.split('').reverse().join ''
2010-02-21 16:13:09 -05:00
2010-03-08 09:13:15 -05:00
alert reverse '.eeffoC yrT'< / textarea > < / div >
2010-02-21 16:13:09 -05:00
< pre id = "repl_results" > < / pre >
2010-02-27 02:21:26 -05:00
< button class = "full_screen" > go full screen< / button >
< button class = "minimize" > minimize< / button >
2010-02-25 20:39:34 -05:00
< button class = "run" > run< / button >
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 >
< div class = "contents" >
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 >
CoffeeScript is a little language that compiles into JavaScript. Think
2009-12-23 19:42:18 -05:00
of it as JavaScript's less ostentatious kid brother — the same genes,
2009-12-24 03:12:07 -05:00
roughly the same height, but a different sense of style. Apart from a handful of
2009-12-23 20:24:55 -05:00
bonus goodies, statements in CoffeeScript correspond one-to-one with their
2009-12-23 19:42:18 -05:00
equivalent in JavaScript, it's just another way of saying it.
2009-12-21 11:41:45 -05:00
< / p >
< p >
2009-12-22 12:18:27 -05:00
< b > Disclaimer:< / b >
2010-02-21 16:13:09 -05:00
CoffeeScript is just for fun. Until it reaches 1.0, < i > there are no guarantees
2010-01-10 17:36:06 -05:00
that the syntax won't change between versions.< / i > That said,
2009-12-23 19:42:18 -05:00
it compiles into clean JavaScript (the good parts) that can use existing
2009-12-24 02:01:39 -05:00
JavaScript libraries seamlessly, and passes through
2009-12-23 19:42:18 -05:00
< a href = "http://www.jslint.com/" > JSLint< / a > without warnings. The compiled
2010-06-28 01:00:53 -04:00
output is pretty-printed and quite readable.
2009-12-21 11:41:45 -05:00
< / p >
2010-01-04 22:19:45 -05:00
2010-01-04 19:15:24 -05:00
< p >
< b > Latest Version:< / b >
2010-06-28 00:19:58 -04:00
< a href = "http://github.com/jashkenas/coffee-script/tarball/0.7.0" > 0.7.0< / 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 >
Mini Overview
< / 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-03-08 06:34:07 -05:00
number< span class = "Keyword" > :< / span > < span class = "Number" > 42< / span >
2010-06-28 00:19:58 -04:00
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-06-28 00:19:58 -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 >
2010-01-26 23:23:59 -05:00
< span class = "FunctionName" > square< / span > < span class = "Keyword" > :< / span > < span class = "FunctionArgument" > (< / span > < span class = "FunctionArgument" > x< / span > < span class = "FunctionArgument" > )< / 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-03-08 06:34:07 -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 > ]
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-03-08 06:34:07 -05:00
math< span class = "Keyword" > :< / span > {
root< span class = "Keyword" > :< / span > Math.sqrt
square< span class = "Keyword" > :< / span > square
2010-01-26 23:23:59 -05:00
< span class = "FunctionName" > cube< / span > < span class = "Keyword" > :< / span > < span class = "FunctionArgument" > (< / span > < span class = "FunctionArgument" > x< / span > < span class = "FunctionArgument" > )< / 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 >
2010-01-26 23:23:59 -05:00
< span class = "FunctionName" > race< / span > < span class = "Keyword" > :< / span > < span class = "FunctionArgument" > (< / span > < span class = "FunctionArgument" > winner, runners...< / span > < span class = "FunctionArgument" > )< / 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-06-28 00:19:58 -04:00
cubes< span class = "Keyword" > :< / span > math.cube num < span class = "Keyword" > for< / span > num < span class = "Keyword" > in< / span > list
< / pre > < pre class = "idle" > < span class = "Storage" > var< / span > _a, _b, _c, _d, 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;
2010-02-25 17:18:28 -05:00
number < span class = "Keyword" > =< / span > < span class = "Number" > 42< / span > ;
2010-06-28 00:19:58 -04:00
opposite < span class = "Keyword" > =< / span > < span class = "BuiltInConstant" > true< / span > ;
< span class = "Keyword" > if< / span > (opposite) {
2010-02-25 17:18:28 -05:00
number < span class = "Keyword" > =< / span > < span class = "Keyword" > -< / span > < span class = "Number" > 42< / span > ;
}
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;
};
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 > ];
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);
}
};
2010-05-15 01:18:05 -04:00
< span class = "FunctionName" > race< / span > = < span class = "Storage" > function< / span > (< span class = "FunctionArgument" > winner< / span > ) {
2010-02-25 17:18:28 -05:00
< span class = "Storage" > var< / span > runners;
2010-05-15 01:18:05 -04:00
< span class = "Storage" > var< / span > _a < span class = "Keyword" > =< / span > arguments.< span class = "LibraryConstant" > length< / span > , _b < span class = "Keyword" > =< / span > _a < span class = "Keyword" > > =< / span > < span class = "Number" > 2< / span > ;
runners < span class = "Keyword" > =< / span > __slice.< span class = "LibraryFunction" > call< / span > (arguments, < span class = "Number" > 1< / span > , _a < span class = "Keyword" > -< / span > < span class = "Number" > 0< / span > );
2010-02-25 17:18:28 -05:00
< span class = "Keyword" > return< / span > < span class = "LibraryFunction" > print< / span > (winner, runners);
};
2010-06-28 00:19:58 -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 > ) {
2010-02-25 17:18:28 -05:00
< span class = "LibraryFunction" > alert< / span > (< span class = "String" > < span class = "String" > " < / span > I knew it!< span class = "String" > " < / span > < / span > );
}
2010-06-28 00:19:58 -04:00
cubes < span class = "Keyword" > =< / span > (< span class = "Storage" > function< / span > () {
2010-04-03 20:43:50 -04:00
_a < span class = "Keyword" > =< / span > []; _c < span class = "Keyword" > =< / span > list;
< span class = "Keyword" > for< / span > (_b < span class = "Keyword" > =< / span > < span class = "Number" > 0< / span > , _d < span class = "Keyword" > =< / span > _c.< span class = "LibraryConstant" > length< / span > ; _b < span class = "Keyword" > < < / span > _d; _b< span class = "Keyword" > ++< / span > ) {
num < span class = "Keyword" > =< / span > _c[_b];
2010-02-25 17:18:28 -05:00
_a.< span class = "LibraryFunction" > push< / span > (math.cube(num));
}
< span class = "Keyword" > return< / span > _a;
2010-04-11 17:57:29 -04:00
})();
2010-06-28 00:19:58 -04:00
< / pre > < button onclick = 'javascript: var _a , _b , _c , _d , cubes , list , math , num , number , opposite , race , square ;
2010-04-03 20:43:50 -04:00
var __slice = Array.prototype.slice;
2010-02-25 17:18:28 -05:00
number = 42;
2010-06-28 00:19:58 -04:00
opposite = true;
if (opposite) {
2010-02-25 17:18:28 -05:00
number = -42;
}
2010-05-15 01:18:05 -04:00
square = function(x) {
2010-02-25 17:18:28 -05:00
return x * x;
};
list = [1, 2, 3, 4, 5];
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);
}
};
2010-05-15 01:18:05 -04:00
race = function(winner) {
2010-02-25 17:18:28 -05:00
var runners;
2010-05-15 01:18:05 -04:00
var _a = arguments.length, _b = _a >= 2;
runners = __slice.call(arguments, 1, _a - 0);
2010-02-25 17:18:28 -05:00
return print(winner, runners);
};
2010-06-28 00:19:58 -04:00
if (typeof elvis !== "undefined" & & elvis !== null) {
2010-02-25 17:18:28 -05:00
alert("I knew it!");
}
2010-06-28 00:19:58 -04:00
cubes = (function() {
2010-04-03 20:43:50 -04:00
_a = []; _c = list;
for (_b = 0, _d = _c.length; _b < _d ; _b + + ) {
num = _c[_b];
2010-02-25 17:18:28 -05:00
_a.push(math.cube(num));
}
return _a;
2010-04-11 17:57:29 -04:00
})();
2010-06-28 00:19:58 -04:00
;alert(cubes);'>run: cubes< / button > < br class = 'clear' / > < / div >
2010-01-04 23:26:27 -05:00
2010-01-04 22:51:02 -05:00
< p >
2010-01-04 23:26:27 -05:00
For a longer CoffeeScript example, check out
2010-03-16 19:13:13 -04:00
< a href = "documentation/docs/underscore.html" > Underscore.coffee< / a > , a port
2010-01-10 17:14:20 -05:00
of the < a href = "http://documentcloud.github.com/underscore/" > Underscore.js< / a >
library of helper functions. Underscore.coffee can pass the entire Underscore.js
2010-01-11 22:55:01 -05:00
test suite. The CoffeeScript version is faster than the original for a number
2010-01-10 17:14:20 -05:00
of methods (in general, due to the speed of CoffeeScript's array comprehensions), and
after being minified and gzipped, is only 241 bytes larger than the original
JavaScript version.
Additional examples are included in the source repository, inside the
2010-01-05 00:45:04 -05:00
< a href = "http://github.com/jashkenas/coffee-script/tree/master/examples/" > examples< / a > folder.
2010-01-04 22:51:02 -05:00
< / p >
2009-12-23 19:42:18 -05:00
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-02-24 18:27:10 -05:00
The CoffeeScript compiler is written in pure CoffeeScript, using a
2010-03-08 06:40:54 -05:00
< a href = "documentation/docs/grammar.html" > small DSL< / a >
2010-02-22 17:14:51 -05:00
on top of the < a href = "http://github.com/zaach/jison" > Jison parser generator< / a > , and is available
2010-02-21 17:30:41 -05:00
as a < a href = "http://nodejs.org/" > Node.js< / a > utility. The core compiler however,
does not depend on Node, and can be run in other server-side-JavaScript environments,
2010-02-22 20:25:15 -05:00
or in the browser (see "Try CoffeeScript", above). This may be helpful,
as Node only run on flavors of nix, and not Windows, for the time being.
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-04-03 20:43:50 -04:00
To install, first make sure you have a working copy of the latest tagged version of
2010-04-12 21:20:00 -04:00
< a href = "http://nodejs.org/" > Node.js< / a > , currently < b > 0.1.90< / b > or higher.
2010-03-03 23:01:53 -05:00
Then clone the CoffeeScript
2010-02-21 19:28:12 -05:00
< a href = "http://github.com/jashkenas/coffee-script" > source repository< / a >
from GitHub, or download the latest
2010-06-28 00:19:58 -04:00
release: < a href = "http://github.com/jashkenas/coffee-script/tarball/0.7.0" > 0.7.0< / a > .
2010-02-21 19:28:12 -05:00
To install the CoffeeScript compiler system-wide
under < tt > /usr/local< / tt > , open the directory and run:
2009-12-24 03:12:07 -05:00
< / p >
2009-12-23 19:42:18 -05:00
< pre >
2010-02-21 18:37:52 -05:00
sudo bin/cake install< / pre >
2009-12-23 19:42:18 -05:00
< p >
2010-06-28 01:06:53 -04:00
Alternatively, if you already have the
< a href = "http://npmjs.org/" > Node Package Manager< / a > installed,
you can use that to grab the latest CoffeeScript:
< / p >
< pre >
sudo npm install coffee-script< / pre >
< p >
Both of these provide the < tt > coffee< / tt > command, which will execute CoffeeScripts
2010-02-27 02:21:26 -05:00
under Node.js by default, but is also used to compile CoffeeScript
2010-02-27 00:38:04 -05:00
< tt > .coffee< / tt > files into JavaScript, or to run an an interactive REPL.
2009-12-26 11:57:13 -05:00
When compiling to JavaScript, < tt > coffee< / tt > writes the output
2009-12-25 03:16:56 -05:00
as < tt > .js< / tt > files in the same directory by default, but output
2009-12-23 19:42:18 -05:00
can be customized with the following options:
< / p >
< table >
< tr >
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.
More pleasant if wrapped with
2010-03-07 18:19:41 -05:00
< a href = "http://utopia.knoware.nl/~hlub/uck/rlwrap/rlwrap.html" > rlwrap< / a > .
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 >
< tr >
< td > < code > -w, --watch< / code > < / td >
< td >
2009-12-24 02:01:39 -05:00
Watch the modification times of the coffee-scripts, recompiling as
2009-12-23 19:42:18 -05:00
soon as a change occurs.
< / td >
< / tr >
< tr >
< td > < code > -p, --print< / code > < / td >
< td >
Instead of writing out the JavaScript as a file, print it
directly to < b > stdout< / b > .
< / td >
< / tr >
< tr >
< td > < code > -l, --lint< / code > < / td >
< td >
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
2009-12-24 02:01:39 -05:00
conjunction with < 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
2010-03-08 06:40:54 -05:00
command line. For example:< br / > < tt > coffee -e "puts num for num in [10..1]"< / tt >
2009-12-23 19:42:18 -05:00
< / td >
< / tr >
2009-12-24 20:37:24 -05:00
< tr >
2010-02-25 18:43:33 -05:00
< td > < code > --no-wrap< / code > < / td >
2009-12-24 20:37:24 -05:00
< td >
2010-01-10 17:14:20 -05:00
Compile the JavaScript without the top-level function safety wrapper.
2010-01-29 23:30:54 -05:00
(Used for CoffeeScript as a Node.js module.)
2010-01-10 17:14:20 -05:00
< / td >
< / tr >
< tr >
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
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 >
2009-12-23 19:42:18 -05:00
< / table >
< p >
< b > Examples:< / b >
< / p >
< pre >
2010-02-27 00:38:04 -05:00
coffee -c path/to/script.coffee
2009-12-26 11:57:13 -05:00
coffee --interactive
coffee --watch --lint experimental.coffee
coffee --print app/scripts/*.coffee > concatenation.js< / pre >
2009-12-23 19:42:18 -05:00
2010-02-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 >
Many of the examples can be run (where it makes sense) by pressing the "run"
2010-02-21 18:37:52 -05:00
button towards the bottom right. You can also paste examples into
2010-02-21 17:30:41 -05:00
"Try CoffeeScript" in the toolbar, and play with them from there.
< / i >
2010-02-21 14:30:21 -05:00
< p >
< span id = "whitespace" class = "bookmark" > < / span >
2010-01-04 22:19:45 -05:00
< b class = "header" > Significant Whitespace< / b >
CoffeeScript uses Python-style significant whitespace: You don't need to
use semicolons < tt > ;< / tt > to terminate expressions, ending
the line will do just as well. Semicolons can still be used to fit
multiple expressions onto a single line. Instead of using curly braces
2010-01-05 00:19:22 -05:00
< tt > { }< / tt > to delimit blocks of code (like < a href = "#functions" > functions< / a > ,
2009-12-25 03:16:56 -05:00
< a href = "#conditionals" > if-statements< / a > ,
2010-01-04 22:19:45 -05:00
< a href = "#switch" > switch< / a > , and < a href = "#try" > try/catch< / a > ),
use indentation.
2009-12-23 19:42:18 -05:00
< / p >
2010-01-26 23:23:59 -05:00
< p >
2010-02-21 17:30:41 -05:00
You don't need to use parentheses to invoke a function if you're passing
2010-06-28 00:19:58 -04:00
arguments:< br / > < tt > print "coffee"< / tt > . Implicit parentheses wrap forwards
to the end of the line, or block expression.
2010-01-26 23:23:59 -05:00
< / p >
2010-01-05 21:40:36 -05:00
< p >
You can use newlines to break up your expression into smaller pieces,
2010-02-21 17:30:41 -05:00
as long as CoffeeScript can determine that the line hasn't finished yet,
because it ends with an operator or a dot.
2010-01-05 21:40:36 -05:00
< / p >
2010-02-21 14:30:21 -05:00
< p >
< span id = "functions" class = "bookmark" > < / span >
2009-12-23 19:42:18 -05:00
< b class = "header" > Functions and Invocation< / b >
2009-12-25 03:16:56 -05:00
Functions are defined by a list of parameters, an arrow, and the
2010-02-21 17:30:41 -05:00
function body. The empty function looks like this: < tt > ->< / tt > All
2010-01-17 18:33:31 -05:00
functions in CoffeeScript are named by default, for easier debugging.
2009-12-23 19:42:18 -05:00
< / p >
2010-01-26 23:23:59 -05:00
< div class = 'code' > < pre class = "idle" > < span class = "FunctionName" > square< / span > < span class = "Keyword" > :< / span > < span class = "FunctionArgument" > (< / span > < span class = "FunctionArgument" > x< / span > < span class = "FunctionArgument" > )< / span > < span class = "Storage" > -> < / span > x < span class = "Keyword" > *< / span > x
< span class = "FunctionName" > cube< / span > < span class = "Keyword" > :< / span > < span class = "FunctionArgument" > (< / span > < span class = "FunctionArgument" > x< / span > < span class = "FunctionArgument" > )< / 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;
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;
};
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;
};
2009-12-26 02:17:34 -05:00
< / pre > < button onclick = 'javascript: var cube , square ;
2010-05-15 01:18:05 -04:00
square = function(x) {
2009-12-21 11:41:45 -05:00
return x * x;
};
2010-05-15 01:18:05 -04:00
cube = function(x) {
2009-12-21 11:41:45 -05:00
return square(x) * x;
};
;alert(cube(5));'>run: cube(5)< / button > < br class = 'clear' / > < / div >
2010-01-17 18:33:31 -05:00
< p >
2010-02-21 18:37:52 -05:00
If you'd like to assign a function literal to a variable, but not have
2010-02-21 17:30:41 -05:00
it be named, just wrap the function definition in parentheses:
2010-01-26 10:52:05 -05:00
< tt > ((x) -> x * x)< / tt >
2010-01-17 18:33:31 -05:00
< / p >
2009-12-21 11:41:45 -05:00
2010-02-21 14:30:21 -05:00
< p >
< span id = "assignment" class = "bookmark" > < / span >
2009-12-23 19:42:18 -05:00
< b class = "header" > Assignment< / b >
Use a colon < tt > :< / tt > to assign, as in
< a href = "http://json.org" > JSON< / a > . Equal signs are only needed for
2010-02-16 18:39:31 -05:00
mathy things. While colons are preferred, the two may be used interchangeably,
2010-02-15 13:43:35 -05:00
even within object literals.
2009-12-23 19:42:18 -05:00
< / p >
2010-03-08 06:34:07 -05:00
< div class = 'code' > < pre class = "idle" > greeting< span class = "Keyword" > :< / span > < span class = "String" > < span class = "String" > " < / span > Hello CoffeeScript< span class = "String" > " < / span > < / span >
difficulty< span class = "Keyword" > :< / span > < span class = "Number" > 0.5< / span >
2009-12-26 02:17:34 -05:00
< / pre > < pre class = "idle" > < span class = "Storage" > var< / span > difficulty, greeting;
greeting < span class = "Keyword" > =< / span > < span class = "String" > < span class = "String" > " < / span > Hello CoffeeScript< span class = "String" > " < / span > < / span > ;
difficulty < span class = "Keyword" > =< / span > < span class = "Number" > 0.5< / span > ;
< / pre > < button onclick = 'javascript: var difficulty , greeting ;
greeting = "Hello CoffeeScript";
difficulty = 0.5;
2009-12-23 19:42:18 -05:00
;alert(greeting);'>run: greeting< / button > < br class = 'clear' / > < / div >
2009-12-26 02:17:34 -05:00
< p >
2010-02-21 17:30:41 -05:00
All declaration of new variables is pushed up to the top of the nearest
2010-01-04 23:26:27 -05:00
lexical scope, so that assignment may always be performed within expressions.
2009-12-26 02:17:34 -05:00
< / p >
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 >
Object and Array literals look very similar to their JavaScript cousins.
When you spread out each assignment on a separate line, the commas are
optional. In this way, assigning object properties looks the same as
2010-01-17 18:33:31 -05:00
assigning local variables, and can be moved around freely. Feel free to mix
2010-01-04 22:19:45 -05:00
and match the two styles.
2009-12-23 19:42:18 -05:00
< / p >
2010-03-08 06:34:07 -05: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-01-04 22:19:45 -05:00
2010-03-08 06:34:07 -05:00
ages< span class = "Keyword" > :< / span > {
max< span class = "Keyword" > :< / span > < span class = "Number" > 10< / span >
ida< span class = "Keyword" > :< / span > < span class = "Number" > 9< / span >
tim< span class = "Keyword" > :< / span > < span class = "Number" > 11< / span >
2009-12-21 11:41:45 -05:00
}
2010-01-04 22:19:45 -05:00
2010-03-08 06:34:07 -05:00
matrix< 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 >
]
< / pre > < pre class = "idle" > < span class = "Storage" > var< / span > ages, matrix, song;
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 > ];
ages < span class = "Keyword" > =< / span > {
2009-12-21 11:41:45 -05:00
max: < span class = "Number" > 10< / span > ,
ida: < span class = "Number" > 9< / span > ,
tim: < span class = "Number" > 11< / span >
};
2010-01-04 22:19:45 -05:00
matrix < 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 > ];
< / pre > < button onclick = 'javascript: var ages , matrix , song ;
2009-12-26 02:17:34 -05:00
song = ["do", "re", "mi", "fa", "so"];
ages = {
2009-12-21 11:41:45 -05:00
max: 10,
ida: 9,
tim: 11
};
2010-01-04 22:19:45 -05:00
matrix = [1, 0, 1, 0, 0, 1, 1, 1, 0];
2009-12-21 11:41:45 -05:00
;alert(song.join(","));'>run: song.join(",")< / button > < 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
of an object, without quoting them as strings. CoffeeScript notices and quotes
them for you, so you don't have to worry about it (say, when using jQuery).
< / p >
< div class = 'code' > < pre class = "idle" > $(< span class = "String" > < span class = "String" > '< / span > .account< span class = "String" > '< / span > < / span > ).css {class< span class = "Keyword" > :< / span > < span class = "String" > < span class = "String" > '< / span > active< span class = "String" > '< / span > < / span > }
< / pre > < pre class = "idle" > < span class = "Keyword" > $< / span > (< span class = "String" > < span class = "String" > '< / span > .account< span class = "String" > '< / span > < / span > ).css({
< 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 >
});
< / pre > < 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-06-28 00:19:58 -04:00
< div class = 'code' > < pre class = "idle" > outer< span class = "Keyword" > :< / span > < span class = "Number" > 1< / span >
< span class = "FunctionName" > changeNumbers< / span > < span class = "Keyword" > :< / span > < span class = "Storage" > -> < / span >
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()
< / pre > < pre class = "idle" > < span class = "Storage" > var< / span > changeNumbers, inner, outer;
outer < span class = "Keyword" > =< / span > < span class = "Number" > 1< / span > ;
< 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 > ;
outer < span class = "Keyword" > =< / span > < span class = "Number" > 10< / span > ;
< span class = "Keyword" > return< / span > outer;
2009-12-21 11:41:45 -05:00
};
2010-06-28 00:19:58 -04:00
inner < span class = "Keyword" > =< / span > changeNumbers();
< / pre > < button onclick = 'javascript: var changeNumbers , inner , outer ;
outer = 1;
changeNumbers = function() {
var inner;
inner = -1;
outer = 10;
return outer;
2009-12-21 11:41:45 -05:00
};
2010-06-28 00:19:58 -04:00
inner = changeNumbers();
;alert(inner);'>run: inner< / button > < br class = 'clear' / > < / div >
2009-12-23 19:42:18 -05:00
< p >
2010-01-04 22:19:45 -05:00
Notice how the all of the variable declarations have been pushed up to
the top of the closest scope, the first time they appear.
< b > num< / b > is not redeclared within the inner function, because it's
already in scope; the < b > new_num< / b > within the function, on the other hand,
should not be able to change the value of the external variable of the same name, and
therefore has a declaration of its own.
2009-12-23 19:42:18 -05:00
< / p >
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 >
object in CommonJS. The < b > existential operator< / b > (below), gives you a
2010-01-17 18:33:31 -05:00
reliable way to figure out where to add them, if you're targeting both
CommonJS and the browser: < tt > root: exports ? this< / tt >
2009-12-23 19:42:18 -05:00
< / p >
2010-02-21 14:30:21 -05:00
< p >
< span id = "conditionals" class = "bookmark" > < / span >
2009-12-23 19:42:18 -05:00
< b class = "header" > Conditionals, Ternaries, and Conditional Assignment< / b >
2009-12-28 21:02:40 -05:00
< b > If/else< / b > statements can be written without the use of parentheses and
2010-01-04 22:19:45 -05:00
curly brackets. As with functions and other block expressions,
multi-line conditionals are delimited by indentation. There's also a handy
postfix form, with the < tt > if< / tt > or < tt > unless< / tt > at the end.
2009-12-23 19:42:18 -05:00
< / p >
2009-12-24 04:38:32 -05:00
< p >
CoffeeScript will compile < b > if< / b > statements using the ternary operator
when possible, to make it easier to use the result as an expression.
< / p >
2010-06-28 00:19:58 -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()
2009-12-21 11:41:45 -05:00
2010-03-08 06:34:07 -05: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-06-28 00:19:58 -04:00
options< span class = "Keyword" > :< / span > < span class = "Keyword" > or< / span > defaultOptions
< / pre > < pre class = "idle" > < span class = "Storage" > var< / span > date, mood, options;
2009-12-21 11:41:45 -05:00
< span class = "Keyword" > if< / span > (singing) {
2010-06-28 00:19:58 -04:00
mood < span class = "Keyword" > =< / span > greatlyImproved;
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" > & < / span > < span class = "Keyword" > & < / span > knowsIt) {
clapsHands();
chaChaCha();
2009-12-21 11:41:45 -05:00
}
2009-12-26 02:17:34 -05:00
date < span class = "Keyword" > =< / span > friday ? sue : jill;
2010-06-28 00:19:58 -04:00
options < span class = "Keyword" > =< / span > options < span class = "Keyword" > ||< / span > defaultOptions;
2009-12-21 11:41:45 -05:00
< / pre > < br class = 'clear' / > < / div >
2009-12-23 19:42:18 -05:00
< p >
2010-03-23 00:18:50 -04:00
You can assign a variable to a half-expression to perform an operation
2010-04-10 13:42:39 -04:00
like Ruby's < tt > ||=< / tt > , which only assigns a value to a variable
2010-03-23 00:18:50 -04:00
if the variable's current value is falsy.
2009-12-23 19:42:18 -05:00
< / p >
2010-02-21 14:30:21 -05:00
< p >
< span id = "aliases" class = "bookmark" > < / span >
2009-12-24 02:01:39 -05:00
< b class = "header" > Aliases< / b >
Because the < tt > ==< / tt > operator frequently causes undesirable coercion,
is intransitive, and has a different meaning than in other languages,
CoffeeScript compiles < tt > ==< / tt > into < tt > ===< / tt > , and < tt > !=< / tt > into
< tt > !==< / tt > .
In addition, < tt > is< / tt > compiles into < tt > ===< / tt > ,
2009-12-24 20:21:20 -05:00
and < tt > isnt< / tt > into < tt > !==< / tt > .
2009-12-24 02:01:39 -05:00
< / p >
< p >
You can use < tt > not< / tt > as an alias for < tt > !< / tt > .
< / p >
2009-12-24 17:37:30 -05:00
< p >
For logic, < tt > and< / tt > compiles to < tt > & & < / tt > , and < tt > or< / tt >
into < tt > ||< / tt > .
< / p >
2009-12-24 04:33:59 -05:00
< p >
Instead of a newline or semicolon, < tt > then< / tt > can be used to separate
2009-12-25 03:16:56 -05:00
conditions from expressions, in < b > while< / b > ,
2009-12-24 04:33:59 -05:00
< b > if< / b > /< b > else< / b > , and < b > switch< / b > /< b > when< / b > statements.
< / p >
2009-12-24 02:01:39 -05:00
< p >
As in < a href = "http://yaml.org/" > YAML< / a > , < tt > on< / tt > and < tt > yes< / tt >
are the same as boolean < tt > true< / tt > , while < tt > off< / tt > and < tt > no< / tt > are boolean < tt > false< / tt > .
< / p >
< p >
For single-line statements, < tt > unless< / tt > can be used as the inverse of < tt > if< / tt > .
< / p >
2010-02-08 10:58:49 -05:00
< p >
As a shortcut for < tt > this.property< / tt > , you can use < tt > @property< / tt > .
< / p >
2010-06-28 01:00:53 -04:00
< p >
You can use < tt > in< / tt > to test for array presence, and < tt > of< / tt > to
test for JavaScript object-key presence.
< / p >
2009-12-31 13:01:10 -05:00
< div class = 'code' > < pre class = "idle" > launch() < span class = "Keyword" > if< / span > ignition < span class = "Keyword" > is< / span > < span class = "BuiltInConstant" > on< / span >
2009-12-24 02:01:39 -05:00
2010-06-28 00:19:58 -04:00
volume< span class = "Keyword" > :< / span > < span class = "Number" > 10< / span > < span class = "Keyword" > if< / span > band < span class = "Keyword" > isnt< / span > SpinalTap
2009-12-24 02:01:39 -05:00
2010-06-28 00:19:58 -04:00
letTheWildRumpusBegin() < span class = "Keyword" > unless< / span > answer < span class = "Keyword" > is< / span > < span class = "BuiltInConstant" > no< / span >
2009-12-24 04:33:59 -05:00
2010-06-28 00:19:58 -04:00
< span class = "Keyword" > if< / span > car.speed < span class = "Keyword" > < < / span > limit < span class = "Keyword" > then< / span > accelerate()
2010-02-08 10:58:49 -05:00
2010-06-28 01:00:53 -04:00
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 > ]
2010-02-08 10:58:49 -05:00
print < span class = "String" > < span class = "String" > " < / span > My name is < span class = "String" > " < / span > < / span > < span class = "Keyword" > +< / span > < span class = "Variable" > @name< / span >
2010-06-28 01:00:53 -04:00
< / pre > < pre class = "idle" > < span class = "Storage" > var< / span > volume, winner;
2009-12-26 02:17:34 -05:00
< span class = "Keyword" > if< / span > (ignition < span class = "Keyword" > ===< / span > < span class = "BuiltInConstant" > true< / span > ) {
2009-12-24 02:01:39 -05:00
launch();
}
2010-06-28 00:19:58 -04:00
< span class = "Keyword" > if< / span > (band < span class = "Keyword" > !< / span > < span class = "Keyword" > ==< / span > SpinalTap) {
2009-12-24 02:01:39 -05:00
volume < span class = "Keyword" > =< / span > < span class = "Number" > 10< / span > ;
}
< span class = "Keyword" > if< / span > (< span class = "Keyword" > !< / span > (answer < span class = "Keyword" > ===< / span > < span class = "BuiltInConstant" > false< / span > )) {
2010-06-28 00:19:58 -04:00
letTheWildRumpusBegin();
2009-12-24 02:01:39 -05:00
}
2010-06-28 00:19:58 -04:00
car.speed < span class = "Keyword" > < < / span > limit ? accelerate() : < span class = "BuiltInConstant" > null< / span > ;
2010-06-28 01:00:53 -04:00
< span class = "Keyword" > if< / 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 > < span class = "Keyword" > ===< / span > pick) {
winner < span class = "Keyword" > =< / span > < span class = "BuiltInConstant" > true< / span > ;
}
2010-02-08 10:58:49 -05:00
< span class = "LibraryFunction" > print< / span > (< 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 > );
2009-12-24 02:01:39 -05:00
< / pre > < 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-06-28 00:19:58 -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
2010-06-28 00:19:58 -04:00
< span class = "FunctionName" > awardMedals< / span > < span class = "Keyword" > :< / span > < span class = "FunctionArgument" > (< / span > < span class = "FunctionArgument" > first, second, rest...< / span > < span class = "FunctionArgument" > )< / span > < span class = "Storage" > -> < / span >
gold< span class = "Keyword" > :< / span > first
silver< span class = "Keyword" > :< / span > second
rest< span class = "Keyword" > :< / span > rest
2010-01-04 23:26:27 -05:00
2010-03-08 06:34:07 -05: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
< / 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;
2010-06-28 00:19:58 -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 > ));
< span class = "FunctionName" > awardMedals< / span > = < span class = "Storage" > function< / span > (< span class = "FunctionArgument" > first, second< / span > ) {
2010-05-15 01:18:05 -04:00
< span class = "Storage" > var< / span > _a < span class = "Keyword" > =< / span > arguments.< span class = "LibraryConstant" > length< / span > , _b < span class = "Keyword" > =< / span > _a < span class = "Keyword" > > =< / span > < span class = "Number" > 3< / span > ;
rest < span class = "Keyword" > =< / span > __slice.< span class = "LibraryFunction" > call< / span > (arguments, < span class = "Number" > 2< / span > , _a < span class = "Keyword" > -< / span > < span class = "Number" > 0< / span > );
2010-01-04 23:26:27 -05:00
gold < span class = "Keyword" > =< / span > first;
silver < span class = "Keyword" > =< / span > second;
2010-06-28 00:19:58 -04:00
rest < span class = "Keyword" > =< / span > rest;
< span class = "Keyword" > return< / span > rest;
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 > ];
2010-06-28 00:19:58 -04:00
awardMedals.< span class = "LibraryFunction" > apply< / span > (< span class = "Variable" > this< / span > , contenders);
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);
< span class = "LibraryFunction" > alert< / span > (< 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
< span class = "LibraryFunction" > alert< / span > (< span class = "String" > < span class = "String" > " < / span > The Field: < span class = "String" > " < / span > < / span > < span class = "Keyword" > +< / span > rest);
< / pre > < button onclick = 'javascript: var awardMedals , contenders , gold , rest , silver ;
2010-04-03 20:43:50 -04:00
var __slice = Array.prototype.slice;
2010-06-28 00:19:58 -04:00
gold = (silver = (rest = "unknown"));
awardMedals = function(first, second) {
2010-05-15 01:18:05 -04:00
var _a = arguments.length, _b = _a >= 3;
rest = __slice.call(arguments, 2, _a - 0);
2010-01-04 23:26:27 -05:00
gold = first;
silver = second;
2010-06-28 00:19:58 -04:00
rest = rest;
return rest;
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"];
2010-06-28 00:19:58 -04:00
awardMedals.apply(this, contenders);
2010-01-04 23:26:27 -05:00
alert("Gold: " + gold);
alert("Silver: " + silver);
2010-06-28 00:19:58 -04:00
alert("The Field: " + rest);
2010-01-04 23:26:27 -05:00
;'>run< / button > < br class = 'clear' / > < / div >
2010-02-21 14:30:21 -05:00
< p >
< span id = "while" class = "bookmark" > < / span >
2010-06-28 01:00:53 -04:00
< b class = "header" > While, Until, and Loop< / b >
2010-01-13 23:24:45 -05:00
The only low-level loop that CoffeeScript provides is the < b > while< / b > loop. The
main difference from JavaScript is that the < b > while< / b > loop can be used
as an expression, returning an array containing the result of each iteration
through the loop.
2009-12-23 19:42:18 -05:00
< / p >
2010-01-17 18:33:31 -05:00
< div class = 'code' > < pre class = "idle" > < span class = "Comment" > < span class = "Comment" > #< / span > Econ 101< / span >
2010-06-28 00:19:58 -04:00
< span class = "Keyword" > if< / span > < span class = "Variable" > this< / span > .studyingEconomics
2010-05-15 01:18:05 -04:00
buy() < span class = "Keyword" > while< / span > supply < span class = "Keyword" > > < / span > demand
sell() < span class = "Keyword" > until< / span > supply < span class = "Keyword" > > < / span > demand
2010-01-13 23:24:45 -05:00
2010-01-17 18:33:31 -05:00
< span class = "Comment" > < span class = "Comment" > #< / span > Nursery Rhyme< / span >
2010-03-08 06:34:07 -05:00
num< span class = "Keyword" > :< / span > < span class = "Number" > 6< / span >
2010-06-28 01:00:53 -04:00
lyrics< span class = "Keyword" > :< / span > < span class = "Keyword" > while< / span > num< span class = "Keyword" > :< / span > < span class = "Keyword" > -< / span > < span class = "Number" > 1< / span >
2010-01-13 23:24:45 -05:00
num < span class = "Keyword" > +< / span > < span class = "String" > < span class = "String" > " < / span > little monkeys, jumping on the bed.< / span >
< span class = "String" > One fell out and bumped his head.< span class = "String" > " < / span > < / span >
2010-02-21 11:45:03 -05:00
< / pre > < pre class = "idle" > < span class = "Storage" > var< / span > _a, lyrics, num;
2010-06-28 00:19:58 -04:00
< span class = "Keyword" > if< / span > (< span class = "Variable" > this< / span > .studyingEconomics) {
2010-01-13 23:24:45 -05:00
< span class = "Keyword" > while< / span > (supply < span class = "Keyword" > > < / span > demand) {
buy();
}
2010-05-15 01:18:05 -04:00
< span class = "Keyword" > while< / span > (< span class = "Keyword" > !< / span > (supply < span class = "Keyword" > > < / span > demand)) {
2010-01-13 23:24:45 -05:00
sell();
}
2009-12-21 11:41:45 -05:00
}
2010-01-13 23:24:45 -05:00
num < span class = "Keyword" > =< / span > < span class = "Number" > 6< / span > ;
lyrics < span class = "Keyword" > =< / span > (< span class = "Storage" > function< / span > () {
2010-02-21 11:45:03 -05:00
_a < span class = "Keyword" > =< / span > [];
2010-01-13 23:24:45 -05:00
< span class = "Keyword" > while< / span > (num < span class = "Keyword" > -< / span > < span class = "Keyword" > =< / span > < span class = "Number" > 1< / span > ) {
2010-02-21 11:45:03 -05:00
_a.< span class = "LibraryFunction" > push< / span > (num < span class = "Keyword" > +< / span > < span class = "String" > < span class = "String" > " < / span > little monkeys, jumping on the bed. \< / span >
2010-01-13 23:24:45 -05:00
< span class = "String" > One fell out and bumped his head.< span class = "String" > " < / span > < / span > );
}
2010-02-21 11:45:03 -05:00
< span class = "Keyword" > return< / span > _a;
2010-04-11 17:57:29 -04:00
})();
2010-02-21 11:45:03 -05:00
< / pre > < button onclick = 'javascript: var _a , lyrics , num ;
2010-06-28 00:19:58 -04:00
if (this.studyingEconomics) {
2010-01-13 23:24:45 -05:00
while (supply > demand) {
buy();
}
2010-05-15 01:18:05 -04:00
while (!(supply > demand)) {
2010-01-13 23:24:45 -05:00
sell();
}
2009-12-21 11:41:45 -05:00
}
2010-01-13 23:24:45 -05:00
num = 6;
lyrics = (function() {
2010-02-21 11:45:03 -05:00
_a = [];
2010-01-13 23:24:45 -05:00
while (num -= 1) {
2010-02-21 11:45:03 -05:00
_a.push(num + " little monkeys, jumping on the bed. \
2010-01-13 23:24:45 -05:00
One fell out and bumped his head.");
}
2010-02-21 11:45:03 -05:00
return _a;
2010-04-11 17:57:29 -04:00
})();
2010-01-13 23:24:45 -05:00
;alert(lyrics.join("\n"));'>run: lyrics.join("\n")< / button > < br class = 'clear' / > < / div >
2009-12-23 19:42:18 -05:00
< p >
2010-06-28 01:00:53 -04:00
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 > .
2009-12-23 20:24:55 -05:00
Other JavaScript loops, such as < b > for< / b > loops and < b > do-while< / b > loops
2010-06-28 01:00:53 -04:00
can be mimicked by variations on < b > loop< / b > , but the hope is that you
2009-12-23 20:24:55 -05:00
won't need to do that with CoffeeScript, either because you're using
2009-12-23 19:42:18 -05:00
< b > each< / b > (< b > forEach< / b > ) style iterators, or...
< / p >
2009-12-21 11:41:45 -05:00
2010-02-21 14:30:21 -05:00
< p >
< span id = "comprehensions" class = "bookmark" > < / span >
2010-01-04 22:19:45 -05:00
< b class = "header" > Comprehensions (Arrays, Objects, and Ranges)< / b >
2009-12-23 20:24:55 -05:00
For your looping needs, CoffeeScript provides array comprehensions
2009-12-23 19:42:18 -05:00
similar to Python's. They replace (and compile into) < b > for< / b > loops, with
optional guard clauses and the value of the current array index.
Unlike for loops, array comprehensions are expressions, and can be returned
and assigned. They should be able to handle most places where you otherwise
would use a loop, < b > each< / b > /< b > forEach< / b > , < b > map< / b > , or < b > select< / b > /< b > filter< / b > .
< / p >
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-03-08 06:34:07 -05:00
lunch< span class = "Keyword" > :< / span > 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 > ]
2009-12-21 11:41:45 -05:00
2010-01-04 22:19:45 -05:00
< span class = "Comment" > < span class = "Comment" > #< / span > Naive collision detection.< / span >
< span class = "Keyword" > for< / span > roid < span class = "Keyword" > in< / span > asteroids
< span class = "Keyword" > for< / span > roid2 < span class = "Keyword" > in< / span > asteroids < span class = "Keyword" > when< / span > roid < span class = "Keyword" > isnt< / span > roid2
2010-01-25 00:14:00 -05:00
roid.explode() < span class = "Keyword" > if< / span > roid.overlaps roid2
2010-02-25 23:39:14 -05:00
< / pre > < pre class = "idle" > < span class = "Storage" > var< / span > _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, food, lunch, roid, roid2;
2010-01-04 19:15:24 -05:00
lunch < span class = "Keyword" > =< / span > (< span class = "Storage" > function< / span > () {
2010-04-03 20:43:50 -04:00
_a < span class = "Keyword" > =< / span > []; _c < 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 > (_b < span class = "Keyword" > =< / span > < span class = "Number" > 0< / span > , _d < span class = "Keyword" > =< / span > _c.< span class = "LibraryConstant" > length< / span > ; _b < span class = "Keyword" > < < / span > _d; _b< span class = "Keyword" > ++< / span > ) {
food < span class = "Keyword" > =< / span > _c[_b];
2010-02-21 11:45:03 -05:00
_a.< span class = "LibraryFunction" > push< / span > (eat(food));
2009-12-31 16:50:46 -05:00
}
2010-02-21 11:45:03 -05:00
< span class = "Keyword" > return< / span > _a;
2010-04-11 17:57:29 -04:00
})();
2010-04-03 20:43:50 -04:00
_f < span class = "Keyword" > =< / span > asteroids;
< span class = "Keyword" > for< / span > (_e < span class = "Keyword" > =< / span > < span class = "Number" > 0< / span > , _g < span class = "Keyword" > =< / span > _f.< span class = "LibraryConstant" > length< / span > ; _e < span class = "Keyword" > < < / span > _g; _e< span class = "Keyword" > ++< / span > ) {
roid < span class = "Keyword" > =< / span > _f[_e];
_i < span class = "Keyword" > =< / span > asteroids;
< span class = "Keyword" > for< / span > (_h < span class = "Keyword" > =< / span > < span class = "Number" > 0< / span > , _j < span class = "Keyword" > =< / span > _i.< span class = "LibraryConstant" > length< / span > ; _h < span class = "Keyword" > < < / span > _j; _h< span class = "Keyword" > ++< / span > ) {
roid2 < span class = "Keyword" > =< / span > _i[_h];
2010-01-10 14:45:44 -05:00
< span class = "Keyword" > if< / span > (roid < span class = "Keyword" > !< / span > < span class = "Keyword" > ==< / span > roid2) {
< span class = "Keyword" > if< / span > (roid.overlaps(roid2)) {
roid.explode();
2010-01-04 22:19:45 -05:00
}
}
2009-12-31 16:50:46 -05:00
}
2009-12-21 11:41:45 -05:00
}
< / pre > < br class = 'clear' / > < / div >
2009-12-26 23:46:31 -05:00
< p >
2010-01-04 22:19:45 -05:00
If you know the start and end of your loop, or would like to step through
in fixed-size increments, you can use a range to specify the start and
2010-06-28 00:19:58 -04:00
end of your comprehension.
2010-01-10 15:52:23 -05:00
< / p >
2010-06-28 00:19:58 -04: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 > ] < span class = "Keyword" > by< / span > < span class = "Keyword" > -< / span > < span class = "Number" > 1< / span >
2010-01-10 15:52:23 -05:00
2010-06-28 00:19:58 -04:00
< span class = "FunctionName" > deliverEggs< / span > < span class = "Keyword" > :< / span > < span class = "Storage" > -> < / span >
2010-01-10 15:52:23 -05:00
< span class = "Keyword" > for< / span > i < span class = "Keyword" > in< / span > [< span class = "Number" > 0< / span > ...eggs.length] < span class = "Keyword" > by< / span > < span class = "Number" > 12< / span >
2010-06-28 00:19:58 -04:00
dozen< span class = "Keyword" > :< / span > eggs[i...i< span class = "Keyword" > +< / span > < span class = "Number" > 12< / span > ]
deliver < span class = "Keyword" > new< / span > < span class = "TypeName" > eggCarton< / span > (dozen)
< / pre > < pre class = "idle" > < span class = "Storage" > var< / span > _a, countdown, deliverEggs, num;
2010-01-10 15:52:23 -05:00
countdown < span class = "Keyword" > =< / span > (< span class = "Storage" > function< / span > () {
2010-06-28 00:19:58 -04:00
_a < 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 > < span class = "Keyword" > =< / span > < span class = "Keyword" > -< / span > < span class = "Number" > 1< / span > ) {
2010-02-21 11:45:03 -05:00
_a.< span class = "LibraryFunction" > push< / span > (num);
2010-01-10 15:52:23 -05:00
}
2010-02-21 11:45:03 -05:00
< span class = "Keyword" > return< / span > _a;
2010-04-11 17:57:29 -04:00
})();
2010-06-28 00:19:58 -04:00
< span class = "FunctionName" > deliverEggs< / span > = < span class = "Storage" > function< / span > () {
< span class = "Storage" > var< / span > _b, _c, dozen, i;
_b < span class = "Keyword" > =< / span > []; (_c < span class = "Keyword" > =< / span > eggs.< span class = "LibraryConstant" > length< / span > );
< span class = "Keyword" > for< / span > (i < span class = "Keyword" > =< / span > < span class = "Number" > 0< / span > ; i < span class = "Keyword" > < < / span > _c; i < span class = "Keyword" > +< / span > < span class = "Keyword" > =< / span > < span class = "Number" > 12< / span > ) {
_b.< span class = "LibraryFunction" > push< / span > ((< span class = "Storage" > function< / span > () {
dozen < span class = "Keyword" > =< / span > eggs.< span class = "LibraryFunction" > slice< / span > (i, i < span class = "Keyword" > +< / span > < span class = "Number" > 12< / span > );
< span class = "Keyword" > return< / span > deliver(< span class = "Keyword" > new< / span > < span class = "TypeName" > eggCarton< / span > (dozen));
2010-04-11 17:57:29 -04:00
})());
2010-01-10 15:52:23 -05:00
}
2010-06-28 00:19:58 -04:00
< span class = "Keyword" > return< / span > _b;
2010-01-10 15:52:23 -05:00
};
2010-06-28 00:19:58 -04:00
< / pre > < button onclick = 'javascript: var _a , countdown , deliverEggs , num ;
2010-01-10 15:52:23 -05:00
countdown = (function() {
2010-06-28 00:19:58 -04:00
_a = [];
for (num = 10; num >= 1; num += -1) {
2010-02-21 11:45:03 -05:00
_a.push(num);
2010-01-10 15:52:23 -05:00
}
2010-02-21 11:45:03 -05:00
return _a;
2010-04-11 17:57:29 -04:00
})();
2010-06-28 00:19:58 -04:00
deliverEggs = function() {
var _b, _c, dozen, i;
_b = []; (_c = eggs.length);
for (i = 0; i < _c ; i + = 12 ) {
_b.push((function() {
dozen = eggs.slice(i, i + 12);
return deliver(new eggCarton(dozen));
2010-04-11 17:57:29 -04:00
})());
2010-01-10 15:52:23 -05:00
}
2010-06-28 00:19:58 -04:00
return _b;
2010-01-10 15:52:23 -05:00
};
;alert(countdown);'>run: countdown< / button > < br class = 'clear' / > < / div >
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-06-28 00:19:58 -04:00
< div class = 'code' > < pre class = "idle" > yearsOld< span class = "Keyword" > :< / span > {max< span class = "Keyword" > :< / span > < span class = "Number" > 10< / span > , ida< span class = "Keyword" > :< / span > < span class = "Number" > 9< / span > , tim< span class = "Keyword" > :< / span > < span class = "Number" > 11< / span > }
2010-01-10 15:52:23 -05:00
2010-06-28 00:19:58 -04:00
ages< span class = "Keyword" > :< / span > < span class = "Keyword" > for< / span > child, age < span class = "Keyword" > of< / span > yearsOld
2010-01-10 23:27:57 -05:00
child < span class = "Keyword" > +< / span > < span class = "String" > < span class = "String" > " < / span > is < span class = "String" > " < / span > < / span > < span class = "Keyword" > +< / span > age
2010-06-28 00:19:58 -04:00
< / pre > < pre class = "idle" > < span class = "Storage" > var< / span > _a, _b, age, ages, child, yearsOld;
2010-01-13 23:24:45 -05:00
< span class = "Storage" > var< / span > __hasProp < span class = "Keyword" > =< / span > < span class = "LibraryClassType" > Object< / span > .< span class = "LibraryConstant" > prototype< / span > .hasOwnProperty;
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 >
};
ages < span class = "Keyword" > =< / span > (< span class = "Storage" > function< / span > () {
2010-06-28 00:19:58 -04:00
_a < span class = "Keyword" > =< / span > []; _b < span class = "Keyword" > =< / span > yearsOld;
2010-02-21 11:45:03 -05:00
< span class = "Keyword" > for< / span > (child < span class = "Keyword" > in< / span > _b) { < span class = "Keyword" > if< / span > (__hasProp.< span class = "LibraryFunction" > call< / span > (_b, child)) {
age < span class = "Keyword" > =< / span > _b[child];
_a.< span class = "LibraryFunction" > push< / span > (child < span class = "Keyword" > +< / span > < span class = "String" > < span class = "String" > " < / span > is < span class = "String" > " < / span > < / span > < span class = "Keyword" > +< / span > age);
}}
< span class = "Keyword" > return< / span > _a;
2010-04-11 17:57:29 -04:00
})();
2010-06-28 00:19:58 -04:00
< / pre > < button onclick = 'javascript: var _a , _b , age , ages , child , yearsOld ;
2010-01-13 23:24:45 -05:00
var __hasProp = Object.prototype.hasOwnProperty;
2010-06-28 00:19:58 -04:00
yearsOld = {
2010-01-10 15:52:23 -05:00
max: 10,
ida: 9,
tim: 11
};
ages = (function() {
2010-06-28 00:19:58 -04:00
_a = []; _b = yearsOld;
2010-02-21 11:45:03 -05:00
for (child in _b) { if (__hasProp.call(_b, child)) {
age = _b[child];
_a.push(child + " is " + age);
}}
return _a;
2010-04-11 17:57:29 -04:00
})();
2010-01-10 15:52:23 -05:00
;alert(ages.join(", "));'>run: ages.join(", ")< / button > < br class = 'clear' / > < / div >
2009-12-21 11:41:45 -05:00
2010-02-21 14:30:21 -05:00
< p >
< span id = "slice_splice" class = "bookmark" > < / span >
2010-01-04 22:19:45 -05:00
< b class = "header" > Array Slicing and Splicing with Ranges< / b >
2009-12-25 19:35:57 -05:00
CoffeeScript borrows Ruby's
< a href = "http://ruby-doc.org/core/classes/Range.html" > range syntax< / a >
for extracting slices of arrays. With two dots (< tt > 3..5< / tt > ), the range
is inclusive: the first argument is the index of the first element in
the slice, and the second is the index of the last one. Three dots signify
a range that excludes the end.
< / p >
2010-03-08 06:34:07 -05:00
< 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 > ]
2009-12-25 19:35:57 -05:00
2010-06-28 00:19:58 -04:00
threeToSix< span class = "Keyword" > :< / span > numbers[< span class = "Number" > 3< / span > ..< span class = "Number" > 6< / span > ]
2009-12-25 19:35:57 -05:00
2010-06-28 00:19:58 -04:00
copy< span class = "Keyword" > :< / span > numbers[< span class = "Number" > 0< / span > ...numbers.length]
2009-12-25 19:35:57 -05:00
2010-06-28 00:19:58 -04:00
< / pre > < pre class = "idle" > < span class = "Storage" > var< / span > copy, numbers, threeToSix;
2009-12-26 02:17:34 -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 > ];
2010-06-28 00:19:58 -04:00
threeToSix < span class = "Keyword" > =< / span > numbers.< span class = "LibraryFunction" > slice< / span > (< span class = "Number" > 3< / span > , < span class = "Number" > 6< / span > < span class = "Keyword" > +< / span > < span class = "Number" > 1< / span > );
copy < span class = "Keyword" > =< / span > numbers.< span class = "LibraryFunction" > slice< / span > (< span class = "Number" > 0< / span > , numbers.< span class = "LibraryConstant" > length< / span > );
< / pre > < button onclick = 'javascript: var copy , numbers , threeToSix ;
2009-12-26 02:17:34 -05:00
numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
2010-06-28 00:19:58 -04:00
threeToSix = numbers.slice(3, 6 + 1);
copy = numbers.slice(0, numbers.length);
;alert(copy);'>run: copy< / button > < br class = 'clear' / > < / div >
2010-01-04 22:19:45 -05:00
< p >
The same syntax can be used with assignment to replace a segment of an
array with new values (to splice it).
< / p >
2010-03-08 06:34:07 -05:00
< 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 > ]
2010-01-04 22:19:45 -05:00
2010-03-08 19:27:38 -05:00
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-01-04 22:19:45 -05:00
< / pre > < pre class = "idle" > < span class = "Storage" > var< / span > numbers;
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.splice.< span class = "LibraryFunction" > apply< / span > (numbers, [< span class = "Number" > 3< / span > , < span class = "Number" > 6< / span > < span class = "Keyword" > -< / span > < span class = "Number" > 3< / span > < span class = "Keyword" > +< / span > < span class = "Number" > 1< / span > ].< span class = "LibraryFunction" > concat< / 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 > ]));
< / pre > < button onclick = 'javascript: var numbers ;
numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
numbers.splice.apply(numbers, [3, 6 - 3 + 1].concat([-3, -4, -5, -6]));
;alert(numbers);'>run: numbers< / button > < br class = 'clear' / > < / div >
2010-01-04 23:26:27 -05:00
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
pushed down into each possible branch of execution, in the function
below.
< / p >
2010-01-26 23:23:59 -05:00
< div class = 'code' > < pre class = "idle" > < span class = "FunctionName" > grade< / span > < span class = "Keyword" > :< / span > < span class = "FunctionArgument" > (< / span > < span class = "FunctionArgument" > student< / span > < span class = "FunctionArgument" > )< / 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-03-08 06:34:07 -05: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;
2010-05-15 01:18:05 -04:00
< span class = "FunctionName" > grade< / span > = < span class = "Storage" > function< / span > (< span class = "FunctionArgument" > student< / 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 = "Keyword" > return< / span > < 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) {
2010-03-23 00:18:50 -04:00
< 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 > ;
}
2010-01-04 22:51:02 -05:00
} < span class = "Keyword" > else< / span > {
< span class = "Keyword" > return< / span > < span class = "String" > < span class = "String" > " < / span > C< span class = "String" > " < / span > < / span > ;
}
};
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 > ;
< / pre > < button onclick = 'javascript: var eldest , grade ;
2010-05-15 01:18:05 -04:00
grade = function(student) {
2010-06-28 00:19:58 -04:00
if (student.excellentWork) {
2010-01-04 22:51:02 -05:00
return "A+";
2010-06-28 00:19:58 -04:00
} else if (student.okayStuff) {
if (student.triedHard) {
2010-03-23 00:18:50 -04:00
return "B";
} else {
return "B-";
}
2010-01-04 22:51:02 -05:00
} else {
return "C";
}
};
eldest = 24 > 21 ? "Liz" : "Ike";
;alert(eldest);'>run: eldest< / button > < 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-03-08 06:34:07 -05: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-01-04 22:51:02 -05:00
< / pre > < pre class = "idle" > < span class = "Storage" > var< / span > one, six, three, two;
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-01-04 22:51:02 -05:00
< / pre > < button onclick = 'javascript: var one , six , three , two ;
2010-03-23 00:18:50 -04:00
six = (one = 1) + (two = 2) + (three = 3);
2010-01-04 22:51:02 -05:00
;alert(six);'>run: six< / button > < br class = 'clear' / > < / div >
< 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-03-08 06:34:07 -05: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-02-21 11:45:03 -05:00
< / pre > < pre class = "idle" > < span class = "Storage" > var< / span > _a, _b, globals, name;
2010-01-13 23:24:45 -05:00
< span class = "Storage" > var< / span > __hasProp < span class = "Keyword" > =< / span > < span class = "LibraryClassType" > Object< / span > .< span class = "LibraryConstant" > prototype< / span > .hasOwnProperty;
2010-02-21 11:45:03 -05:00
globals < span class = "Keyword" > =< / span > (< span class = "Storage" > function< / span > () {
_a < span class = "Keyword" > =< / span > []; _b < span class = "Keyword" > =< / span > < span class = "LibraryClassType" > window< / span > ;
< span class = "Keyword" > for< / span > (name < span class = "Keyword" > in< / span > _b) { < span class = "Keyword" > if< / span > (__hasProp.< span class = "LibraryFunction" > call< / span > (_b, name)) {
_a.< span class = "LibraryFunction" > push< / span > (name);
}}
< span class = "Keyword" > return< / span > _a;
2010-04-11 17:57:29 -04:00
})().< span class = "LibraryFunction" > slice< / span > (< span class = "Number" > 0< / span > , < span class = "Number" > 10< / span > );
2010-02-21 11:45:03 -05:00
< / pre > < button onclick = 'javascript: var _a , _b , globals , name ;
2010-01-13 23:24:45 -05:00
var __hasProp = Object.prototype.hasOwnProperty;
2010-02-21 11:45:03 -05:00
globals = (function() {
_a = []; _b = window;
for (name in _b) { if (__hasProp.call(_b, name)) {
_a.push(name);
}}
return _a;
2010-04-11 17:57:29 -04:00
})().slice(0, 10);
2010-01-04 22:51:02 -05:00
;alert(globals);'>run: globals< / button > < br class = 'clear' / > < / div >
< 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
2010-01-17 18:33:31 -05:00
< 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
)
< / pre > < pre class = "idle" > < span class = "LibraryFunction" > alert< / span > ((< span class = "Storage" > function< / span > () {
< span class = "Keyword" > try< / span > {
< span class = "Keyword" > return< / span > nonexistent / undefined;
} < 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-04-11 17:57:29 -04:00
})());
2010-01-04 22:51:02 -05:00
< / pre > < button onclick = 'javascript: alert ( ( function ( ) {
try {
return nonexistent / undefined;
} catch (error) {
2010-01-17 18:33:31 -05:00
return "And the error is ... " + error;
2010-01-04 22:51:02 -05:00
}
2010-04-11 17:57:29 -04:00
})());
2010-01-04 22:51:02 -05:00
;'>run< / button > < 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-02-13 15:25:04 -05:00
2010-02-21 14:30:21 -05:00
< p >
< span id = "existence" class = "bookmark" > < / span >
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-03-08 06:34:07 -05: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
speed < span class = "Keyword" > ?< / span > < span class = "Keyword" > =< / span > < span class = "Number" > 140< / span >
< / pre > < pre class = "idle" > < span class = "Storage" > var< / span > solipsism, speed;
2010-02-13 15:25:04 -05: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 > ;
}
speed < span class = "Keyword" > =< / span > (< span class = "Keyword" > typeof< / span > speed < 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 > speed < span class = "Keyword" > !< / span > < span class = "Keyword" > ==< / span > < span class = "BuiltInConstant" > null< / span > ) ? speed : < span class = "Number" > 140< / span > ;
< / pre > < button onclick = 'javascript: var solipsism , speed ;
2010-02-13 15:25:04 -05:00
if ((typeof mind !== "undefined" & & mind !== null) & & !(typeof world !== "undefined" & & world !== null)) {
2010-01-26 23:23:59 -05:00
solipsism = true;
}
speed = (typeof speed !== "undefined" & & speed !== null) ? speed : 140;
;alert(speed);'>run: speed< / button > < br class = 'clear' / > < / div >
< 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-06-28 00:19:58 -04:00
< div class = 'code' > < pre class = "idle" > lottery.drawWinner()< span class = "Keyword" > ?< / span > .address< span class = "Keyword" > ?< / span > .zipcode
2010-02-21 11:45:03 -05:00
< / pre > < pre class = "idle" > < span class = "Storage" > var< / span > _a;
2010-06-28 00:19:58 -04:00
< span class = "Keyword" > typeof< / span > (_a < span class = "Keyword" > =< / span > (lottery.drawWinner())) < span class = "Keyword" > ===< / span > < span class = "String" > < span class = "String" > " < / span > undefined< span class = "String" > " < / span > < / span > < span class = "Keyword" > ||< / span > _a < span class = "Keyword" > ==< / span > undefined ? undefined : _a.address < span class = "Keyword" > ==< / span > undefined ? undefined : _a.address.zipcode;
2010-01-26 23:23:59 -05:00
< / pre > < br class = 'clear' / > < / div >
< 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-03-08 06:34:07 -05:00
< div class = 'code' > < pre class = "idle" > < span class = "Storage" > class< / span > < span class = "TypeName" > Animal< / span >
2010-02-27 20:12:47 -05:00
< span class = "FunctionName" > move< / span > < span class = "Keyword" > :< / span > < span class = "FunctionArgument" > (< / span > < span class = "FunctionArgument" > meters< / span > < span class = "FunctionArgument" > )< / span > < span class = "Storage" > -> < / span >
alert < span class = "Variable" > @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-02-13 15:25:04 -05:00
2010-03-08 06:34:07 -05:00
< span class = "Storage" > class< / span > < span class = "TypeName" > Snake< / span > < span class = "InheritedClass" > < span class = "Keyword" > extends< / span > Animal< / span >
2010-02-27 20:12:47 -05:00
< span class = "FunctionName" > constructor< / span > < span class = "Keyword" > :< / span > < span class = "FunctionArgument" > (< / span > < span class = "FunctionArgument" > name< / span > < span class = "FunctionArgument" > )< / span > < span class = "Storage" > -> < / span >
< span class = "Variable" > @name< / span > < span class = "Keyword" > :< / span > name
2009-12-21 11:41:45 -05:00
2010-02-27 20:12:47 -05:00
< span class = "FunctionName" > move< / span > < span class = "Keyword" > :< / span > < span class = "Storage" > -> < / span >
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
2010-03-08 06:34:07 -05:00
< span class = "Storage" > class< / span > < span class = "TypeName" > Horse< / span > < span class = "InheritedClass" > < span class = "Keyword" > extends< / span > Animal< / span >
2010-02-27 20:12:47 -05:00
< span class = "FunctionName" > constructor< / span > < span class = "Keyword" > :< / span > < span class = "FunctionArgument" > (< / span > < span class = "FunctionArgument" > name< / span > < span class = "FunctionArgument" > )< / span > < span class = "Storage" > -> < / span >
< span class = "Variable" > @name< / span > < span class = "Keyword" > :< / span > name
2010-02-15 13:55:28 -05:00
2010-02-27 20:12:47 -05:00
< span class = "FunctionName" > move< / span > < span class = "Keyword" > :< / span > < span class = "Storage" > -> < / span >
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-03-08 06:34:07 -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 >
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;
< span class = "Storage" > var< / span > < span class = "FunctionName" > __extends< / span > = < span class = "Storage" > function< / span > (< span class = "FunctionArgument" > child, parent< / span > ) {
< span class = "Storage" > var< / span > < span class = "FunctionName" > ctor< / span > = < span class = "Storage" > function< / span > (){ };
< span class = "LibraryClassType" > ctor< / span > .< span class = "LibraryConstant" > prototype< / span > = parent.< span class = "LibraryConstant" > prototype< / span > ;
child.__superClass__ < span class = "Keyword" > =< / 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 > ();
< span class = "LibraryClassType" > child< / span > .< span class = "LibraryConstant" > prototype< / span > .< span class = "FunctionName" > constructor< / span > = child;
};
2010-05-15 01:18:05 -04:00
< span class = "FunctionName" > Animal< / span > = < span class = "Storage" > function< / span > () { };
< 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 > ) {
2009-12-21 11:41:45 -05: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-05-15 01:18:05 -04:00
< span class = "FunctionName" > Snake< / span > = < span class = "Storage" > function< / span > (< span class = "FunctionArgument" > name< / span > ) {
2010-02-13 15:25:04 -05:00
< span class = "Variable" > this< / span > .< span class = "LibraryConstant" > name< / span > < span class = "Keyword" > =< / span > name;
< span class = "Keyword" > return< / span > < span class = "Variable" > this< / span > ;
};
2010-02-27 20:12:47 -05:00
__extends(Snake, Animal);
2010-05-15 01:18:05 -04:00
< span class = "LibraryClassType" > Snake< / span > .< span class = "LibraryConstant" > prototype< / span > .< span class = "FunctionName" > move< / span > = < span class = "Storage" > function< / span > () {
2010-02-15 13:55:28 -05:00
< span class = "LibraryFunction" > alert< / span > (< span class = "String" > < span class = "String" > " < / span > Slithering...< span class = "String" > " < / span > < / span > );
< span class = "Keyword" > return< / span > Snake.__superClass__.move.< span class = "LibraryFunction" > call< / span > (< span class = "Variable" > this< / span > , < span class = "Number" > 5< / span > );
};
2010-05-15 01:18:05 -04:00
< span class = "FunctionName" > Horse< / span > = < span class = "Storage" > function< / span > (< span class = "FunctionArgument" > name< / span > ) {
2010-02-13 15:25:04 -05:00
< span class = "Variable" > this< / span > .< span class = "LibraryConstant" > name< / span > < span class = "Keyword" > =< / span > name;
< span class = "Keyword" > return< / span > < span class = "Variable" > this< / span > ;
};
2010-02-27 20:12:47 -05:00
__extends(Horse, Animal);
2010-05-15 01:18:05 -04:00
< span class = "LibraryClassType" > Horse< / span > .< span class = "LibraryConstant" > prototype< / span > .< span class = "FunctionName" > move< / span > = < span class = "Storage" > function< / span > () {
2010-02-15 13:55:28 -05:00
< span class = "LibraryFunction" > alert< / span > (< span class = "String" > < span class = "String" > " < / span > Galloping...< span class = "String" > " < / span > < / span > );
< span class = "Keyword" > return< / span > Horse.__superClass__.move.< span class = "LibraryFunction" > call< / span > (< span class = "Variable" > this< / span > , < span class = "Number" > 45< / span > );
};
2010-05-15 01:18:05 -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 > );
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();
2010-02-27 20:12:47 -05:00
< / pre > < button onclick = 'javascript: var Animal , Horse , Snake , sam , tom ;
var __extends = function(child, parent) {
var ctor = function(){ };
ctor.prototype = parent.prototype;
child.__superClass__ = parent.prototype;
child.prototype = new ctor();
child.prototype.constructor = child;
};
2010-05-15 01:18:05 -04:00
Animal = function() { };
Animal.prototype.move = function(meters) {
2009-12-21 11:41:45 -05:00
return alert(this.name + " moved " + meters + "m.");
};
2010-05-15 01:18:05 -04:00
Snake = function(name) {
2010-02-13 15:25:04 -05:00
this.name = name;
return this;
};
2010-02-27 20:12:47 -05:00
__extends(Snake, Animal);
2010-05-15 01:18:05 -04:00
Snake.prototype.move = function() {
2010-02-15 13:55:28 -05:00
alert("Slithering...");
return Snake.__superClass__.move.call(this, 5);
};
2010-05-15 01:18:05 -04:00
Horse = function(name) {
2010-02-13 15:25:04 -05:00
this.name = name;
return this;
};
2010-02-27 20:12:47 -05:00
__extends(Horse, Animal);
2010-05-15 01:18:05 -04:00
Horse.prototype.move = function() {
2010-02-15 13:55:28 -05:00
alert("Galloping...");
return Horse.__superClass__.move.call(this, 45);
};
2010-05-15 01:18:05 -04:00
2009-12-26 02:17:34 -05:00
sam = new Snake("Sammy the Python");
tom = new Horse("Tommy the Palomino");
2009-12-21 11:41:45 -05:00
sam.move();
tom.move();
;'>run< / button > < 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
2010-03-08 06:44:47 -05:00
helps with proper prototype setup, < tt > ::< / tt > gives you
2010-02-27 20:12:47 -05:00
quick access to an object's prototype, and < tt > super()< / tt >
is converted into a call against the immediate ancestor's method of the same name.
2010-02-26 20:09:49 -05:00
< / p >
2010-02-27 20:12:47 -05:00
< div class = 'code' > < pre class = "idle" > < span class = "FunctionName" > String::dasherize< / span > < span class = "Keyword" > :< / span > < span class = "Storage" > -> < / span >
< span class = "Variable" > this< / span > .replace(< 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 > )
2010-05-15 01:18:05 -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 > );
};
2010-05-15 01:18:05 -04:00
< / pre > < button onclick = 'javascript: String . prototype . dasherize = function() {
2010-02-27 20:12:47 -05:00
return this.replace(/_/g, "-");
};
;alert("one_two".dasherize());'>run: "one_two".dasherize()< / button > < br class = 'clear' / > < / div >
2010-04-03 20:43:50 -04:00
< p >
Finally, you may assign Class-level (static) properties within a class
definition by using< br / > < tt > @property: value< / tt >
< / p >
2009-12-21 11:41:45 -05:00
2010-02-21 14:30:21 -05:00
< p >
< span id = "pattern_matching" class = "bookmark" > < / span >
2010-01-11 22:55:01 -05:00
< b class = "header" > Pattern Matching (Destructuring Assignment)< / b >
2010-01-12 00:09:23 -05:00
To make extracting values from complex arrays and objects more convenient,
CoffeeScript implements ECMAScript Harmony's proposed
2010-01-11 22:55:01 -05:00
< a href = "http://wiki.ecmascript.org/doku.php?id=harmony:destructuring" > destructuring assignment< / a >
syntax. When you assign an array or object literal to a value, CoffeeScript
breaks up and matches both sides against each other, assigning the values
on the right to the variables on the left. In the simplest case, it can be
used for parallel assignment:
< / p >
2010-06-28 00:19:58 -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 >
< span class = "Keyword" > [< / span > theBait, theSwitch< span class = "Keyword" > ]:< / span > [theSwitch, theBait]
< / pre > < pre class = "idle" > < span class = "Storage" > var< / span > _a, theBait, theSwitch;
theBait < span class = "Keyword" > =< / span > < span class = "Number" > 1000< / span > ;
theSwitch < span class = "Keyword" > =< / span > < span class = "Number" > 0< / span > ;
_a < span class = "Keyword" > =< / span > [theSwitch, theBait];
theBait < span class = "Keyword" > =< / span > _a[< span class = "Number" > 0< / span > ];
theSwitch < span class = "Keyword" > =< / span > _a[< span class = "Number" > 1< / span > ];
< / pre > < button onclick = 'javascript: var _a , theBait , theSwitch ;
theBait = 1000;
theSwitch = 0;
_a = [theSwitch, theBait];
theBait = _a[0];
theSwitch = _a[1];
;alert(theBait);'>run: theBait< / button > < 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 >
2010-06-28 00:19:58 -04:00
< div class = 'code' > < pre class = "idle" > < span class = "FunctionName" > weatherReport< / span > < span class = "Keyword" > :< / span > < span class = "FunctionArgument" > (< / span > < span class = "FunctionArgument" > location< / span > < span class = "FunctionArgument" > )< / 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-06-28 00:19:58 -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 >
< / pre > < pre class = "idle" > < span class = "Storage" > var< / span > _a, city, forecast, temp, weatherReport;
< 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 > ];
};
2010-06-28 00:19:58 -04:00
_a < span class = "Keyword" > =< / span > weatherReport(< span class = "String" > < span class = "String" > " < / span > Berkeley, CA< span class = "String" > " < / span > < / span > );
2010-02-21 11:45:03 -05:00
city < span class = "Keyword" > =< / span > _a[< span class = "Number" > 0< / span > ];
temp < span class = "Keyword" > =< / span > _a[< span class = "Number" > 1< / span > ];
forecast < span class = "Keyword" > =< / span > _a[< span class = "Number" > 2< / span > ];
2010-06-28 00:19:58 -04:00
< / pre > < button onclick = 'javascript: var _a , city , forecast , temp , weatherReport ;
weatherReport = function(location) {
2010-01-11 22:55:01 -05:00
return [location, 72, "Mostly Sunny"];
};
2010-06-28 00:19:58 -04:00
_a = weatherReport("Berkeley, CA");
2010-02-21 11:45:03 -05:00
city = _a[0];
temp = _a[1];
forecast = _a[2];
2010-01-11 22:55:01 -05:00
;alert(forecast);'>run: forecast< / button > < br class = 'clear' / > < / div >
< p >
Pattern matching can be used with any depth of array and object nesting,
2010-01-12 00:09:23 -05:00
to help pull out deeply nested properties.
2010-01-11 22:55:01 -05:00
< / p >
2010-03-08 06:34:07 -05:00
< div class = 'code' > < pre class = "idle" > futurists< span class = "Keyword" > :< / span > {
sculptor< span class = "Keyword" > :< / span > < span class = "String" > < span class = "String" > " < / span > Umberto Boccioni< span class = "String" > " < / span > < / span >
painter< span class = "Keyword" > :< / span > < span class = "String" > < span class = "String" > " < / span > Vladimir Burliuk< span class = "String" > " < / span > < / span >
poet< span class = "Keyword" > :< / span > {
name< span class = "Keyword" > :< / span > < span class = "String" > < span class = "String" > " < / span > F.T. Marinetti< span class = "String" > " < / span > < / span >
address< span class = "Keyword" > :< / span > [
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-05-15 01:18:05 -04:00
{poet< span class = "Keyword" > :< / span > {name, address< span class = "Keyword" > :< / span > [street, city]}}< span class = "Keyword" > :< / span > futurists
< / pre > < pre class = "idle" > < span class = "Storage" > var< / span > _a, _b, _c, city, futurists, name, street;
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 > ]
}
};
2010-02-21 11:45:03 -05:00
_a < span class = "Keyword" > =< / span > futurists;
_b < span class = "Keyword" > =< / span > _a.poet;
2010-05-15 01:18:05 -04:00
name < span class = "Keyword" > =< / span > _b.< span class = "LibraryConstant" > name< / span > ;
2010-02-21 11:45:03 -05:00
_c < span class = "Keyword" > =< / span > _b.address;
street < span class = "Keyword" > =< / span > _c[< span class = "Number" > 0< / span > ];
city < span class = "Keyword" > =< / span > _c[< span class = "Number" > 1< / span > ];
2010-05-15 01:18:05 -04:00
< / pre > < button onclick = 'javascript: var _a , _b , _c , city , futurists , name , street ;
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"]
}
};
2010-02-21 11:45:03 -05:00
_a = futurists;
_b = _a.poet;
2010-05-15 01:18:05 -04:00
name = _b.name;
2010-02-21 11:45:03 -05:00
_c = _b.address;
street = _c[0];
city = _c[1];
2010-05-15 01:18:05 -04:00
;alert(name + " — " + street);'>run: name + " — " + street< / button > < br class = 'clear' / > < / div >
2010-03-23 00:18:50 -04:00
< p >
Pattern matching can even be combined with splats.
< / p >
< div class = 'code' > < pre class = "idle" > tag< span class = "Keyword" > :< / span > < span class = "String" > < span class = "String" > " < / span > < impossible> < span class = "String" > " < / span > < / span >
< 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 > )
< / pre > < pre class = "idle" > < span class = "Storage" > var< / span > _a, close, contents, open, tag;
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;
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 > ;
_a < 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 > _a[< span class = "Number" > 0< / span > ];
2010-04-03 20:43:50 -04:00
contents < span class = "Keyword" > =< / span > __slice.< span class = "LibraryFunction" > call< / span > (_a, < span class = "Number" > 1< / span > , _a.< span class = "LibraryConstant" > length< / span > < span class = "Keyword" > -< / span > < span class = "Number" > 1< / span > );
2010-03-23 00:18:50 -04:00
close < span class = "Keyword" > =< / span > _a[_a.< span class = "LibraryConstant" > length< / span > < span class = "Keyword" > -< / span > < span class = "Number" > 1< / span > ];
< / pre > < button onclick = 'javascript: var _a , close , contents , open , tag ;
2010-04-03 20:43:50 -04:00
var __slice = Array.prototype.slice;
2010-03-23 00:18:50 -04:00
tag = "< impossible > ";
_a = tag.split("");
open = _a[0];
2010-04-03 20:43:50 -04:00
contents = __slice.call(_a, 1, _a.length - 1);
2010-03-23 00:18:50 -04:00
close = _a[_a.length - 1];
;alert(contents.join(""));'>run: contents.join("")< / button > < 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
as 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 >
2010-01-26 23:23:59 -05:00
< div class = 'code' > < pre class = "idle" > < span class = "FunctionName" > Account< / span > < span class = "Keyword" > :< / span > < span class = "FunctionArgument" > (< / span > < span class = "FunctionArgument" > customer, cart< / span > < span class = "FunctionArgument" > )< / span > < span class = "Storage" > -> < / span >
2010-02-08 10:58:49 -05: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-01-26 23:23:59 -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" > (< / span > < span class = "FunctionArgument" > event< / span > < span class = "FunctionArgument" > )< / 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;
2010-05-15 01:18:05 -04:00
< span class = "FunctionName" > Account< / span > = < span class = "Storage" > function< / span > (< span class = "FunctionArgument" > customer, cart< / 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;
2010-06-28 00:19:58 -04: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 > (__this) {
< span class = "Storage" > var< / span > < span class = "FunctionName" > __func< / span > = < span class = "Storage" > function< / span > (< span class = "FunctionArgument" > event< / span > ) {
2010-01-17 18:12:59 -05:00
< span class = "Keyword" > return< / span > < span class = "Variable" > this< / span > .customer.purchase(< span class = "Variable" > this< / span > .cart);
2010-06-28 00:19:58 -04:00
};
< span class = "Keyword" > return< / span > (< span class = "Storage" > function< / span > () {
< span class = "Keyword" > return< / span > __func.< span class = "LibraryFunction" > apply< / span > (__this, arguments);
});
})(< span class = "Variable" > this< / span > ));
2010-01-13 23:24:45 -05:00
};
< / pre > < 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-03-08 06:34:07 -05: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;
< span class = "FunctionName" > hi< / span > = < span class = "Storage" > function< / span > () {
2009-12-23 20:24:55 -05: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
};
2009-12-26 02:17:34 -05:00
< / pre > < button onclick = 'javascript: var hi ;
hi = function() {
2009-12-23 20:24:55 -05:00
return [document.title, "Hello JavaScript"].join(": ");
2009-12-21 11:41:45 -05:00
};
2009-12-23 20:24:55 -05:00
;alert(hi());'>run: hi()< / button > < 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 >
2009-12-24 02:01:39 -05:00
< b > Switch< / b > statements in JavaScript are rather broken. You can only
2009-12-24 03:12:07 -05:00
do comparisons based on string equality, and need to remember to < b > break< / b > at the end of
2009-12-24 01:22:41 -05:00
every < b > case< / b > statement to avoid accidentally falling through to
2009-12-24 04:33:59 -05:00
the default case. CoffeeScript compiles < b > switch< / b > statements into JavaScript if-else chains, allowing you to
2009-12-23 19:42:18 -05:00
compare any object (via < b > ===< / b > ), preventing fall-through, and resulting
2009-12-24 04:33:59 -05:00
in a returnable, assignable expression. The format is: < tt > switch< / tt > condition,
< tt > when< / tt > clauses, < tt > else< / tt > the default case.
2009-12-23 19:42:18 -05:00
< / p >
2010-01-13 23:24:45 -05:00
< p >
As in Ruby, < b > switch< / b > statements in CoffeeScript can take multiple
values for each < b > when< / b > clause. If any of the values match, the clause
runs.
< / p >
2009-12-23 19:42:18 -05:00
< div class = 'code' > < pre class = "idle" > < span class = "Keyword" > switch< / span > day
2010-06-28 00:19:58 -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 > goToWork()
< span class = "Keyword" > when< / span > < span class = "String" > < span class = "String" > " < / span > Tue< span class = "String" > " < / span > < / span > < span class = "Keyword" > then< / span > goToThePark()
< span class = "Keyword" > when< / span > < span class = "String" > < span class = "String" > " < / span > Thu< span class = "String" > " < / span > < / span > < span class = "Keyword" > then< / span > goIceFishing()
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
goToBingo()
goDancing()
< span class = "Keyword" > when< / span > < span class = "String" > < span class = "String" > " < / span > Sun< span class = "String" > " < / span > < / span > < span class = "Keyword" > then< / span > goToChurch()
< span class = "Keyword" > else< / span > goToWork()
2010-01-13 23:24:45 -05:00
< / pre > < pre class = "idle" > < span class = "Keyword" > if< / span > (day < span class = "Keyword" > ===< / span > < span class = "String" > < span class = "String" > " < / span > Mon< span class = "String" > " < / span > < / span > ) {
2010-06-28 00:19:58 -04:00
goToWork();
2010-01-13 23:24:45 -05:00
} < span class = "Keyword" > else< / span > < span class = "Keyword" > if< / span > (day < span class = "Keyword" > ===< / span > < span class = "String" > < span class = "String" > " < / span > Tue< span class = "String" > " < / span > < / span > ) {
2010-06-28 00:19:58 -04:00
goToThePark();
2010-01-13 23:24:45 -05:00
} < span class = "Keyword" > else< / span > < span class = "Keyword" > if< / span > (day < span class = "Keyword" > ===< / span > < span class = "String" > < span class = "String" > " < / span > Thu< span class = "String" > " < / span > < / span > ) {
2010-06-28 00:19:58 -04:00
goIceFishing();
2010-01-13 23:24:45 -05:00
} < span class = "Keyword" > else< / span > < span class = "Keyword" > if< / span > (day < span class = "Keyword" > ===< / span > < span class = "String" > < span class = "String" > " < / span > Fri< span class = "String" > " < / span > < / span > < span class = "Keyword" > ||< / span > day < span class = "Keyword" > ===< / 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" > ===< / span > bingoDay) {
goToBingo();
goDancing();
2009-12-23 20:48:55 -05:00
}
2010-01-13 23:24:45 -05:00
} < span class = "Keyword" > else< / span > < span class = "Keyword" > if< / span > (day < span class = "Keyword" > ===< / span > < span class = "String" > < span class = "String" > " < / span > Sun< span class = "String" > " < / span > < / span > ) {
2010-06-28 00:19:58 -04:00
goToChurch();
2009-12-21 11:41:45 -05:00
} < span class = "Keyword" > else< / span > {
2010-06-28 00:19:58 -04:00
goToWork();
2009-12-21 11:41:45 -05:00
}
< / pre > < br class = 'clear' / > < / div >
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()
2009-12-21 11:41:45 -05: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
}
< / pre > < br class = 'clear' / > < / div >
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-03-08 06:34:07 -05: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-03-08 06:34:07 -05: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;
cholesterol < span class = "Keyword" > =< / span > < span class = "Number" > 127< / span > ;
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 > );
< / pre > < button onclick = 'javascript: var cholesterol , healthy ;
cholesterol = 127;
healthy = (200 > cholesterol) & & (cholesterol > 60);
;alert(healthy);'>run: healthy< / button > < br class = 'clear' / > < / div >
2010-03-23 00:18:50 -04:00
< p >
< span id = "interpolation" class = "bookmark" > < / span >
< b class = "header" > String and RegExp Interpolation< / b >
A version of < a href = "http://wiki.ecmascript.org/doku.php?id=strawman:string_interpolation" > ECMAScript Harmony's proposed string interpolation< / a >
is included in CoffeeScript. Simple variables can be included by marking
them with a dollar sign.
< / p >
< div class = 'code' > < pre class = "idle" > author< span class = "Keyword" > :< / span > < span class = "String" > < span class = "String" > " < / span > Wittgenstein< span class = "String" > " < / span > < / span >
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 > < span class = "String" > " < / span > < / span >
< / pre > < pre class = "idle" > < span class = "Storage" > var< / span > author, quote;
author < span class = "Keyword" > =< / span > < span class = "String" > < span class = "String" > " < / span > Wittgenstein< span class = "String" > " < / span > < / span > ;
2010-04-10 13:42:39 -04: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);
2010-03-23 00:18:50 -04:00
< / pre > < button onclick = 'javascript: var author , quote ;
author = "Wittgenstein";
2010-04-10 13:42:39 -04:00
quote = ("A picture is a fact. -- " + author);
2010-03-23 00:18:50 -04:00
;alert(quote);'>run: quote< / button > < br class = 'clear' / > < / div >
< p >
And arbitrary expressions can be interpolated by using brackets < tt > ${ ... }< / tt > < br / >
Interpolation works the same way within regular expressions.
< / p >
< div class = 'code' > < pre class = "idle" > 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 >
sep< span class = "Keyword" > :< / span > < span class = "String" > < span class = "String" > " < / span > [.< span class = "UserDefinedConstant" > \\< / span > /< span class = "UserDefinedConstant" > \\< / span > - ]< span class = "String" > " < / span > < / span >
dates< span class = "Keyword" > :< / span > < span class = "String" > < span class = "String" > /< / span > < span class = "UserDefinedConstant" > \d< / span > +< span class = "String" > < span class = "String" > $< / span > sep< / span > < span class = "UserDefinedConstant" > \d< / span > +< span class = "String" > < span class = "String" > $< / span > sep< / span > < span class = "UserDefinedConstant" > \d< / span > +< span class = "String" > /< / span > g< / span >
< / pre > < pre class = "idle" > < span class = "Storage" > var< / span > dates, sentence, sep;
2010-04-10 13:42:39 -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 > );
2010-03-23 00:18:50 -04:00
sep < span class = "Keyword" > =< / span > < span class = "String" > < span class = "String" > " < / span > [.< span class = "UserDefinedConstant" > \\< / span > /< span class = "UserDefinedConstant" > \\< / span > - ]< span class = "String" > " < / span > < / span > ;
2010-04-10 13:42:39 -04:00
dates < span class = "Keyword" > =< / span > (< span class = "Keyword" > new< / span > < span class = "TypeName" > RegExp< / span > ((< span class = "String" > < span class = "String" > " < / span > < span class = "UserDefinedConstant" > \\< / span > d+< span class = "String" > " < / span > < / span > < span class = "Keyword" > +< / span > sep < span class = "Keyword" > +< / span > < span class = "String" > < span class = "String" > " < / span > < span class = "UserDefinedConstant" > \\< / span > d+< span class = "String" > " < / span > < / span > < span class = "Keyword" > +< / span > sep < span class = "Keyword" > +< / span > < span class = "String" > < span class = "String" > " < / span > < span class = "UserDefinedConstant" > \\< / span > d+< span class = "String" > " < / span > < / span > ), < span class = "String" > < span class = "String" > " < / span > g< span class = "String" > " < / span > < / span > ));
2010-03-23 00:18:50 -04:00
< / pre > < button onclick = 'javascript: var dates , sentence , sep ;
2010-04-10 13:42:39 -04:00
sentence = ("" + (22 / 7) + " is a decent approximation of π");
2010-03-23 00:18:50 -04:00
sep = "[.\\/\\- ]";
2010-04-10 13:42:39 -04:00
dates = (new RegExp(("\\d+" + sep + "\\d+" + sep + "\\d+"), "g"));
2010-03-23 00:18:50 -04:00
;alert(sentence);'>run: sentence< / button > < br class = 'clear' / > < / div >
2010-02-21 14:30:21 -05:00
< p >
< span id = "strings" class = "bookmark" > < / span >
2010-01-12 00:00:38 -05:00
< b class = "header" > Multiline Strings and Heredocs< / b >
2009-12-23 19:42:18 -05:00
Multiline strings are allowed in CoffeeScript.
< / p >
2010-06-28 00:19:58 -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 >
2009-12-21 11:41:45 -05: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;
mobyDick < span class = "Keyword" > =< / span > < span class = "String" > < span class = "String" > " < / span > Call me Ishmael. Some years ago -- \< / span >
2009-12-24 01:22:41 -05: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 >
2009-12-21 11:41:45 -05:00
< span class = "String" > world...< span class = "String" > " < / span > < / span > ;
2010-06-28 00:19:58 -04:00
< / pre > < button onclick = 'javascript: var mobyDick ;
mobyDick = "Call me Ishmael. Some years ago -- \
2009-12-24 01:22:41 -05:00
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...";
2010-06-28 00:19:58 -04:00
;alert(mobyDick);'>run: mobyDick< / button > < 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-03-08 06:34:07 -05:00
< div class = 'code' > < pre class = "idle" > html< span class = "Keyword" > :< / span > < span class = "String" > < span class = "String" > '''< / span > < / span >
2010-01-12 00:00:38 -05:00
< span class = "String" > < strong> < / span >
< span class = "String" > cup of coffeescript< / span >
< span class = "String" > < /strong> < / span >
2010-01-25 00:14:00 -05:00
< span class = "String" > < span class = "String" > '''< / span > < / span >
2010-01-12 00:00:38 -05:00
< / pre > < pre class = "idle" > < span class = "Storage" > var< / span > html;
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 > ;
2010-02-21 18:37:52 -05:00
< / pre > < 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-02-21 18:37:52 -05:00
< h2 >
< span id = "cake" class = "bookmark" > < / span >
Cake, and Cakefiles
< / h2 >
< p >
CoffeeScript includes a simple build system similar to Make and Rake. Naturally,
it's called Cake, and is used for the build and test tasks for the CoffeeScript
language itself. Tasks are defined in a file named < tt > Cakefile< / tt > , and
can be invoked by running < tt > cake taskname< / tt > from within the directory.
To print a list of all the tasks, just run < tt > cake< / tt > .
< / 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
function to invoke when the task is run. Here's a hypothetical task
that uses the Node.js API.
< / p >
2010-03-16 19:13:13 -04:00
< div class = 'code' > < pre class = "idle" > task < span class = "String" > < span class = "String" > '< / span > test< span class = "String" > '< / span > < / span > , < span class = "String" > < span class = "String" > '< / span > run each of the unit tests< span class = "String" > '< / span > < / span > , < span class = "Storage" > -> < / span >
2010-06-28 00:19:58 -04:00
< span class = "Keyword" > for< / span > test < span class = "Keyword" > in< / span > files
2010-02-21 18:37:52 -05:00
fs.readFile test, < span class = "FunctionArgument" > (< / span > < span class = "FunctionArgument" > err, code< / span > < span class = "FunctionArgument" > )< / span > < span class = "Storage" > -> < / span > eval coffee.compile code
2010-03-16 19:13:13 -04:00
< / pre > < pre class = "idle" > task(< span class = "String" > < span class = "String" > '< / span > test< span class = "String" > '< / span > < / span > , < span class = "String" > < span class = "String" > '< / span > run each of the unit tests< span class = "String" > '< / span > < / span > , < span class = "Storage" > function< / span > () {
2010-06-28 00:19:58 -04:00
< span class = "Storage" > var< / span > _a, _b, _c, _d;
_a < span class = "Keyword" > =< / span > []; _c < span class = "Keyword" > =< / span > files;
2010-04-03 20:43:50 -04:00
< span class = "Keyword" > for< / span > (_b < span class = "Keyword" > =< / span > < span class = "Number" > 0< / span > , _d < span class = "Keyword" > =< / span > _c.< span class = "LibraryConstant" > length< / span > ; _b < span class = "Keyword" > < < / span > _d; _b< span class = "Keyword" > ++< / span > ) {
2010-06-28 00:19:58 -04:00
(< span class = "Storage" > function< / span > () {
< span class = "Storage" > var< / span > test < span class = "Keyword" > =< / span > _c[_b];
< span class = "Keyword" > return< / span > _a.< span class = "LibraryFunction" > push< / span > (fs.readFile(test, < span class = "Storage" > function< / span > (err, code) {
< span class = "Keyword" > return< / span > < span class = "LibraryFunction" > eval< / span > (coffee.< span class = "LibraryFunction" > compile< / span > (code));
}));
})();
2010-02-21 18:37:52 -05:00
}
< span class = "Keyword" > return< / span > _a;
});
2010-01-12 00:00:38 -05:00
< / pre > < br class = 'clear' / > < / div >
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
(< a href = "extras/coffee-script.js" > Download current version here, 43k 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,
2010-02-25 18:20:15 -05:00
as well as 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-02-21 14:30:21 -05:00
< h2 >
< span id = "resources" class = "bookmark" > < / span >
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 / >
Bugs reports, feature requests, and general discussion all belong here.
2009-12-31 18:22:51 -05:00
< / li >
2010-02-15 18:58:27 -05:00
< li >
2010-02-25 17:18:28 -05:00
If you'd like to chat, stop by < tt > #coffeescript< / tt > on Freenode in the
2010-02-25 18:20:15 -05:00
IRC client of your choice, or on
2010-02-25 17:18:28 -05:00
< a href = "http://webchat.freenode.net/" > webchat.freenode.net< / a > .
2010-02-15 18:58:27 -05:00
< / li >
2010-05-15 01:18:05 -04:00
< li >
< b > yeungda< / b > 's < a href = "http://github.com/yeungda/jcoffeescript" > JCoffeeScript< / a >
— A Java Library that uses Rhino to compile CoffeeScript, allowing
compilation within Java projects or on systems that Node.js doesn't support.
< / li >
2010-03-07 17:52:14 -05:00
< li >
< b > defunkt< / b > 's < a href = "http://github.com/defunkt/coffee-mode" > CoffeeScript Major Mode< / a >
— a Emacs major mode that provides syntax highlighting, indentation
2010-04-03 20:43:50 -04:00
support, and some bonus commands.
< / li >
< li >
< b > jashkenas< / b > 's < a href = "http://github.com/jashkenas/coffee-script-tmbundle" > CoffeeScript TextMate Bundle< / a >
— which provides syntax highlighting, snippet expansion, and the
ability to run bits of CoffeeScript from within TextMate itself.
< / li >
< li >
< b > kchmck< / b > 's < a href = "http://github.com/kchmck/vim-coffee-script" > Vim CoffeeScript< / a >
— which adds Vim syntax highlighting and indentation support.
2010-03-07 17:52:14 -05:00
< / li >
2010-04-13 23:41:18 -04:00
< li >
< b > yeungda< / b > 's < a href = "http://yeungda.github.com/coffeescript-idea/" > coffeescript-idea< / a >
— a plugin for IntelliJ IDEA and RubyMine providing syntax highlighting.
< / li >
2010-03-07 17:52:14 -05:00
< li >
< b > mattly< / b > 's < a href = "http://github.com/mattly/rack-coffee" > rack-coffee< / a >
2010-03-08 06:34:07 -05:00
— a small Rack middleware for serving CoffeeScript files as
2010-03-07 17:52:14 -05:00
compiled JavaScript on the fly.
< / li >
2010-04-03 20:43:50 -04:00
< li >
< b > jnicklas< / b > 's < a href = "http://github.com/jnicklas/bistro_car" > BistroCar< / a >
— a plugin that serves and bundles CoffeeScript from within your
Rails application.
< / li >
2010-05-15 01:18:05 -04:00
< li >
< b > sutto< / b > 's < a href = "http://github.com/Sutto/barista" > Barista< / a >
— a BistroCar alternative that integrates well with
< a href = "http://documentcloud.github.com/jammit" > Jammit< / a > and Rails 3.
< / li >
2010-04-11 17:57:29 -04:00
< li >
< b > inem< / b > and < b > gerad< / b > 's < a href = "http://github.com/gerad/coffee-haml-filter" > coffee-haml-filter< / a >
— a custom filter for rendering CoffeeScript inline within
< a href = "http://haml-lang.com/" > HAML< / a > templates.
< / li >
2010-06-28 01:00:53 -04:00
< li >
< b > jashkenas< / b > 's < a href = "http://jashkenas.github.com/docco/" > Docco< / a >
— A quick-and-dirty literate-programming-style documentation generator
for CoffeeScript. Used to produce the annotated source.
< / 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 >
2010-06-28 01:00:53 -04:00
< p >
< b class = "header" style = "margin-top: 20px;" > 0.7.0< / b >
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 >
if you'd like to iterate downward. Reporting of syntax errors is greatly
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 >
< b class = "header" style = "margin-top: 20px;" > 0.6.2< / b >
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 >
< b class = "header" style = "margin-top: 20px;" > 0.6.1< / b >
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 >
< b class = "header" style = "margin-top: 20px;" > 0.6.0< / 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 >
< b class = "header" style = "margin-top: 20px;" > 0.5.6< / b >
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 >
< b class = "header" style = "margin-top: 20px;" > 0.5.5< / b >
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 >
< b class = "header" style = "margin-top: 20px;" > 0.5.4< / b >
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 >
< b class = "header" style = "margin-top: 20px;" > 0.5.3< / 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 >
< b class = "header" style = "margin-top: 20px;" > 0.5.2< / b >
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 >
< b class = "header" style = "margin-top: 20px;" > 0.5.1< / b >
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 >
< b class = "header" style = "margin-top: 20px;" > 0.5.0< / 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 >
< b class = "header" style = "margin-top: 20px;" > 0.3.2< / b >
< tt > @property< / tt > is now a shorthand for < tt > this.property< / tt > .< br / >
Switched the default JavaScript engine from Narwhal to Node.js. Pass
the < tt > --narwhal< / tt > flag if you'd like to continue using it.
< / p >
2009-12-25 03:16:56 -05:00
2009-12-24 12:54:12 -05:00
< p >
2010-01-26 23:23:59 -05:00
< b class = "header" style = "margin-top: 20px;" > 0.3.0< / b >
CoffeeScript 0.3 includes major syntax changes:
< br / >
The function symbol was changed to
< tt > ->< / tt > , and the bound function symbol is now < tt > =>< / tt > .
< br / >
Parameter lists in function definitions must now be wrapped in parentheses.
< br / >
Added property soaking, with the < tt > ?.< / tt > operator.
< br / >
Made parentheses optional, when invoking functions with arguments.
< br / >
Removed the obsolete block literal syntax.
2009-12-24 12:54:12 -05:00
< / p >
2009-12-25 03:16:56 -05:00
2010-01-17 18:12:59 -05:00
< p >
< b class = "header" style = "margin-top: 20px;" > 0.2.6< / b >
Added Python-style chained comparisons, the conditional existence
operator < tt > ?=< / tt > , and some examples from < i > Beautiful Code< / i > .
Bugfixes relating to statement-to-expression conversion, arguments-to-array
conversion, and the TextMate syntax highlighter.
< / p >
2010-01-13 23:24:45 -05:00
< p >
< b class = "header" style = "margin-top: 20px;" > 0.2.5< / b >
The conditions in switch statements can now take multiple values at once —
If any of them are true, the case will run. Added the long arrow < tt > ==>< / tt > ,
which defines and immediately binds a function to < tt > this< / tt > . While loops can
now be used as expressions, in the same way that comprehensions can. Splats
can be used within pattern matches to soak up the rest of an array.
< / p >
2010-01-12 00:09:23 -05:00
< p >
< b class = "header" style = "margin-top: 20px;" > 0.2.4< / b >
Added ECMAScript Harmony style destructuring assignment, for dealing with
extracting values from nested arrays and objects. Added indentation-sensitive
heredocs for nicely formatted strings or chunks of code.
< / p >
2010-01-11 22:55:01 -05:00
2010-01-11 00:01:16 -05:00
< p >
< b class = "header" style = "margin-top: 20px;" > 0.2.3< / b >
Axed the unsatisfactory < tt > ino< / tt > keyword, replacing it with < tt > of< / tt > for
2010-01-11 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 >
< b class = "header" style = "margin-top: 20px;" > 0.2.2< / b >
2010-01-10 16:16:59 -05:00
When performing a comprehension over an object, use < tt > ino< / tt > , instead
of < tt > in< / tt > , which helps us generate smaller, more efficient code at
compile time.
< br / >
Added < tt > ::< / tt > as a shorthand for saying < tt > .prototype.< / tt >
< br / >
2010-01-10 14:45:44 -05:00
The "splat" symbol has been changed from a prefix asterisk < tt > *< / tt > , to
2010-01-10 16:16:59 -05:00
a postfix ellipsis < tt > ...< / tt >
< br / >
Added JavaScript's < tt > in< / tt > operator,
2010-01-10 14:45:44 -05:00
empty < tt > return< / tt > statements, and empty < tt > while< / tt > loops.
2010-01-10 16:16:59 -05:00
< br / >
2010-01-10 14:45:44 -05:00
Constructor functions that start with capital letters now include a
safety check to make sure that the new instance of the object is returned.
2010-01-10 16:16:59 -05:00
< br / >
2010-01-10 14:45:44 -05:00
The < tt > extends< / tt > keyword now functions identically to < tt > goog.inherits< / tt >
in Google's Closure Library.
< / p >
2010-01-05 09:29:10 -05:00
< p >
< b class = "header" style = "margin-top: 20px;" > 0.2.1< / b >
Arguments objects are now converted into real arrays when referenced.
< / p >
2010-01-05 00:19:22 -05:00
< p >
< b class = "header" style = "margin-top: 20px;" > 0.2.0< / b >
Major release. Significant whitespace. Better statement-to-expression
conversion. Splats. Splice literals. Object comprehensions. Blocks.
2010-01-17 18:12:59 -05:00
The existential operator. Many thanks to all the folks who posted issues,
2010-01-05 09:29:10 -05:00
with special thanks to
2010-01-05 00:19:22 -05:00
< a href = "http://github.com/kamatsu" > Liam O'Connor-Davis< / a > for whitespace
and expression help.
< / p >
2009-12-27 15:49:11 -05:00
< p >
< b class = "header" style = "margin-top: 20px;" > 0.1.6< / b >
Bugfix for running < tt > coffee --interactive< / tt > and < tt > --run< / tt >
2009-12-31 13:01:10 -05:00
from outside of the CoffeeScript directory. Bugfix for nested
function/if-statements.
2009-12-27 15:49:11 -05:00
< / p >
2009-12-31 13:01:10 -05:00
2009-12-27 00:25:37 -05:00
< p >
< b class = "header" style = "margin-top: 20px;" > 0.1.5< / b >
Array slice literals and array comprehensions can now both take Ruby-style
2009-12-31 13:01:10 -05:00
ranges to specify the start and end. JavaScript variable declaration is
now pushed up to the top of the scope, making all assignment statements into
2009-12-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 >
< b class = "header" style = "margin-top: 20px;" > 0.1.4< / b >
The official CoffeeScript extension is now < tt > .coffee< / tt > instead of
2009-12-25 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 >
< b class = "header" style = "margin-top: 20px;" > 0.1.3< / 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 >
2009-12-24 20:05:55 -05:00
< b class = "header" style = "margin-top: 20px;" > 0.1.2< / 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 >
< b class = "header" style = "margin-top: 20px;" > 0.1.1< / 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 >
2009-12-24 20:05:55 -05:00
< b class = "header" style = "margin-top: 20px;" > 0.1.0< / 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" >
2010-02-25 20:39:34 -05:00
# Set up the compilation function, to run when you stop typing.
compile_source: ->
2010-02-24 20:41:56 -05:00
source: $('#repl_source').val()
window.compiled_js: ''
try
2010-06-28 00:19:58 -04:00
window.compiled_js: CoffeeScript.compile source, {noWrap: true}
2010-02-25 20:39:34 -05:00
$('#repl_results').text window.compiled_js
$('#error').hide();
catch error
$('#error').text(error.message).show();
# Listen for keypresses and recompile.
$('#repl_source').keyup -> compile_source()
2010-02-24 20:41:56 -05:00
2010-02-25 20:39:34 -05:00
# Eval the compiled js.
$('button.run').click ->
2010-02-24 20:41:56 -05:00
try
eval window.compiled_js
catch error then alert error
current_nav: null
2010-02-25 20:39:34 -05:00
# Helper to hide the menus.
2010-02-24 20:41:56 -05:00
close_menus: ->
2010-02-27 02:21:26 -05:00
if current_nav
current_nav.removeClass 'active'
document.body.className: 'minimized'
2010-02-24 20:41:56 -05:00
current_nav: null
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'
if this isnt (current_nav and current_nav[0])
close_menus();
current_nav: $(this)
current_nav.addClass 'active'
false
$(document.body).click -> close_menus()
2010-02-25 00:26:59 -05:00
2010-02-27 02:21:26 -05:00
$('.navigation .full_screen').click ->
document.body.className: 'full_screen'
$('.navigation .minimize').click ->
document.body.className: 'minimized'
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 > ')
2010-02-25 20:39:34 -05:00
compile_source()
2010-02-24 20:41:56 -05:00
< / script >
2010-02-21 23:29:31 -05:00
2010-02-24 23:57:39 -05:00
< script src = "http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" > < / script >
< 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 >