haml-more 0.4.0.a
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +79 -0
- data/lib/haml/more/coffee_script.rb +137 -0
- data/lib/haml/more/content_for.rb +25 -0
- data/lib/haml/more.rb +45 -0
- data/lib/haml-more.rb +1 -0
- data/lib/sass/more.rb +16 -0
- data/lib/sass-more.rb +1 -0
- data/spec/sass/more_spec.rb +21 -0
- data/vendor/coffee-script/Cakefile +57 -0
- data/vendor/coffee-script/LICENSE +22 -0
- data/vendor/coffee-script/README +41 -0
- data/vendor/coffee-script/Rakefile +20 -0
- data/vendor/coffee-script/bin/cake +7 -0
- data/vendor/coffee-script/bin/coffee +7 -0
- data/vendor/coffee-script/documentation/coffee/aliases.coffee +9 -0
- data/vendor/coffee-script/documentation/coffee/arguments.coffee +4 -0
- data/vendor/coffee-script/documentation/coffee/array_comprehensions.coffee +7 -0
- data/vendor/coffee-script/documentation/coffee/assignment.coffee +2 -0
- data/vendor/coffee-script/documentation/coffee/cake_tasks.coffee +5 -0
- data/vendor/coffee-script/documentation/coffee/comparisons.coffee +5 -0
- data/vendor/coffee-script/documentation/coffee/conditionals.coffee +9 -0
- data/vendor/coffee-script/documentation/coffee/embedded.coffee +5 -0
- data/vendor/coffee-script/documentation/coffee/existence.coffee +8 -0
- data/vendor/coffee-script/documentation/coffee/expressions.coffee +9 -0
- data/vendor/coffee-script/documentation/coffee/expressions_assignment.coffee +1 -0
- data/vendor/coffee-script/documentation/coffee/expressions_comprehension.coffee +3 -0
- data/vendor/coffee-script/documentation/coffee/expressions_try.coffee +6 -0
- data/vendor/coffee-script/documentation/coffee/fat_arrow.coffee +6 -0
- data/vendor/coffee-script/documentation/coffee/functions.coffee +2 -0
- data/vendor/coffee-script/documentation/coffee/heredocs.coffee +5 -0
- data/vendor/coffee-script/documentation/coffee/multiple_return_values.coffee +5 -0
- data/vendor/coffee-script/documentation/coffee/object_comprehensions.coffee +4 -0
- data/vendor/coffee-script/documentation/coffee/object_extraction.coffee +13 -0
- data/vendor/coffee-script/documentation/coffee/objects_and_arrays.coffee +13 -0
- data/vendor/coffee-script/documentation/coffee/overview.coffee +29 -0
- data/vendor/coffee-script/documentation/coffee/parallel_assignment.coffee +4 -0
- data/vendor/coffee-script/documentation/coffee/range_comprehensions.coffee +6 -0
- data/vendor/coffee-script/documentation/coffee/scope.coffee +5 -0
- data/vendor/coffee-script/documentation/coffee/slices.coffee +6 -0
- data/vendor/coffee-script/documentation/coffee/soaks.coffee +1 -0
- data/vendor/coffee-script/documentation/coffee/splats.coffee +25 -0
- data/vendor/coffee-script/documentation/coffee/splices.coffee +5 -0
- data/vendor/coffee-script/documentation/coffee/strings.coffee +8 -0
- data/vendor/coffee-script/documentation/coffee/super.coffee +34 -0
- data/vendor/coffee-script/documentation/coffee/switch.coffee +10 -0
- data/vendor/coffee-script/documentation/coffee/try.coffee +7 -0
- data/vendor/coffee-script/documentation/coffee/while.coffee +10 -0
- data/vendor/coffee-script/documentation/css/docs.css +213 -0
- data/vendor/coffee-script/documentation/css/idle.css +63 -0
- data/vendor/coffee-script/documentation/css/logo.png +0 -0
- data/vendor/coffee-script/documentation/index.html.erb +967 -0
- data/vendor/coffee-script/documentation/js/aliases.js +14 -0
- data/vendor/coffee-script/documentation/js/arguments.js +8 -0
- data/vendor/coffee-script/documentation/js/array_comprehensions.js +26 -0
- data/vendor/coffee-script/documentation/js/assignment.js +5 -0
- data/vendor/coffee-script/documentation/js/cake_tasks.js +14 -0
- data/vendor/coffee-script/documentation/js/comparisons.js +5 -0
- data/vendor/coffee-script/documentation/js/conditionals.js +12 -0
- data/vendor/coffee-script/documentation/js/embedded.js +6 -0
- data/vendor/coffee-script/documentation/js/existence.js +7 -0
- data/vendor/coffee-script/documentation/js/expressions.js +13 -0
- data/vendor/coffee-script/documentation/js/expressions_assignment.js +4 -0
- data/vendor/coffee-script/documentation/js/expressions_comprehension.js +12 -0
- data/vendor/coffee-script/documentation/js/expressions_try.js +9 -0
- data/vendor/coffee-script/documentation/js/fat_arrow.js +15 -0
- data/vendor/coffee-script/documentation/js/functions.js +9 -0
- data/vendor/coffee-script/documentation/js/heredocs.js +4 -0
- data/vendor/coffee-script/documentation/js/intro.js +7 -0
- data/vendor/coffee-script/documentation/js/multiple_return_values.js +11 -0
- data/vendor/coffee-script/documentation/js/object_comprehensions.js +17 -0
- data/vendor/coffee-script/documentation/js/object_extraction.js +17 -0
- data/vendor/coffee-script/documentation/js/objects_and_arrays.js +10 -0
- data/vendor/coffee-script/documentation/js/overview.js +43 -0
- data/vendor/coffee-script/documentation/js/parallel_assignment.js +8 -0
- data/vendor/coffee-script/documentation/js/punctuation.js +8 -0
- data/vendor/coffee-script/documentation/js/range_comprehensions.js +21 -0
- data/vendor/coffee-script/documentation/js/scope.js +10 -0
- data/vendor/coffee-script/documentation/js/slices.js +6 -0
- data/vendor/coffee-script/documentation/js/soaks.js +4 -0
- data/vendor/coffee-script/documentation/js/splats.js +16 -0
- data/vendor/coffee-script/documentation/js/splices.js +5 -0
- data/vendor/coffee-script/documentation/js/strings.js +9 -0
- data/vendor/coffee-script/documentation/js/super.js +37 -0
- data/vendor/coffee-script/documentation/js/switch.js +18 -0
- data/vendor/coffee-script/documentation/js/try.js +10 -0
- data/vendor/coffee-script/documentation/js/while.js +22 -0
- data/vendor/coffee-script/documentation/underscore.html +627 -0
- data/vendor/coffee-script/examples/beautiful_code/binary_search.coffee +16 -0
- data/vendor/coffee-script/examples/beautiful_code/quicksort_runtime.coffee +13 -0
- data/vendor/coffee-script/examples/beautiful_code/regular_expression_matcher.coffee +34 -0
- data/vendor/coffee-script/examples/blocks.coffee +57 -0
- data/vendor/coffee-script/examples/code.coffee +173 -0
- data/vendor/coffee-script/examples/computer_science/README +4 -0
- data/vendor/coffee-script/examples/computer_science/binary_search.coffee +25 -0
- data/vendor/coffee-script/examples/computer_science/bubble_sort.coffee +11 -0
- data/vendor/coffee-script/examples/computer_science/linked_list.coffee +106 -0
- data/vendor/coffee-script/examples/computer_science/luhn_algorithm.coffee +36 -0
- data/vendor/coffee-script/examples/computer_science/merge_sort.coffee +19 -0
- data/vendor/coffee-script/examples/computer_science/selection_sort.coffee +23 -0
- data/vendor/coffee-script/examples/poignant.coffee +186 -0
- data/vendor/coffee-script/examples/potion.coffee +205 -0
- data/vendor/coffee-script/examples/underscore.coffee +603 -0
- data/vendor/coffee-script/examples/web_server.coffee +12 -0
- data/vendor/coffee-script/extras/CoffeeScript.tmbundle/Preferences/CoffeeScript.tmPreferences +24 -0
- data/vendor/coffee-script/extras/CoffeeScript.tmbundle/Syntaxes/CoffeeScript.tmLanguage +361 -0
- data/vendor/coffee-script/extras/CoffeeScript.tmbundle/info.plist +10 -0
- data/vendor/coffee-script/extras/EXTRAS +20 -0
- data/vendor/coffee-script/extras/coffee.vim +117 -0
- data/vendor/coffee-script/index.html +1847 -0
- data/vendor/coffee-script/lib/bin/cake +7 -0
- data/vendor/coffee-script/lib/bin/coffee +7 -0
- data/vendor/coffee-script/lib/cake.js +80 -0
- data/vendor/coffee-script/lib/coffee-script.js +61 -0
- data/vendor/coffee-script/lib/command_line.js +201 -0
- data/vendor/coffee-script/lib/grammar.js +564 -0
- data/vendor/coffee-script/lib/lexer.js +405 -0
- data/vendor/coffee-script/lib/narwhal.js +44 -0
- data/vendor/coffee-script/lib/nodes.js +1328 -0
- data/vendor/coffee-script/lib/optparse.js +117 -0
- data/vendor/coffee-script/lib/parser.js +536 -0
- data/vendor/coffee-script/lib/repl.js +32 -0
- data/vendor/coffee-script/lib/rewriter.js +383 -0
- data/vendor/coffee-script/lib/scope.js +114 -0
- data/vendor/coffee-script/package.json +7 -0
- data/vendor/coffee-script/src/cake.coffee +45 -0
- data/vendor/coffee-script/src/coffee-script.coffee +45 -0
- data/vendor/coffee-script/src/command_line.coffee +130 -0
- data/vendor/coffee-script/src/grammar.coffee +456 -0
- data/vendor/coffee-script/src/lexer.coffee +327 -0
- data/vendor/coffee-script/src/narwhal.coffee +42 -0
- data/vendor/coffee-script/src/nodes.coffee +1045 -0
- data/vendor/coffee-script/src/optparse.coffee +79 -0
- data/vendor/coffee-script/src/repl.coffee +23 -0
- data/vendor/coffee-script/src/rewriter.coffee +253 -0
- data/vendor/coffee-script/src/scope.coffee +75 -0
- data/vendor/coffee-script/test/test_arguments.coffee +34 -0
- data/vendor/coffee-script/test/test_array_comprehension.coffee +42 -0
- data/vendor/coffee-script/test/test_assignment.coffee +26 -0
- data/vendor/coffee-script/test/test_blocks.coffee +4 -0
- data/vendor/coffee-script/test/test_calling_super.coffee +42 -0
- data/vendor/coffee-script/test/test_chained_calls.coffee +25 -0
- data/vendor/coffee-script/test/test_destructuring_assignment.coffee +62 -0
- data/vendor/coffee-script/test/test_everything.coffee +29 -0
- data/vendor/coffee-script/test/test_exceptions.coffee +2 -0
- data/vendor/coffee-script/test/test_existence.coffee +81 -0
- data/vendor/coffee-script/test/test_expressions.coffee +30 -0
- data/vendor/coffee-script/test/test_fancy_if_statement.coffee +26 -0
- data/vendor/coffee-script/test/test_functions.coffee +80 -0
- data/vendor/coffee-script/test/test_funky_comments.coffee +25 -0
- data/vendor/coffee-script/test/test_heredocs.coffee +46 -0
- data/vendor/coffee-script/test/test_lexical_scope.coffee +10 -0
- data/vendor/coffee-script/test/test_literals.coffee +56 -0
- data/vendor/coffee-script/test/test_nested_comprehensions.coffee +11 -0
- data/vendor/coffee-script/test/test_newline_escaping.coffee +6 -0
- data/vendor/coffee-script/test/test_operations.coffee +18 -0
- data/vendor/coffee-script/test/test_range_comprehension.coffee +20 -0
- data/vendor/coffee-script/test/test_ranges_and_slices.coffee +16 -0
- data/vendor/coffee-script/test/test_splats.coffee +47 -0
- data/vendor/coffee-script/test/test_splices.coffee +5 -0
- data/vendor/coffee-script/test/test_switch.coffee +64 -0
- data/vendor/coffee-script/test/test_while.coffee +30 -0
- data/vendor/coffee-script/vendor/jison/Jakefile +31 -0
- data/vendor/coffee-script/vendor/jison/README.md +347 -0
- data/vendor/coffee-script/vendor/jison/bin/jison +3 -0
- data/vendor/coffee-script/vendor/jison/bin/json2jison +3 -0
- data/vendor/coffee-script/vendor/jison/examples/ansic.jison +415 -0
- data/vendor/coffee-script/vendor/jison/examples/basic.json +8 -0
- data/vendor/coffee-script/vendor/jison/examples/basic2.json +9 -0
- data/vendor/coffee-script/vendor/jison/examples/basic2_lex.json +16 -0
- data/vendor/coffee-script/vendor/jison/examples/basic_lex.json +15 -0
- data/vendor/coffee-script/vendor/jison/examples/calculator.jison +38 -0
- data/vendor/coffee-script/vendor/jison/examples/calculator.jisonlex +14 -0
- data/vendor/coffee-script/vendor/jison/examples/calculator.json +42 -0
- data/vendor/coffee-script/vendor/jison/examples/classy.json +105 -0
- data/vendor/coffee-script/vendor/jison/examples/classy_ast.json +126 -0
- data/vendor/coffee-script/vendor/jison/examples/dism.json +25 -0
- data/vendor/coffee-script/vendor/jison/examples/dism_lr0.json +26 -0
- data/vendor/coffee-script/vendor/jison/examples/json.js +80 -0
- data/vendor/coffee-script/vendor/jison/examples/json_ast.js +83 -0
- data/vendor/coffee-script/vendor/jison/examples/precedence.json +26 -0
- data/vendor/coffee-script/vendor/jison/examples/reduce_conflict.json +13 -0
- data/vendor/coffee-script/vendor/jison/lib/jison/bnf.js +43 -0
- data/vendor/coffee-script/vendor/jison/lib/jison/jisonlex.js +18 -0
- data/vendor/coffee-script/vendor/jison/lib/jison/json2jison.js +146 -0
- data/vendor/coffee-script/vendor/jison/lib/jison/lexer.js +224 -0
- data/vendor/coffee-script/vendor/jison/lib/jison/util/bnf-parser.js +383 -0
- data/vendor/coffee-script/vendor/jison/lib/jison/util/lex-parser.js +407 -0
- data/vendor/coffee-script/vendor/jison/lib/jison/util/set.js +94 -0
- data/vendor/coffee-script/vendor/jison/lib/jison/util/typal.js +90 -0
- data/vendor/coffee-script/vendor/jison/lib/jison.js +1414 -0
- data/vendor/coffee-script/vendor/jison/package.json +14 -0
- data/vendor/coffee-script/vendor/jison/src/bnf.jison +110 -0
- data/vendor/coffee-script/vendor/jison/src/bnf.jisonlex +25 -0
- data/vendor/coffee-script/vendor/jison/src/bnf.lex.json +24 -0
- data/vendor/coffee-script/vendor/jison/src/jisonlex.jison +129 -0
- data/vendor/coffee-script/vendor/jison/src/jisonlex.jisonlex +31 -0
- data/vendor/coffee-script/vendor/jison/src/jisonlex.lex.json +30 -0
- data/vendor/coffee-script/vendor/jison/tests/all-tests.js +8 -0
- data/vendor/coffee-script/vendor/jison/tests/grammar/bnf.js +91 -0
- data/vendor/coffee-script/vendor/jison/tests/grammar/bnf_parse.js +65 -0
- data/vendor/coffee-script/vendor/jison/tests/grammar/grammar-tests.js +10 -0
- data/vendor/coffee-script/vendor/jison/tests/grammar/json2jison.js +24 -0
- data/vendor/coffee-script/vendor/jison/tests/grammar/lex/ansic.jisonlex +115 -0
- data/vendor/coffee-script/vendor/jison/tests/grammar/lex/bnf.jisonlex +25 -0
- data/vendor/coffee-script/vendor/jison/tests/grammar/lex/bnf.lex.json +24 -0
- data/vendor/coffee-script/vendor/jison/tests/grammar/lex/lex_grammar.jisonlex +31 -0
- data/vendor/coffee-script/vendor/jison/tests/grammar/lex/lex_grammar.lex.json +30 -0
- data/vendor/coffee-script/vendor/jison/tests/grammar/lex.jison +119 -0
- data/vendor/coffee-script/vendor/jison/tests/grammar/lex.js +58 -0
- data/vendor/coffee-script/vendor/jison/tests/grammar/lex_parse.js +117 -0
- data/vendor/coffee-script/vendor/jison/tests/lexer/lexer-tests.js +6 -0
- data/vendor/coffee-script/vendor/jison/tests/lexer/regexplexer.js +417 -0
- data/vendor/coffee-script/vendor/jison/tests/parser/actions.js +311 -0
- data/vendor/coffee-script/vendor/jison/tests/parser/api.js +236 -0
- data/vendor/coffee-script/vendor/jison/tests/parser/generator.js +196 -0
- data/vendor/coffee-script/vendor/jison/tests/parser/lalr.js +183 -0
- data/vendor/coffee-script/vendor/jison/tests/parser/lr0.js +72 -0
- data/vendor/coffee-script/vendor/jison/tests/parser/lr1.js +119 -0
- data/vendor/coffee-script/vendor/jison/tests/parser/parser-tests.js +14 -0
- data/vendor/coffee-script/vendor/jison/tests/parser/precedence.js +237 -0
- data/vendor/coffee-script/vendor/jison/tests/parser/slr.js +52 -0
- data/vendor/coffee-script/vendor/jison/tests/parser/tables.js +126 -0
- data/vendor/coffee-script/vendor/jison/tests/performance.js +110 -0
- data/vendor/coffee-script/vendor/jison/tests/setup.js +3 -0
- metadata +324 -0
@@ -0,0 +1,1847 @@
|
|
1
|
+
|
2
|
+
|
3
|
+
|
4
|
+
<!DOCTYPE html>
|
5
|
+
<html>
|
6
|
+
<head>
|
7
|
+
<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
|
8
|
+
<title>CoffeeScript</title>
|
9
|
+
<link rel="stylesheet" type="text/css" href="documentation/css/docs.css" />
|
10
|
+
<link rel="stylesheet" type="text/css" href="documentation/css/idle.css" />
|
11
|
+
</head>
|
12
|
+
<body>
|
13
|
+
|
14
|
+
<div id="fadeout"></div>
|
15
|
+
|
16
|
+
<div id="flybar">
|
17
|
+
<a id="logo" href="#top"> </a>
|
18
|
+
<div class="navigation">
|
19
|
+
<div class="button">
|
20
|
+
Table of Contents
|
21
|
+
</div>
|
22
|
+
<div class="contents">
|
23
|
+
<a href="#overview">Mini Overview</a>
|
24
|
+
<a href="#installation">Installation and Usage</a>
|
25
|
+
<a href="#language">Language Reference</a>
|
26
|
+
<a href="#whitespace">Significant Whitespace</a>
|
27
|
+
<a href="#functions">Functions and Invocation</a>
|
28
|
+
<a href="#assignment">Assignment</a>
|
29
|
+
<a href="#objects_and_arrays">Objects and Arrays</a>
|
30
|
+
<a href="#lexical_scope">Lexical Scoping and Variable Safety</a>
|
31
|
+
<a href="#conditionals">Conditionals, Ternaries, and Conditional Assignment</a>
|
32
|
+
<a href="#aliases">Aliases</a>
|
33
|
+
<a href="#splats">Splats...</a>
|
34
|
+
<a href="#arguments">Arguments are Arrays</a>
|
35
|
+
<a href="#while">While Loops</a>
|
36
|
+
<a href="#comprehensions">Comprehensions (Arrays, Objects, and Ranges)</a>
|
37
|
+
<a href="#slice_splice">Array Slicing and Splicing with Ranges</a>
|
38
|
+
<a href="#expressions">Everything is an Expression</a>
|
39
|
+
<a href="#existence">The Existential Operator</a>
|
40
|
+
<a href="#inheritance">Inheritance, and Calling Super from a Subclass</a>
|
41
|
+
<a href="#pattern_matching">Pattern Matching</a>
|
42
|
+
<a href="#fat_arrow">Function Binding</a>
|
43
|
+
<a href="#embedded">Embedded JavaScript</a>
|
44
|
+
<a href="#switch">Switch/When/Else</a>
|
45
|
+
<a href="#try">Try/Catch/Finally</a>
|
46
|
+
<a href="#comparisons">Chained Comparisons</a>
|
47
|
+
<a href="#strings">Multiline Strings and Heredocs</a>
|
48
|
+
<a href="#cake">Cake, and Cakefiles</a>
|
49
|
+
<a href="#resources">Resources</a>
|
50
|
+
<a href="#change_log">Change Log</a>
|
51
|
+
</div>
|
52
|
+
</div>
|
53
|
+
<div class="navigation try">
|
54
|
+
<div class="button">
|
55
|
+
Try CoffeeScript
|
56
|
+
</div>
|
57
|
+
<div class="contents repl_wrapper">
|
58
|
+
<div class="code">
|
59
|
+
<textarea id="repl_source">reverse: (string) ->
|
60
|
+
string.split('').reverse().join ''
|
61
|
+
|
62
|
+
alert reverse '!tpircseeffoC'</textarea>
|
63
|
+
<pre id="repl_results"></pre>
|
64
|
+
<button class="compile" onclick="javascript: repl_compile();">compile</button>
|
65
|
+
<button class="run" onclick="javascript: repl_run();">run</button>
|
66
|
+
<br class="clear" />
|
67
|
+
</div>
|
68
|
+
</div>
|
69
|
+
</div>
|
70
|
+
</div>
|
71
|
+
|
72
|
+
<div class="container">
|
73
|
+
<span class="bookmark" id="top"></span>
|
74
|
+
|
75
|
+
<p>
|
76
|
+
CoffeeScript is a little language that compiles into JavaScript. Think
|
77
|
+
of it as JavaScript's less ostentatious kid brother — the same genes,
|
78
|
+
roughly the same height, but a different sense of style. Apart from a handful of
|
79
|
+
bonus goodies, statements in CoffeeScript correspond one-to-one with their
|
80
|
+
equivalent in JavaScript, it's just another way of saying it.
|
81
|
+
</p>
|
82
|
+
|
83
|
+
<p>
|
84
|
+
<b>Disclaimer:</b>
|
85
|
+
CoffeeScript is just for fun. Until it reaches 1.0, <i>there are no guarantees
|
86
|
+
that the syntax won't change between versions.</i> That said,
|
87
|
+
it compiles into clean JavaScript (the good parts) that can use existing
|
88
|
+
JavaScript libraries seamlessly, and passes through
|
89
|
+
<a href="http://www.jslint.com/">JSLint</a> without warnings. The compiled
|
90
|
+
output is quite readable — pretty-printed, with comments
|
91
|
+
preserved intact.
|
92
|
+
</p>
|
93
|
+
|
94
|
+
<p>
|
95
|
+
<b>Latest Version:</b>
|
96
|
+
<a href="http://github.com/jashkenas/coffee-script/tarball/0.5.1">0.5.1</a>
|
97
|
+
</p>
|
98
|
+
|
99
|
+
<h2>
|
100
|
+
<span id="overview" class="bookmark"></span>
|
101
|
+
Mini Overview
|
102
|
+
</h2>
|
103
|
+
|
104
|
+
<p><i>CoffeeScript on the left, compiled JavaScript output on the right.</i></p>
|
105
|
+
|
106
|
+
<div class='code'><pre class="idle"><span class="Comment"><span class="Comment">#</span> Assignment:</span>
|
107
|
+
<span class="FunctionName">number</span><span class="Keyword">:</span> <span class="Number">42</span>
|
108
|
+
<span class="FunctionName">opposite_day</span><span class="Keyword">:</span> <span class="BuiltInConstant">true</span>
|
109
|
+
|
110
|
+
<span class="Comment"><span class="Comment">#</span> Conditions:</span>
|
111
|
+
<span class="FunctionName">number</span><span class="Keyword">:</span> <span class="Keyword">-</span><span class="Number">42</span> <span class="Keyword">if</span> opposite_day
|
112
|
+
|
113
|
+
<span class="Comment"><span class="Comment">#</span> Functions:</span>
|
114
|
+
<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
|
115
|
+
|
116
|
+
<span class="Comment"><span class="Comment">#</span> Arrays:</span>
|
117
|
+
<span class="FunctionName">list</span><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>]
|
118
|
+
|
119
|
+
<span class="Comment"><span class="Comment">#</span> Objects:</span>
|
120
|
+
<span class="FunctionName">math</span><span class="Keyword">:</span> {
|
121
|
+
<span class="FunctionName">root</span><span class="Keyword">:</span> Math.sqrt
|
122
|
+
<span class="FunctionName">square</span><span class="Keyword">:</span> square
|
123
|
+
<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
|
124
|
+
}
|
125
|
+
|
126
|
+
<span class="Comment"><span class="Comment">#</span> Splats:</span>
|
127
|
+
<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>
|
128
|
+
print winner, runners
|
129
|
+
|
130
|
+
<span class="Comment"><span class="Comment">#</span> Existence:</span>
|
131
|
+
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>
|
132
|
+
|
133
|
+
<span class="Comment"><span class="Comment">#</span> Array comprehensions:</span>
|
134
|
+
<span class="FunctionName">cubed_list</span><span class="Keyword">:</span> math.cube num <span class="Keyword">for</span> num <span class="Keyword">in</span> list
|
135
|
+
</pre><pre class="idle"><span class="Storage">var</span> _a, _b, _c, cubed_list, list, math, num, number, opposite_day, race, square;
|
136
|
+
<span class="Comment"><span class="Comment">//</span> Assignment:</span>
|
137
|
+
number <span class="Keyword">=</span> <span class="Number">42</span>;
|
138
|
+
opposite_day <span class="Keyword">=</span> <span class="BuiltInConstant">true</span>;
|
139
|
+
<span class="Comment"><span class="Comment">//</span> Conditions:</span>
|
140
|
+
<span class="Keyword">if</span> (opposite_day) {
|
141
|
+
number <span class="Keyword">=</span> <span class="Keyword">-</span><span class="Number">42</span>;
|
142
|
+
}
|
143
|
+
<span class="Comment"><span class="Comment">//</span> Functions:</span>
|
144
|
+
square <span class="Keyword">=</span> <span class="Storage">function</span> <span class="FunctionName">square</span>(<span class="FunctionArgument">x</span>) {
|
145
|
+
<span class="Keyword">return</span> x <span class="Keyword">*</span> x;
|
146
|
+
};
|
147
|
+
<span class="Comment"><span class="Comment">//</span> Arrays:</span>
|
148
|
+
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>];
|
149
|
+
<span class="Comment"><span class="Comment">//</span> Objects:</span>
|
150
|
+
math <span class="Keyword">=</span> {
|
151
|
+
root: <span class="LibraryClassType">Math</span>.sqrt,
|
152
|
+
square: square,
|
153
|
+
cube: <span class="Storage">function</span> <span class="FunctionName">cube</span>(<span class="FunctionArgument">x</span>) {
|
154
|
+
<span class="Keyword">return</span> x <span class="Keyword">*</span> square(x);
|
155
|
+
}
|
156
|
+
};
|
157
|
+
<span class="Comment"><span class="Comment">//</span> Splats:</span>
|
158
|
+
race <span class="Keyword">=</span> <span class="Storage">function</span> <span class="FunctionName">race</span>(<span class="FunctionArgument">winner</span>) {
|
159
|
+
<span class="Storage">var</span> runners;
|
160
|
+
runners <span class="Keyword">=</span> <span class="LibraryClassType">Array</span>.<span class="LibraryConstant">prototype</span>.slice.<span class="LibraryFunction">call</span>(arguments, <span class="Number">1</span>);
|
161
|
+
<span class="Keyword">return</span> <span class="LibraryFunction">print</span>(winner, runners);
|
162
|
+
};
|
163
|
+
<span class="Comment"><span class="Comment">//</span> Existence:</span>
|
164
|
+
<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>)) {
|
165
|
+
<span class="LibraryFunction">alert</span>(<span class="String"><span class="String">"</span>I knew it!<span class="String">"</span></span>);
|
166
|
+
}
|
167
|
+
<span class="Comment"><span class="Comment">//</span> Array comprehensions:</span>
|
168
|
+
cubed_list <span class="Keyword">=</span> (<span class="Storage">function</span>() {
|
169
|
+
_a <span class="Keyword">=</span> []; _b <span class="Keyword">=</span> list;
|
170
|
+
<span class="Keyword">for</span> (_c <span class="Keyword">=</span> <span class="Number">0</span>; _c <span class="Keyword"><</span> _b.<span class="LibraryConstant">length</span>; _c<span class="Keyword">++</span>) {
|
171
|
+
num <span class="Keyword">=</span> _b[_c];
|
172
|
+
_a.<span class="LibraryFunction">push</span>(math.cube(num));
|
173
|
+
}
|
174
|
+
<span class="Keyword">return</span> _a;
|
175
|
+
}).<span class="LibraryFunction">call</span>(<span class="Variable">this</span>);
|
176
|
+
</pre><button onclick='javascript: var _a, _b, _c, cubed_list, list, math, num, number, opposite_day, race, square;
|
177
|
+
// Assignment:
|
178
|
+
number = 42;
|
179
|
+
opposite_day = true;
|
180
|
+
// Conditions:
|
181
|
+
if (opposite_day) {
|
182
|
+
number = -42;
|
183
|
+
}
|
184
|
+
// Functions:
|
185
|
+
square = function square(x) {
|
186
|
+
return x * x;
|
187
|
+
};
|
188
|
+
// Arrays:
|
189
|
+
list = [1, 2, 3, 4, 5];
|
190
|
+
// Objects:
|
191
|
+
math = {
|
192
|
+
root: Math.sqrt,
|
193
|
+
square: square,
|
194
|
+
cube: function cube(x) {
|
195
|
+
return x * square(x);
|
196
|
+
}
|
197
|
+
};
|
198
|
+
// Splats:
|
199
|
+
race = function race(winner) {
|
200
|
+
var runners;
|
201
|
+
runners = Array.prototype.slice.call(arguments, 1);
|
202
|
+
return print(winner, runners);
|
203
|
+
};
|
204
|
+
// Existence:
|
205
|
+
if ((typeof elvis !== "undefined" && elvis !== null)) {
|
206
|
+
alert("I knew it!");
|
207
|
+
}
|
208
|
+
// Array comprehensions:
|
209
|
+
cubed_list = (function() {
|
210
|
+
_a = []; _b = list;
|
211
|
+
for (_c = 0; _c < _b.length; _c++) {
|
212
|
+
num = _b[_c];
|
213
|
+
_a.push(math.cube(num));
|
214
|
+
}
|
215
|
+
return _a;
|
216
|
+
}).call(this);
|
217
|
+
;alert(cubed_list);'>run: cubed_list</button><br class='clear' /></div>
|
218
|
+
|
219
|
+
<p>
|
220
|
+
For a longer CoffeeScript example, check out
|
221
|
+
<a href="documentation/underscore.html">Underscore.coffee</a>, a port
|
222
|
+
of the <a href="http://documentcloud.github.com/underscore/">Underscore.js</a>
|
223
|
+
library of helper functions. Underscore.coffee can pass the entire Underscore.js
|
224
|
+
test suite. The CoffeeScript version is faster than the original for a number
|
225
|
+
of methods (in general, due to the speed of CoffeeScript's array comprehensions), and
|
226
|
+
after being minified and gzipped, is only 241 bytes larger than the original
|
227
|
+
JavaScript version.
|
228
|
+
Additional examples are included in the source repository, inside the
|
229
|
+
<a href="http://github.com/jashkenas/coffee-script/tree/master/examples/">examples</a> folder.
|
230
|
+
</p>
|
231
|
+
|
232
|
+
<h2>
|
233
|
+
<span id="installation" class="bookmark"></span>
|
234
|
+
Installation and Usage
|
235
|
+
</h2>
|
236
|
+
|
237
|
+
<p>
|
238
|
+
The CoffeeScript compiler is written in pure CoffeeScript, using a
|
239
|
+
<a href="http://github.com/jashkenas/coffee-script/blob/master/src/grammar.coffee">small DSL</a>
|
240
|
+
on top of the <a href="http://github.com/zaach/jison">Jison parser generator</a>, and is available
|
241
|
+
as a <a href="http://nodejs.org/">Node.js</a> utility. The core compiler however,
|
242
|
+
does not depend on Node, and can be run in other server-side-JavaScript environments,
|
243
|
+
or in the browser (see "Try CoffeeScript", above). This may be helpful,
|
244
|
+
as Node only run on flavors of nix, and not Windows, for the time being.
|
245
|
+
</p>
|
246
|
+
|
247
|
+
<p>
|
248
|
+
To install, first make sure you have a working version of
|
249
|
+
<a href="http://nodejs.org/">Node.js</a>, 0.1.30 or higher. Then clone the CoffeeScript
|
250
|
+
<a href="http://github.com/jashkenas/coffee-script">source repository</a>
|
251
|
+
from GitHub, or download the latest
|
252
|
+
release: <a href="http://github.com/jashkenas/coffee-script/tarball/0.5.1">0.5.1</a>.
|
253
|
+
To install the CoffeeScript compiler system-wide
|
254
|
+
under <tt>/usr/local</tt>, open the directory and run:
|
255
|
+
</p>
|
256
|
+
|
257
|
+
<pre>
|
258
|
+
sudo bin/cake install</pre>
|
259
|
+
|
260
|
+
<p>
|
261
|
+
This provides the <tt>coffee</tt> command, which can
|
262
|
+
be used to compile CoffeeScript <tt>.coffee</tt> files into JavaScript, as
|
263
|
+
well as debug them. The <tt>coffee</tt>
|
264
|
+
command also provides direct evaluation and an interactive REPL.
|
265
|
+
When compiling to JavaScript, <tt>coffee</tt> writes the output
|
266
|
+
as <tt>.js</tt> files in the same directory by default, but output
|
267
|
+
can be customized with the following options:
|
268
|
+
</p>
|
269
|
+
|
270
|
+
<table>
|
271
|
+
<tr>
|
272
|
+
<td width="25%"><code>-i, --interactive</code></td>
|
273
|
+
<td>
|
274
|
+
Launch an interactive CoffeeScript session to try short snippets.
|
275
|
+
</td>
|
276
|
+
</tr>
|
277
|
+
<tr>
|
278
|
+
<td><code>-r, --run</code></td>
|
279
|
+
<td>
|
280
|
+
Compile and execute a given CoffeeScript without saving the intermediate
|
281
|
+
JavaScript.
|
282
|
+
</td>
|
283
|
+
</tr>
|
284
|
+
<tr>
|
285
|
+
<td><code>-o, --output [DIR]</code></td>
|
286
|
+
<td>
|
287
|
+
Write out all compiled JavaScript files into the specified directory.
|
288
|
+
</td>
|
289
|
+
</tr>
|
290
|
+
<tr>
|
291
|
+
<td><code>-w, --watch</code></td>
|
292
|
+
<td>
|
293
|
+
Watch the modification times of the coffee-scripts, recompiling as
|
294
|
+
soon as a change occurs.
|
295
|
+
</td>
|
296
|
+
</tr>
|
297
|
+
<tr>
|
298
|
+
<td><code>-p, --print</code></td>
|
299
|
+
<td>
|
300
|
+
Instead of writing out the JavaScript as a file, print it
|
301
|
+
directly to <b>stdout</b>.
|
302
|
+
</td>
|
303
|
+
</tr>
|
304
|
+
<tr>
|
305
|
+
<td><code>-l, --lint</code></td>
|
306
|
+
<td>
|
307
|
+
If the <tt>jsl</tt>
|
308
|
+
(<a href="http://www.javascriptlint.com/">JavaScript Lint</a>)
|
309
|
+
command is installed, use it
|
310
|
+
to check the compilation of a CoffeeScript file. (Handy in
|
311
|
+
conjunction with <tt>--watch</tt>)
|
312
|
+
</td>
|
313
|
+
</tr>
|
314
|
+
<tr>
|
315
|
+
<td><code>-e, --eval</code></td>
|
316
|
+
<td>
|
317
|
+
Compile and print a little snippet of CoffeeScript directly from the
|
318
|
+
command line. For example:<br /><tt>coffee -e "square: (x) -> x * x"</tt>
|
319
|
+
</td>
|
320
|
+
</tr>
|
321
|
+
<tr>
|
322
|
+
<td><code>-n, --no-wrap</code></td>
|
323
|
+
<td>
|
324
|
+
Compile the JavaScript without the top-level function safety wrapper.
|
325
|
+
(Used for CoffeeScript as a Node.js module.)
|
326
|
+
</td>
|
327
|
+
</tr>
|
328
|
+
<tr>
|
329
|
+
<td><code>-t, --tokens</code></td>
|
330
|
+
<td>
|
331
|
+
Instead of parsing the CoffeeScript, just lex it, and print out the
|
332
|
+
token stream: <tt>[IDENTIFIER square] [ASSIGN :] [PARAM_START (]</tt> ...
|
333
|
+
</td>
|
334
|
+
</tr>
|
335
|
+
<tr>
|
336
|
+
<td><code>-tr, --tree</code></td>
|
337
|
+
<td>
|
338
|
+
Instead of compiling the CoffeeScript, just lex and parse it, and print
|
339
|
+
out the parse tree:
|
340
|
+
<pre class="no_bar">
|
341
|
+
Expressions
|
342
|
+
Assign
|
343
|
+
Value "square"
|
344
|
+
Code "x"
|
345
|
+
Op *
|
346
|
+
Value "x"
|
347
|
+
Value "x"</pre>
|
348
|
+
</td>
|
349
|
+
</tr>
|
350
|
+
</table>
|
351
|
+
|
352
|
+
<p>
|
353
|
+
<b>Examples:</b>
|
354
|
+
</p>
|
355
|
+
|
356
|
+
<pre>
|
357
|
+
coffee path/to/script.coffee
|
358
|
+
coffee --interactive
|
359
|
+
coffee --watch --lint experimental.coffee
|
360
|
+
coffee --print app/scripts/*.coffee > concatenation.js</pre>
|
361
|
+
|
362
|
+
<h2>
|
363
|
+
<span id="language" class="bookmark"></span>
|
364
|
+
Language Reference
|
365
|
+
</h2>
|
366
|
+
|
367
|
+
<p>
|
368
|
+
<i>
|
369
|
+
This reference is structured so that it can be read from top to bottom,
|
370
|
+
if you like. Later sections use ideas and syntax previously introduced.
|
371
|
+
Familiarity with JavaScript is assumed.
|
372
|
+
In all of the following examples, the source CoffeeScript is provided on
|
373
|
+
the left, and the direct compilation into JavaScript is on the right.
|
374
|
+
</i>
|
375
|
+
</p>
|
376
|
+
|
377
|
+
<p>
|
378
|
+
<i>
|
379
|
+
Many of the examples can be run (where it makes sense) by pressing the "run"
|
380
|
+
button towards the bottom right. You can also paste examples into
|
381
|
+
"Try CoffeeScript" in the toolbar, and play with them from there.
|
382
|
+
</i>
|
383
|
+
<p>
|
384
|
+
<span id="whitespace" class="bookmark"></span>
|
385
|
+
<b class="header">Significant Whitespace</b>
|
386
|
+
CoffeeScript uses Python-style significant whitespace: You don't need to
|
387
|
+
use semicolons <tt>;</tt> to terminate expressions, ending
|
388
|
+
the line will do just as well. Semicolons can still be used to fit
|
389
|
+
multiple expressions onto a single line. Instead of using curly braces
|
390
|
+
<tt>{ }</tt> to delimit blocks of code (like <a href="#functions">functions</a>,
|
391
|
+
<a href="#conditionals">if-statements</a>,
|
392
|
+
<a href="#switch">switch</a>, and <a href="#try">try/catch</a>),
|
393
|
+
use indentation.
|
394
|
+
</p>
|
395
|
+
|
396
|
+
<p>
|
397
|
+
You don't need to use parentheses to invoke a function if you're passing
|
398
|
+
arguments:<br /><tt>print "coffee"</tt>
|
399
|
+
</p>
|
400
|
+
|
401
|
+
<p>
|
402
|
+
You can use newlines to break up your expression into smaller pieces,
|
403
|
+
as long as CoffeeScript can determine that the line hasn't finished yet,
|
404
|
+
because it ends with an operator or a dot.
|
405
|
+
</p>
|
406
|
+
|
407
|
+
<p>
|
408
|
+
<span id="functions" class="bookmark"></span>
|
409
|
+
<b class="header">Functions and Invocation</b>
|
410
|
+
Functions are defined by a list of parameters, an arrow, and the
|
411
|
+
function body. The empty function looks like this: <tt>-></tt> All
|
412
|
+
functions in CoffeeScript are named by default, for easier debugging.
|
413
|
+
</p>
|
414
|
+
<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
|
415
|
+
<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
|
416
|
+
</pre><pre class="idle"><span class="Storage">var</span> cube, square;
|
417
|
+
square <span class="Keyword">=</span> <span class="Storage">function</span> <span class="FunctionName">square</span>(<span class="FunctionArgument">x</span>) {
|
418
|
+
<span class="Keyword">return</span> x <span class="Keyword">*</span> x;
|
419
|
+
};
|
420
|
+
cube <span class="Keyword">=</span> <span class="Storage">function</span> <span class="FunctionName">cube</span>(<span class="FunctionArgument">x</span>) {
|
421
|
+
<span class="Keyword">return</span> square(x) <span class="Keyword">*</span> x;
|
422
|
+
};
|
423
|
+
</pre><button onclick='javascript: var cube, square;
|
424
|
+
square = function square(x) {
|
425
|
+
return x * x;
|
426
|
+
};
|
427
|
+
cube = function cube(x) {
|
428
|
+
return square(x) * x;
|
429
|
+
};
|
430
|
+
;alert(cube(5));'>run: cube(5)</button><br class='clear' /></div>
|
431
|
+
<p>
|
432
|
+
If you'd like to assign a function literal to a variable, but not have
|
433
|
+
it be named, just wrap the function definition in parentheses:
|
434
|
+
<tt>((x) -> x * x)</tt>
|
435
|
+
</p>
|
436
|
+
|
437
|
+
<p>
|
438
|
+
<span id="assignment" class="bookmark"></span>
|
439
|
+
<b class="header">Assignment</b>
|
440
|
+
Use a colon <tt>:</tt> to assign, as in
|
441
|
+
<a href="http://json.org">JSON</a>. Equal signs are only needed for
|
442
|
+
mathy things. While colons are preferred, the two may be used interchangeably,
|
443
|
+
even within object literals.
|
444
|
+
</p>
|
445
|
+
<div class='code'><pre class="idle"><span class="FunctionName">greeting</span><span class="Keyword">:</span> <span class="String"><span class="String">"</span>Hello CoffeeScript<span class="String">"</span></span>
|
446
|
+
<span class="FunctionName">difficulty</span><span class="Keyword">:</span> <span class="Number">0.5</span>
|
447
|
+
</pre><pre class="idle"><span class="Storage">var</span> difficulty, greeting;
|
448
|
+
greeting <span class="Keyword">=</span> <span class="String"><span class="String">"</span>Hello CoffeeScript<span class="String">"</span></span>;
|
449
|
+
difficulty <span class="Keyword">=</span> <span class="Number">0.5</span>;
|
450
|
+
</pre><button onclick='javascript: var difficulty, greeting;
|
451
|
+
greeting = "Hello CoffeeScript";
|
452
|
+
difficulty = 0.5;
|
453
|
+
;alert(greeting);'>run: greeting</button><br class='clear' /></div>
|
454
|
+
<p>
|
455
|
+
All declaration of new variables is pushed up to the top of the nearest
|
456
|
+
lexical scope, so that assignment may always be performed within expressions.
|
457
|
+
</p>
|
458
|
+
|
459
|
+
<p>
|
460
|
+
<span id="objects_and_arrays" class="bookmark"></span>
|
461
|
+
<b class="header">Objects and Arrays</b>
|
462
|
+
Object and Array literals look very similar to their JavaScript cousins.
|
463
|
+
When you spread out each assignment on a separate line, the commas are
|
464
|
+
optional. In this way, assigning object properties looks the same as
|
465
|
+
assigning local variables, and can be moved around freely. Feel free to mix
|
466
|
+
and match the two styles.
|
467
|
+
</p>
|
468
|
+
<div class='code'><pre class="idle"><span class="FunctionName">song</span><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>]
|
469
|
+
|
470
|
+
<span class="FunctionName">ages</span><span class="Keyword">:</span> {
|
471
|
+
<span class="FunctionName">max</span><span class="Keyword">:</span> <span class="Number">10</span>
|
472
|
+
<span class="FunctionName">ida</span><span class="Keyword">:</span> <span class="Number">9</span>
|
473
|
+
<span class="FunctionName">tim</span><span class="Keyword">:</span> <span class="Number">11</span>
|
474
|
+
}
|
475
|
+
|
476
|
+
<span class="FunctionName">matrix</span><span class="Keyword">:</span> [
|
477
|
+
<span class="Number">1</span>, <span class="Number">0</span>, <span class="Number">1</span>
|
478
|
+
<span class="Number">0</span>, <span class="Number">0</span>, <span class="Number">1</span>
|
479
|
+
<span class="Number">1</span>, <span class="Number">1</span>, <span class="Number">0</span>
|
480
|
+
]
|
481
|
+
</pre><pre class="idle"><span class="Storage">var</span> ages, matrix, song;
|
482
|
+
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>];
|
483
|
+
ages <span class="Keyword">=</span> {
|
484
|
+
max: <span class="Number">10</span>,
|
485
|
+
ida: <span class="Number">9</span>,
|
486
|
+
tim: <span class="Number">11</span>
|
487
|
+
};
|
488
|
+
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>];
|
489
|
+
</pre><button onclick='javascript: var ages, matrix, song;
|
490
|
+
song = ["do", "re", "mi", "fa", "so"];
|
491
|
+
ages = {
|
492
|
+
max: 10,
|
493
|
+
ida: 9,
|
494
|
+
tim: 11
|
495
|
+
};
|
496
|
+
matrix = [1, 0, 1, 0, 0, 1, 1, 1, 0];
|
497
|
+
;alert(song.join(","));'>run: song.join(",")</button><br class='clear' /></div>
|
498
|
+
|
499
|
+
<p>
|
500
|
+
<span id="lexical_scope" class="bookmark"></span>
|
501
|
+
<b class="header">Lexical Scoping and Variable Safety</b>
|
502
|
+
The CoffeeScript compiler takes care to make sure that all of your variables
|
503
|
+
are properly declared within lexical scope — you never need to write
|
504
|
+
<tt>var</tt> yourself.
|
505
|
+
</p>
|
506
|
+
<div class='code'><pre class="idle"><span class="FunctionName">num</span><span class="Keyword">:</span> <span class="Number">1</span>
|
507
|
+
<span class="FunctionName">change_numbers</span><span class="Keyword">:</span> <span class="Storage">-></span>
|
508
|
+
<span class="FunctionName">new_num</span><span class="Keyword">:</span> <span class="Keyword">-</span><span class="Number">1</span>
|
509
|
+
<span class="FunctionName">num</span><span class="Keyword">:</span> <span class="Number">10</span>
|
510
|
+
<span class="FunctionName">new_num</span><span class="Keyword">:</span> change_numbers()
|
511
|
+
</pre><pre class="idle"><span class="Storage">var</span> change_numbers, new_num, num;
|
512
|
+
num <span class="Keyword">=</span> <span class="Number">1</span>;
|
513
|
+
change_numbers <span class="Keyword">=</span> <span class="Storage">function</span> <span class="FunctionName">change_numbers</span>() {
|
514
|
+
<span class="Storage">var</span> new_num;
|
515
|
+
new_num <span class="Keyword">=</span> <span class="Keyword">-</span><span class="Number">1</span>;
|
516
|
+
<span class="Keyword">return</span> num <span class="Keyword">=</span> <span class="Number">10</span>;
|
517
|
+
};
|
518
|
+
new_num <span class="Keyword">=</span> change_numbers();
|
519
|
+
</pre><button onclick='javascript: var change_numbers, new_num, num;
|
520
|
+
num = 1;
|
521
|
+
change_numbers = function change_numbers() {
|
522
|
+
var new_num;
|
523
|
+
new_num = -1;
|
524
|
+
return num = 10;
|
525
|
+
};
|
526
|
+
new_num = change_numbers();
|
527
|
+
;alert(new_num);'>run: new_num</button><br class='clear' /></div>
|
528
|
+
<p>
|
529
|
+
Notice how the all of the variable declarations have been pushed up to
|
530
|
+
the top of the closest scope, the first time they appear.
|
531
|
+
<b>num</b> is not redeclared within the inner function, because it's
|
532
|
+
already in scope; the <b>new_num</b> within the function, on the other hand,
|
533
|
+
should not be able to change the value of the external variable of the same name, and
|
534
|
+
therefore has a declaration of its own.
|
535
|
+
</p>
|
536
|
+
<p>
|
537
|
+
Although suppressed within this documentation for clarity, all
|
538
|
+
CoffeeScript output is wrapped in an anonymous function:
|
539
|
+
<tt>(function(){ ... })();</tt> This safety wrapper, combined with the
|
540
|
+
automatic generation of the <tt>var</tt> keyword, make it exceedingly difficult
|
541
|
+
to pollute the global namespace by accident.
|
542
|
+
</p>
|
543
|
+
<p>
|
544
|
+
If you'd like to create top-level variables for other scripts to use,
|
545
|
+
attach them as properties on <b>window</b>, or on the <b>exports</b>
|
546
|
+
object in CommonJS. The <b>existential operator</b> (below), gives you a
|
547
|
+
reliable way to figure out where to add them, if you're targeting both
|
548
|
+
CommonJS and the browser: <tt>root: exports ? this</tt>
|
549
|
+
</p>
|
550
|
+
|
551
|
+
<p>
|
552
|
+
<span id="conditionals" class="bookmark"></span>
|
553
|
+
<b class="header">Conditionals, Ternaries, and Conditional Assignment</b>
|
554
|
+
<b>If/else</b> statements can be written without the use of parentheses and
|
555
|
+
curly brackets. As with functions and other block expressions,
|
556
|
+
multi-line conditionals are delimited by indentation. There's also a handy
|
557
|
+
postfix form, with the <tt>if</tt> or <tt>unless</tt> at the end.
|
558
|
+
</p>
|
559
|
+
<p>
|
560
|
+
CoffeeScript will compile <b>if</b> statements using the ternary operator
|
561
|
+
when possible, to make it easier to use the result as an expression.
|
562
|
+
</p>
|
563
|
+
<div class='code'><pre class="idle"><span class="FunctionName">mood</span><span class="Keyword">:</span> greatly_improved <span class="Keyword">if</span> singing
|
564
|
+
|
565
|
+
<span class="Keyword">if</span> happy <span class="Keyword">and</span> knows_it
|
566
|
+
claps_hands()
|
567
|
+
cha_cha_cha()
|
568
|
+
|
569
|
+
<span class="FunctionName">date</span><span class="Keyword">:</span> <span class="Keyword">if</span> friday <span class="Keyword">then</span> sue <span class="Keyword">else</span> jill
|
570
|
+
|
571
|
+
expensive <span class="Keyword">||</span><span class="Keyword">=</span> do_the_math()
|
572
|
+
</pre><pre class="idle"><span class="Storage">var</span> date, expensive, mood;
|
573
|
+
<span class="Keyword">if</span> (singing) {
|
574
|
+
mood <span class="Keyword">=</span> greatly_improved;
|
575
|
+
}
|
576
|
+
<span class="Keyword">if</span> (happy <span class="Keyword">&</span><span class="Keyword">&</span> knows_it) {
|
577
|
+
claps_hands();
|
578
|
+
cha_cha_cha();
|
579
|
+
}
|
580
|
+
date <span class="Keyword">=</span> friday ? sue : jill;
|
581
|
+
expensive <span class="Keyword">=</span> expensive <span class="Keyword">||</span> do_the_math();
|
582
|
+
</pre><br class='clear' /></div>
|
583
|
+
<p>
|
584
|
+
The conditional assignment operators are included: <tt>||=</tt>,
|
585
|
+
which only assigns a value to a variable if the variable's current value
|
586
|
+
is falsy, and <tt>&&=</tt>, which only replaces the value of
|
587
|
+
truthy variables.
|
588
|
+
</p>
|
589
|
+
|
590
|
+
<p>
|
591
|
+
<span id="aliases" class="bookmark"></span>
|
592
|
+
<b class="header">Aliases</b>
|
593
|
+
Because the <tt>==</tt> operator frequently causes undesirable coercion,
|
594
|
+
is intransitive, and has a different meaning than in other languages,
|
595
|
+
CoffeeScript compiles <tt>==</tt> into <tt>===</tt>, and <tt>!=</tt> into
|
596
|
+
<tt>!==</tt>.
|
597
|
+
In addition, <tt>is</tt> compiles into <tt>===</tt>,
|
598
|
+
and <tt>isnt</tt> into <tt>!==</tt>.
|
599
|
+
</p>
|
600
|
+
<p>
|
601
|
+
You can use <tt>not</tt> as an alias for <tt>!</tt>.
|
602
|
+
</p>
|
603
|
+
<p>
|
604
|
+
For logic, <tt>and</tt> compiles to <tt>&&</tt>, and <tt>or</tt>
|
605
|
+
into <tt>||</tt>.
|
606
|
+
</p>
|
607
|
+
<p>
|
608
|
+
Instead of a newline or semicolon, <tt>then</tt> can be used to separate
|
609
|
+
conditions from expressions, in <b>while</b>,
|
610
|
+
<b>if</b>/<b>else</b>, and <b>switch</b>/<b>when</b> statements.
|
611
|
+
</p>
|
612
|
+
<p>
|
613
|
+
As in <a href="http://yaml.org/">YAML</a>, <tt>on</tt> and <tt>yes</tt>
|
614
|
+
are the same as boolean <tt>true</tt>, while <tt>off</tt> and <tt>no</tt> are boolean <tt>false</tt>.
|
615
|
+
</p>
|
616
|
+
<p>
|
617
|
+
For single-line statements, <tt>unless</tt> can be used as the inverse of <tt>if</tt>.
|
618
|
+
</p>
|
619
|
+
<p>
|
620
|
+
As a shortcut for <tt>this.property</tt>, you can use <tt>@property</tt>.
|
621
|
+
</p>
|
622
|
+
<div class='code'><pre class="idle">launch() <span class="Keyword">if</span> ignition <span class="Keyword">is</span> <span class="BuiltInConstant">on</span>
|
623
|
+
|
624
|
+
<span class="FunctionName">volume</span><span class="Keyword">:</span> <span class="Number">10</span> <span class="Keyword">if</span> band <span class="Keyword">isnt</span> spinal_tap
|
625
|
+
|
626
|
+
let_the_wild_rumpus_begin() <span class="Keyword">unless</span> answer <span class="Keyword">is</span> <span class="BuiltInConstant">no</span>
|
627
|
+
|
628
|
+
<span class="Keyword">if</span> car.speed <span class="Keyword"><</span> speed_limit <span class="Keyword">then</span> accelerate()
|
629
|
+
|
630
|
+
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>
|
631
|
+
</pre><pre class="idle"><span class="Storage">var</span> volume;
|
632
|
+
<span class="Keyword">if</span> (ignition <span class="Keyword">===</span> <span class="BuiltInConstant">true</span>) {
|
633
|
+
launch();
|
634
|
+
}
|
635
|
+
<span class="Keyword">if</span> (band <span class="Keyword">!</span><span class="Keyword">==</span> spinal_tap) {
|
636
|
+
volume <span class="Keyword">=</span> <span class="Number">10</span>;
|
637
|
+
}
|
638
|
+
<span class="Keyword">if</span> (<span class="Keyword">!</span>(answer <span class="Keyword">===</span> <span class="BuiltInConstant">false</span>)) {
|
639
|
+
let_the_wild_rumpus_begin();
|
640
|
+
}
|
641
|
+
car.speed <span class="Keyword"><</span> speed_limit ? accelerate() : <span class="BuiltInConstant">null</span>;
|
642
|
+
<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>);
|
643
|
+
</pre><br class='clear' /></div>
|
644
|
+
|
645
|
+
<p>
|
646
|
+
<span id="splats" class="bookmark"></span>
|
647
|
+
<b class="header">Splats...</b>
|
648
|
+
The JavaScript <b>arguments object</b> is a useful way to work with
|
649
|
+
functions that accept variable numbers of arguments. CoffeeScript provides
|
650
|
+
splats <tt>...</tt>, both for function definition as well as invocation,
|
651
|
+
making variable numbers of arguments a little bit more palatable.
|
652
|
+
</p>
|
653
|
+
<div class='code'><pre class="idle"><span class="FunctionName">gold</span><span class="Keyword">:</span> <span class="FunctionName">silver</span><span class="Keyword">:</span> <span class="FunctionName">the_field</span><span class="Keyword">:</span> <span class="String"><span class="String">"</span>unknown<span class="String">"</span></span>
|
654
|
+
|
655
|
+
<span class="FunctionName">award_medals</span><span class="Keyword">:</span> <span class="FunctionArgument">(</span><span class="FunctionArgument">first, second, rest...</span><span class="FunctionArgument">)</span> <span class="Storage">-></span>
|
656
|
+
<span class="FunctionName">gold</span><span class="Keyword">:</span> first
|
657
|
+
<span class="FunctionName">silver</span><span class="Keyword">:</span> second
|
658
|
+
<span class="FunctionName">the_field</span><span class="Keyword">:</span> rest
|
659
|
+
|
660
|
+
<span class="FunctionName">contenders</span><span class="Keyword">:</span> [
|
661
|
+
<span class="String"><span class="String">"</span>Michael Phelps<span class="String">"</span></span>
|
662
|
+
<span class="String"><span class="String">"</span>Liu Xiang<span class="String">"</span></span>
|
663
|
+
<span class="String"><span class="String">"</span>Yao Ming<span class="String">"</span></span>
|
664
|
+
<span class="String"><span class="String">"</span>Allyson Felix<span class="String">"</span></span>
|
665
|
+
<span class="String"><span class="String">"</span>Shawn Johnson<span class="String">"</span></span>
|
666
|
+
<span class="String"><span class="String">"</span>Roman Sebrle<span class="String">"</span></span>
|
667
|
+
<span class="String"><span class="String">"</span>Guo Jingjing<span class="String">"</span></span>
|
668
|
+
<span class="String"><span class="String">"</span>Tyson Gay<span class="String">"</span></span>
|
669
|
+
<span class="String"><span class="String">"</span>Asafa Powell<span class="String">"</span></span>
|
670
|
+
<span class="String"><span class="String">"</span>Usain Bolt<span class="String">"</span></span>
|
671
|
+
]
|
672
|
+
|
673
|
+
award_medals contenders...
|
674
|
+
|
675
|
+
alert <span class="String"><span class="String">"</span>Gold: <span class="String">"</span></span> <span class="Keyword">+</span> gold
|
676
|
+
alert <span class="String"><span class="String">"</span>Silver: <span class="String">"</span></span> <span class="Keyword">+</span> silver
|
677
|
+
alert <span class="String"><span class="String">"</span>The Field: <span class="String">"</span></span> <span class="Keyword">+</span> the_field
|
678
|
+
</pre><pre class="idle"><span class="Storage">var</span> award_medals, contenders, gold, silver, the_field;
|
679
|
+
gold <span class="Keyword">=</span> (silver <span class="Keyword">=</span> (the_field <span class="Keyword">=</span> <span class="String"><span class="String">"</span>unknown<span class="String">"</span></span>));
|
680
|
+
award_medals <span class="Keyword">=</span> <span class="Storage">function</span> <span class="FunctionName">award_medals</span>(<span class="FunctionArgument">first, second</span>) {
|
681
|
+
<span class="Storage">var</span> rest;
|
682
|
+
rest <span class="Keyword">=</span> <span class="LibraryClassType">Array</span>.<span class="LibraryConstant">prototype</span>.slice.<span class="LibraryFunction">call</span>(arguments, <span class="Number">2</span>);
|
683
|
+
gold <span class="Keyword">=</span> first;
|
684
|
+
silver <span class="Keyword">=</span> second;
|
685
|
+
<span class="Keyword">return</span> the_field <span class="Keyword">=</span> rest;
|
686
|
+
};
|
687
|
+
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>];
|
688
|
+
award_medals.<span class="LibraryFunction">apply</span>(<span class="Variable">this</span>, contenders);
|
689
|
+
<span class="LibraryFunction">alert</span>(<span class="String"><span class="String">"</span>Gold: <span class="String">"</span></span> <span class="Keyword">+</span> gold);
|
690
|
+
<span class="LibraryFunction">alert</span>(<span class="String"><span class="String">"</span>Silver: <span class="String">"</span></span> <span class="Keyword">+</span> silver);
|
691
|
+
<span class="LibraryFunction">alert</span>(<span class="String"><span class="String">"</span>The Field: <span class="String">"</span></span> <span class="Keyword">+</span> the_field);
|
692
|
+
</pre><button onclick='javascript: var award_medals, contenders, gold, silver, the_field;
|
693
|
+
gold = (silver = (the_field = "unknown"));
|
694
|
+
award_medals = function award_medals(first, second) {
|
695
|
+
var rest;
|
696
|
+
rest = Array.prototype.slice.call(arguments, 2);
|
697
|
+
gold = first;
|
698
|
+
silver = second;
|
699
|
+
return the_field = rest;
|
700
|
+
};
|
701
|
+
contenders = ["Michael Phelps", "Liu Xiang", "Yao Ming", "Allyson Felix", "Shawn Johnson", "Roman Sebrle", "Guo Jingjing", "Tyson Gay", "Asafa Powell", "Usain Bolt"];
|
702
|
+
award_medals.apply(this, contenders);
|
703
|
+
alert("Gold: " + gold);
|
704
|
+
alert("Silver: " + silver);
|
705
|
+
alert("The Field: " + the_field);
|
706
|
+
;'>run</button><br class='clear' /></div>
|
707
|
+
|
708
|
+
<p>
|
709
|
+
<span id="arguments" class="bookmark"></span>
|
710
|
+
<b class="header">Arguments are Arrays</b>
|
711
|
+
If you reference the <b>arguments object</b> directly, it will be converted
|
712
|
+
into a real Array, making all of the
|
713
|
+
<a href="https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Objects/Array">Array methods</a>
|
714
|
+
available.
|
715
|
+
</p>
|
716
|
+
<div class='code'><pre class="idle"><span class="FunctionName">backwards</span><span class="Keyword">:</span> <span class="Storage">-></span>
|
717
|
+
alert arguments.reverse()
|
718
|
+
|
719
|
+
backwards <span class="String"><span class="String">"</span>stairway<span class="String">"</span></span>, <span class="String"><span class="String">"</span>to<span class="String">"</span></span>, <span class="String"><span class="String">"</span>heaven<span class="String">"</span></span>
|
720
|
+
</pre><pre class="idle"><span class="Storage">var</span> backwards;
|
721
|
+
backwards <span class="Keyword">=</span> <span class="Storage">function</span> <span class="FunctionName">backwards</span>() {
|
722
|
+
arguments <span class="Keyword">=</span> <span class="LibraryClassType">Array</span>.<span class="LibraryConstant">prototype</span>.slice.<span class="LibraryFunction">call</span>(arguments, <span class="Number">0</span>);
|
723
|
+
<span class="Keyword">return</span> <span class="LibraryFunction">alert</span>(arguments.<span class="LibraryFunction">reverse</span>());
|
724
|
+
};
|
725
|
+
backwards(<span class="String"><span class="String">"</span>stairway<span class="String">"</span></span>, <span class="String"><span class="String">"</span>to<span class="String">"</span></span>, <span class="String"><span class="String">"</span>heaven<span class="String">"</span></span>);
|
726
|
+
</pre><button onclick='javascript: var backwards;
|
727
|
+
backwards = function backwards() {
|
728
|
+
arguments = Array.prototype.slice.call(arguments, 0);
|
729
|
+
return alert(arguments.reverse());
|
730
|
+
};
|
731
|
+
backwards("stairway", "to", "heaven");
|
732
|
+
;'>run</button><br class='clear' /></div>
|
733
|
+
|
734
|
+
<p>
|
735
|
+
<span id="while" class="bookmark"></span>
|
736
|
+
<b class="header">While Loops</b>
|
737
|
+
The only low-level loop that CoffeeScript provides is the <b>while</b> loop. The
|
738
|
+
main difference from JavaScript is that the <b>while</b> loop can be used
|
739
|
+
as an expression, returning an array containing the result of each iteration
|
740
|
+
through the loop.
|
741
|
+
</p>
|
742
|
+
<div class='code'><pre class="idle"><span class="Comment"><span class="Comment">#</span> Econ 101</span>
|
743
|
+
<span class="Keyword">if</span> <span class="Variable">this</span>.studying_economics
|
744
|
+
<span class="Keyword">while</span> supply <span class="Keyword">></span> demand <span class="Keyword">then</span> buy()
|
745
|
+
<span class="Keyword">while</span> supply <span class="Keyword"><</span> demand <span class="Keyword">then</span> sell()
|
746
|
+
|
747
|
+
<span class="Comment"><span class="Comment">#</span> Nursery Rhyme</span>
|
748
|
+
<span class="FunctionName">num</span><span class="Keyword">:</span> <span class="Number">6</span>
|
749
|
+
<span class="FunctionName">lyrics</span><span class="Keyword">:</span> <span class="Keyword">while</span> num <span class="Keyword">-</span><span class="Keyword">=</span> <span class="Number">1</span>
|
750
|
+
num <span class="Keyword">+</span> <span class="String"><span class="String">"</span> little monkeys, jumping on the bed.</span>
|
751
|
+
<span class="String"> One fell out and bumped his head.<span class="String">"</span></span>
|
752
|
+
</pre><pre class="idle"><span class="Storage">var</span> _a, lyrics, num;
|
753
|
+
<span class="Comment"><span class="Comment">//</span> Econ 101</span>
|
754
|
+
<span class="Keyword">if</span> (<span class="Variable">this</span>.studying_economics) {
|
755
|
+
<span class="Keyword">while</span> (supply <span class="Keyword">></span> demand) {
|
756
|
+
buy();
|
757
|
+
}
|
758
|
+
<span class="Keyword">while</span> (supply <span class="Keyword"><</span> demand) {
|
759
|
+
sell();
|
760
|
+
}
|
761
|
+
}
|
762
|
+
<span class="Comment"><span class="Comment">//</span> Nursery Rhyme</span>
|
763
|
+
num <span class="Keyword">=</span> <span class="Number">6</span>;
|
764
|
+
lyrics <span class="Keyword">=</span> (<span class="Storage">function</span>() {
|
765
|
+
_a <span class="Keyword">=</span> [];
|
766
|
+
<span class="Keyword">while</span> (num <span class="Keyword">-</span><span class="Keyword">=</span> <span class="Number">1</span>) {
|
767
|
+
_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>
|
768
|
+
<span class="String">One fell out and bumped his head.<span class="String">"</span></span>);
|
769
|
+
}
|
770
|
+
<span class="Keyword">return</span> _a;
|
771
|
+
}).<span class="LibraryFunction">call</span>(<span class="Variable">this</span>);
|
772
|
+
</pre><button onclick='javascript: var _a, lyrics, num;
|
773
|
+
// Econ 101
|
774
|
+
if (this.studying_economics) {
|
775
|
+
while (supply > demand) {
|
776
|
+
buy();
|
777
|
+
}
|
778
|
+
while (supply < demand) {
|
779
|
+
sell();
|
780
|
+
}
|
781
|
+
}
|
782
|
+
// Nursery Rhyme
|
783
|
+
num = 6;
|
784
|
+
lyrics = (function() {
|
785
|
+
_a = [];
|
786
|
+
while (num -= 1) {
|
787
|
+
_a.push(num + " little monkeys, jumping on the bed. \
|
788
|
+
One fell out and bumped his head.");
|
789
|
+
}
|
790
|
+
return _a;
|
791
|
+
}).call(this);
|
792
|
+
;alert(lyrics.join("\n"));'>run: lyrics.join("\n")</button><br class='clear' /></div>
|
793
|
+
<p>
|
794
|
+
Other JavaScript loops, such as <b>for</b> loops and <b>do-while</b> loops
|
795
|
+
can be mimicked by variations on <b>while</b>, but the hope is that you
|
796
|
+
won't need to do that with CoffeeScript, either because you're using
|
797
|
+
<b>each</b> (<b>forEach</b>) style iterators, or...
|
798
|
+
</p>
|
799
|
+
|
800
|
+
<p>
|
801
|
+
<span id="comprehensions" class="bookmark"></span>
|
802
|
+
<b class="header">Comprehensions (Arrays, Objects, and Ranges)</b>
|
803
|
+
For your looping needs, CoffeeScript provides array comprehensions
|
804
|
+
similar to Python's. They replace (and compile into) <b>for</b> loops, with
|
805
|
+
optional guard clauses and the value of the current array index.
|
806
|
+
Unlike for loops, array comprehensions are expressions, and can be returned
|
807
|
+
and assigned. They should be able to handle most places where you otherwise
|
808
|
+
would use a loop, <b>each</b>/<b>forEach</b>, <b>map</b>, or <b>select</b>/<b>filter</b>.
|
809
|
+
</p>
|
810
|
+
<div class='code'><pre class="idle"><span class="Comment"><span class="Comment">#</span> Eat lunch.</span>
|
811
|
+
<span class="FunctionName">lunch</span><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>]
|
812
|
+
|
813
|
+
<span class="Comment"><span class="Comment">#</span> Naive collision detection.</span>
|
814
|
+
<span class="Keyword">for</span> roid <span class="Keyword">in</span> asteroids
|
815
|
+
<span class="Keyword">for</span> roid2 <span class="Keyword">in</span> asteroids <span class="Keyword">when</span> roid <span class="Keyword">isnt</span> roid2
|
816
|
+
roid.explode() <span class="Keyword">if</span> roid.overlaps roid2
|
817
|
+
</pre><pre class="idle"><span class="Storage">var</span> _a, _b, _c, _d, _e, _f, _g, food, lunch, roid, roid2;
|
818
|
+
<span class="Comment"><span class="Comment">//</span> Eat lunch.</span>
|
819
|
+
lunch <span class="Keyword">=</span> (<span class="Storage">function</span>() {
|
820
|
+
_a <span class="Keyword">=</span> []; _b <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>];
|
821
|
+
<span class="Keyword">for</span> (_c <span class="Keyword">=</span> <span class="Number">0</span>; _c <span class="Keyword"><</span> _b.<span class="LibraryConstant">length</span>; _c<span class="Keyword">++</span>) {
|
822
|
+
food <span class="Keyword">=</span> _b[_c];
|
823
|
+
_a.<span class="LibraryFunction">push</span>(eat(food));
|
824
|
+
}
|
825
|
+
<span class="Keyword">return</span> _a;
|
826
|
+
}).<span class="LibraryFunction">call</span>(<span class="Variable">this</span>);
|
827
|
+
<span class="Comment"><span class="Comment">//</span> Naive collision detection.</span>
|
828
|
+
_d <span class="Keyword">=</span> asteroids;
|
829
|
+
<span class="Keyword">for</span> (_e <span class="Keyword">=</span> <span class="Number">0</span>; _e <span class="Keyword"><</span> _d.<span class="LibraryConstant">length</span>; _e<span class="Keyword">++</span>) {
|
830
|
+
roid <span class="Keyword">=</span> _d[_e];
|
831
|
+
_f <span class="Keyword">=</span> asteroids;
|
832
|
+
<span class="Keyword">for</span> (_g <span class="Keyword">=</span> <span class="Number">0</span>; _g <span class="Keyword"><</span> _f.<span class="LibraryConstant">length</span>; _g<span class="Keyword">++</span>) {
|
833
|
+
roid2 <span class="Keyword">=</span> _f[_g];
|
834
|
+
<span class="Keyword">if</span> (roid <span class="Keyword">!</span><span class="Keyword">==</span> roid2) {
|
835
|
+
<span class="Keyword">if</span> (roid.overlaps(roid2)) {
|
836
|
+
roid.explode();
|
837
|
+
}
|
838
|
+
}
|
839
|
+
}
|
840
|
+
}
|
841
|
+
</pre><br class='clear' /></div>
|
842
|
+
<p>
|
843
|
+
If you know the start and end of your loop, or would like to step through
|
844
|
+
in fixed-size increments, you can use a range to specify the start and
|
845
|
+
end of your comprehension. (The long line-breaking "for" definitions in
|
846
|
+
the compiled JS below allow ranges to count downwards, as well as upwards).
|
847
|
+
</p>
|
848
|
+
<div class='code'><pre class="idle"><span class="FunctionName">countdown</span><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>]
|
849
|
+
|
850
|
+
<span class="FunctionName">egg_delivery</span><span class="Keyword">:</span> <span class="Storage">-></span>
|
851
|
+
<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>
|
852
|
+
<span class="FunctionName">dozen_eggs</span><span class="Keyword">:</span> eggs[i...i<span class="Keyword">+</span><span class="Number">12</span>]
|
853
|
+
deliver <span class="Keyword">new</span> <span class="TypeName">egg_carton</span>(dozen)
|
854
|
+
</pre><pre class="idle"><span class="Storage">var</span> _a, _b, _c, _d, _e, countdown, egg_delivery, num;
|
855
|
+
countdown <span class="Keyword">=</span> (<span class="Storage">function</span>() {
|
856
|
+
_a <span class="Keyword">=</span> []; _d <span class="Keyword">=</span> <span class="Number">10</span>; _e <span class="Keyword">=</span> <span class="Number">1</span>;
|
857
|
+
<span class="Keyword">for</span> (_c <span class="Keyword">=</span> <span class="Number">0</span>, num<span class="Keyword">=</span>_d; (_d <span class="Keyword"><=</span> _e ? num <span class="Keyword"><=</span> _e : num <span class="Keyword">>=</span> _e); (_d <span class="Keyword"><=</span> _e ? num <span class="Keyword">+</span><span class="Keyword">=</span> <span class="Number">1</span> : num <span class="Keyword">-</span><span class="Keyword">=</span> <span class="Number">1</span>), _c<span class="Keyword">++</span>) {
|
858
|
+
_a.<span class="LibraryFunction">push</span>(num);
|
859
|
+
}
|
860
|
+
<span class="Keyword">return</span> _a;
|
861
|
+
}).<span class="LibraryFunction">call</span>(<span class="Variable">this</span>);
|
862
|
+
egg_delivery <span class="Keyword">=</span> <span class="Storage">function</span> <span class="FunctionName">egg_delivery</span>() {
|
863
|
+
<span class="Storage">var</span> _f, _g, _h, _i, _j, dozen_eggs, i;
|
864
|
+
_f <span class="Keyword">=</span> []; _i <span class="Keyword">=</span> <span class="Number">0</span>; _j <span class="Keyword">=</span> eggs.<span class="LibraryConstant">length</span>;
|
865
|
+
<span class="Keyword">for</span> (_h <span class="Keyword">=</span> <span class="Number">0</span>, i<span class="Keyword">=</span>_i; (_i <span class="Keyword"><=</span> _j ? i <span class="Keyword"><</span> _j : i <span class="Keyword">></span> _j); (_i <span class="Keyword"><=</span> _j ? i <span class="Keyword">+</span><span class="Keyword">=</span> <span class="Number">12</span> : i <span class="Keyword">-</span><span class="Keyword">=</span> <span class="Number">12</span>), _h<span class="Keyword">++</span>) {
|
866
|
+
_f.<span class="LibraryFunction">push</span>((<span class="Storage">function</span>() {
|
867
|
+
dozen_eggs <span class="Keyword">=</span> eggs.<span class="LibraryFunction">slice</span>(i, i <span class="Keyword">+</span> <span class="Number">12</span>);
|
868
|
+
<span class="Keyword">return</span> deliver(<span class="Keyword">new</span> <span class="TypeName">egg_carton</span>(dozen));
|
869
|
+
}).<span class="LibraryFunction">call</span>(<span class="Variable">this</span>));
|
870
|
+
}
|
871
|
+
<span class="Keyword">return</span> _f;
|
872
|
+
};
|
873
|
+
</pre><button onclick='javascript: var _a, _b, _c, _d, _e, countdown, egg_delivery, num;
|
874
|
+
countdown = (function() {
|
875
|
+
_a = []; _d = 10; _e = 1;
|
876
|
+
for (_c = 0, num=_d; (_d <= _e ? num <= _e : num >= _e); (_d <= _e ? num += 1 : num -= 1), _c++) {
|
877
|
+
_a.push(num);
|
878
|
+
}
|
879
|
+
return _a;
|
880
|
+
}).call(this);
|
881
|
+
egg_delivery = function egg_delivery() {
|
882
|
+
var _f, _g, _h, _i, _j, dozen_eggs, i;
|
883
|
+
_f = []; _i = 0; _j = eggs.length;
|
884
|
+
for (_h = 0, i=_i; (_i <= _j ? i < _j : i > _j); (_i <= _j ? i += 12 : i -= 12), _h++) {
|
885
|
+
_f.push((function() {
|
886
|
+
dozen_eggs = eggs.slice(i, i + 12);
|
887
|
+
return deliver(new egg_carton(dozen));
|
888
|
+
}).call(this));
|
889
|
+
}
|
890
|
+
return _f;
|
891
|
+
};
|
892
|
+
;alert(countdown);'>run: countdown</button><br class='clear' /></div>
|
893
|
+
<p>
|
894
|
+
Comprehensions can also be used to iterate over the keys and values in
|
895
|
+
an object. Use <tt>of</tt> to signal comprehension over the properties of
|
896
|
+
an object instead of the values in an array.
|
897
|
+
</p>
|
898
|
+
<div class='code'><pre class="idle"><span class="FunctionName">years_old</span><span class="Keyword">:</span> {<span class="FunctionName">max</span><span class="Keyword">:</span> <span class="Number">10</span>, <span class="FunctionName">ida</span><span class="Keyword">:</span> <span class="Number">9</span>, <span class="FunctionName">tim</span><span class="Keyword">:</span> <span class="Number">11</span>}
|
899
|
+
|
900
|
+
<span class="FunctionName">ages</span><span class="Keyword">:</span> <span class="Keyword">for</span> child, age <span class="Keyword">of</span> years_old
|
901
|
+
child <span class="Keyword">+</span> <span class="String"><span class="String">"</span> is <span class="String">"</span></span> <span class="Keyword">+</span> age
|
902
|
+
</pre><pre class="idle"><span class="Storage">var</span> _a, _b, age, ages, child, years_old;
|
903
|
+
<span class="Storage">var</span> __hasProp <span class="Keyword">=</span> <span class="LibraryClassType">Object</span>.<span class="LibraryConstant">prototype</span>.hasOwnProperty;
|
904
|
+
years_old <span class="Keyword">=</span> {
|
905
|
+
max: <span class="Number">10</span>,
|
906
|
+
ida: <span class="Number">9</span>,
|
907
|
+
tim: <span class="Number">11</span>
|
908
|
+
};
|
909
|
+
ages <span class="Keyword">=</span> (<span class="Storage">function</span>() {
|
910
|
+
_a <span class="Keyword">=</span> []; _b <span class="Keyword">=</span> years_old;
|
911
|
+
<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)) {
|
912
|
+
age <span class="Keyword">=</span> _b[child];
|
913
|
+
_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);
|
914
|
+
}}
|
915
|
+
<span class="Keyword">return</span> _a;
|
916
|
+
}).<span class="LibraryFunction">call</span>(<span class="Variable">this</span>);
|
917
|
+
</pre><button onclick='javascript: var _a, _b, age, ages, child, years_old;
|
918
|
+
var __hasProp = Object.prototype.hasOwnProperty;
|
919
|
+
years_old = {
|
920
|
+
max: 10,
|
921
|
+
ida: 9,
|
922
|
+
tim: 11
|
923
|
+
};
|
924
|
+
ages = (function() {
|
925
|
+
_a = []; _b = years_old;
|
926
|
+
for (child in _b) { if (__hasProp.call(_b, child)) {
|
927
|
+
age = _b[child];
|
928
|
+
_a.push(child + " is " + age);
|
929
|
+
}}
|
930
|
+
return _a;
|
931
|
+
}).call(this);
|
932
|
+
;alert(ages.join(", "));'>run: ages.join(", ")</button><br class='clear' /></div>
|
933
|
+
|
934
|
+
<p>
|
935
|
+
<span id="slice_splice" class="bookmark"></span>
|
936
|
+
<b class="header">Array Slicing and Splicing with Ranges</b>
|
937
|
+
CoffeeScript borrows Ruby's
|
938
|
+
<a href="http://ruby-doc.org/core/classes/Range.html">range syntax</a>
|
939
|
+
for extracting slices of arrays. With two dots (<tt>3..5</tt>), the range
|
940
|
+
is inclusive: the first argument is the index of the first element in
|
941
|
+
the slice, and the second is the index of the last one. Three dots signify
|
942
|
+
a range that excludes the end.
|
943
|
+
</p>
|
944
|
+
<div class='code'><pre class="idle"><span class="FunctionName">numbers</span><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>]
|
945
|
+
|
946
|
+
<span class="FunctionName">three_to_six</span><span class="Keyword">:</span> numbers[<span class="Number">3</span>..<span class="Number">6</span>]
|
947
|
+
|
948
|
+
<span class="FunctionName">numbers_copy</span><span class="Keyword">:</span> numbers[<span class="Number">0</span>...numbers.length]
|
949
|
+
|
950
|
+
</pre><pre class="idle"><span class="Storage">var</span> numbers, numbers_copy, three_to_six;
|
951
|
+
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>];
|
952
|
+
three_to_six <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>);
|
953
|
+
numbers_copy <span class="Keyword">=</span> numbers.<span class="LibraryFunction">slice</span>(<span class="Number">0</span>, numbers.<span class="LibraryConstant">length</span>);
|
954
|
+
</pre><button onclick='javascript: var numbers, numbers_copy, three_to_six;
|
955
|
+
numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
|
956
|
+
three_to_six = numbers.slice(3, 6 + 1);
|
957
|
+
numbers_copy = numbers.slice(0, numbers.length);
|
958
|
+
;alert(numbers_copy);'>run: numbers_copy</button><br class='clear' /></div>
|
959
|
+
<p>
|
960
|
+
The same syntax can be used with assignment to replace a segment of an
|
961
|
+
array with new values (to splice it).
|
962
|
+
</p>
|
963
|
+
<div class='code'><pre class="idle"><span class="FunctionName">numbers</span><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>]
|
964
|
+
|
965
|
+
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>]
|
966
|
+
|
967
|
+
|
968
|
+
</pre><pre class="idle"><span class="Storage">var</span> numbers;
|
969
|
+
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>];
|
970
|
+
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>]));
|
971
|
+
</pre><button onclick='javascript: var numbers;
|
972
|
+
numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
|
973
|
+
numbers.splice.apply(numbers, [3, 6 - 3 + 1].concat([-3, -4, -5, -6]));
|
974
|
+
;alert(numbers);'>run: numbers</button><br class='clear' /></div>
|
975
|
+
|
976
|
+
<p>
|
977
|
+
<span id="expressions" class="bookmark"></span>
|
978
|
+
<b class="header">Everything is an Expression (at least, as much as possible)</b>
|
979
|
+
You might have noticed how even though we don't add return statements
|
980
|
+
to CoffeeScript functions, they nonetheless return their final value.
|
981
|
+
The CoffeeScript compiler tries to make sure that all statements in the
|
982
|
+
language can be used as expressions. Watch how the <tt>return</tt> gets
|
983
|
+
pushed down into each possible branch of execution, in the function
|
984
|
+
below.
|
985
|
+
</p>
|
986
|
+
<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>
|
987
|
+
<span class="Keyword">if</span> student.excellent_work
|
988
|
+
<span class="String"><span class="String">"</span>A+<span class="String">"</span></span>
|
989
|
+
<span class="Keyword">else</span> <span class="Keyword">if</span> student.okay_stuff
|
990
|
+
<span class="Keyword">if</span> student.tried_hard <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>
|
991
|
+
<span class="Keyword">else</span>
|
992
|
+
<span class="String"><span class="String">"</span>C<span class="String">"</span></span>
|
993
|
+
|
994
|
+
<span class="FunctionName">eldest</span><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>
|
995
|
+
</pre><pre class="idle"><span class="Storage">var</span> eldest, grade;
|
996
|
+
grade <span class="Keyword">=</span> <span class="Storage">function</span> <span class="FunctionName">grade</span>(<span class="FunctionArgument">student</span>) {
|
997
|
+
<span class="Keyword">if</span> (student.excellent_work) {
|
998
|
+
<span class="Keyword">return</span> <span class="String"><span class="String">"</span>A+<span class="String">"</span></span>;
|
999
|
+
} <span class="Keyword">else</span> <span class="Keyword">if</span> (student.okay_stuff) {
|
1000
|
+
<span class="Keyword">return</span> student.tried_hard ? <span class="String"><span class="String">"</span>B<span class="String">"</span></span> : <span class="String"><span class="String">"</span>B-<span class="String">"</span></span>;
|
1001
|
+
} <span class="Keyword">else</span> {
|
1002
|
+
<span class="Keyword">return</span> <span class="String"><span class="String">"</span>C<span class="String">"</span></span>;
|
1003
|
+
}
|
1004
|
+
};
|
1005
|
+
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>;
|
1006
|
+
</pre><button onclick='javascript: var eldest, grade;
|
1007
|
+
grade = function grade(student) {
|
1008
|
+
if (student.excellent_work) {
|
1009
|
+
return "A+";
|
1010
|
+
} else if (student.okay_stuff) {
|
1011
|
+
return student.tried_hard ? "B" : "B-";
|
1012
|
+
} else {
|
1013
|
+
return "C";
|
1014
|
+
}
|
1015
|
+
};
|
1016
|
+
eldest = 24 > 21 ? "Liz" : "Ike";
|
1017
|
+
;alert(eldest);'>run: eldest</button><br class='clear' /></div>
|
1018
|
+
<p>
|
1019
|
+
Even though functions will always return their final value, it's both possible
|
1020
|
+
and encouraged to return early from a function body writing out the explicit
|
1021
|
+
return (<tt>return value</tt>), when you know that you're done.
|
1022
|
+
</p>
|
1023
|
+
<p>
|
1024
|
+
Because variable declarations occur at the top of scope, assignment can
|
1025
|
+
be used within expressions, even for variables that haven't been seen before:
|
1026
|
+
</p>
|
1027
|
+
<div class='code'><pre class="idle"><span class="FunctionName">six</span><span class="Keyword">:</span> (<span class="FunctionName">one</span><span class="Keyword">:</span> <span class="Number">1</span>) <span class="Keyword">+</span> (<span class="FunctionName">two</span><span class="Keyword">:</span> <span class="Number">2</span>) <span class="Keyword">+</span> (<span class="FunctionName">three</span><span class="Keyword">:</span> <span class="Number">3</span>)
|
1028
|
+
</pre><pre class="idle"><span class="Storage">var</span> one, six, three, two;
|
1029
|
+
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>));
|
1030
|
+
</pre><button onclick='javascript: var one, six, three, two;
|
1031
|
+
six = ((one = 1)) + ((two = 2)) + ((three = 3));
|
1032
|
+
;alert(six);'>run: six</button><br class='clear' /></div>
|
1033
|
+
<p>
|
1034
|
+
Things that would otherwise be statements in JavaScript, when used
|
1035
|
+
as part of an expression in CoffeeScript, are converted into expressions
|
1036
|
+
by wrapping them in a closure. This lets you do useful things, like assign
|
1037
|
+
the result of a comprehension to a variable:
|
1038
|
+
</p>
|
1039
|
+
<div class='code'><pre class="idle"><span class="Comment"><span class="Comment">#</span> The first ten global properties.</span>
|
1040
|
+
|
1041
|
+
<span class="FunctionName">globals</span><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>]
|
1042
|
+
</pre><pre class="idle"><span class="Storage">var</span> _a, _b, globals, name;
|
1043
|
+
<span class="Storage">var</span> __hasProp <span class="Keyword">=</span> <span class="LibraryClassType">Object</span>.<span class="LibraryConstant">prototype</span>.hasOwnProperty;
|
1044
|
+
<span class="Comment"><span class="Comment">//</span> The first ten global properties.</span>
|
1045
|
+
globals <span class="Keyword">=</span> (<span class="Storage">function</span>() {
|
1046
|
+
_a <span class="Keyword">=</span> []; _b <span class="Keyword">=</span> <span class="LibraryClassType">window</span>;
|
1047
|
+
<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)) {
|
1048
|
+
_a.<span class="LibraryFunction">push</span>(name);
|
1049
|
+
}}
|
1050
|
+
<span class="Keyword">return</span> _a;
|
1051
|
+
}).<span class="LibraryFunction">call</span>(<span class="Variable">this</span>).<span class="LibraryFunction">slice</span>(<span class="Number">0</span>, <span class="Number">10</span>);
|
1052
|
+
</pre><button onclick='javascript: var _a, _b, globals, name;
|
1053
|
+
var __hasProp = Object.prototype.hasOwnProperty;
|
1054
|
+
// The first ten global properties.
|
1055
|
+
globals = (function() {
|
1056
|
+
_a = []; _b = window;
|
1057
|
+
for (name in _b) { if (__hasProp.call(_b, name)) {
|
1058
|
+
_a.push(name);
|
1059
|
+
}}
|
1060
|
+
return _a;
|
1061
|
+
}).call(this).slice(0, 10);
|
1062
|
+
;alert(globals);'>run: globals</button><br class='clear' /></div>
|
1063
|
+
<p>
|
1064
|
+
As well as silly things, like passing a <b>try/catch</b> statement directly
|
1065
|
+
into a function call:
|
1066
|
+
</p>
|
1067
|
+
<div class='code'><pre class="idle">alert(
|
1068
|
+
<span class="Keyword">try</span>
|
1069
|
+
nonexistent <span class="Keyword">/</span> <span class="BuiltInConstant">undefined</span>
|
1070
|
+
<span class="Keyword">catch</span> error
|
1071
|
+
<span class="String"><span class="String">"</span>And the error is ... <span class="String">"</span></span> <span class="Keyword">+</span> error
|
1072
|
+
)
|
1073
|
+
</pre><pre class="idle"><span class="LibraryFunction">alert</span>((<span class="Storage">function</span>() {
|
1074
|
+
<span class="Keyword">try</span> {
|
1075
|
+
<span class="Keyword">return</span> nonexistent / undefined;
|
1076
|
+
} <span class="Keyword">catch</span> (error) {
|
1077
|
+
<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;
|
1078
|
+
}
|
1079
|
+
}).<span class="LibraryFunction">call</span>(<span class="Variable">this</span>));
|
1080
|
+
</pre><button onclick='javascript: alert((function() {
|
1081
|
+
try {
|
1082
|
+
return nonexistent / undefined;
|
1083
|
+
} catch (error) {
|
1084
|
+
return "And the error is ... " + error;
|
1085
|
+
}
|
1086
|
+
}).call(this));
|
1087
|
+
;'>run</button><br class='clear' /></div>
|
1088
|
+
<p>
|
1089
|
+
There are a handful of statements in JavaScript that can't be meaningfully
|
1090
|
+
converted into expressions, namely <tt>break</tt>, <tt>continue</tt>,
|
1091
|
+
and <tt>return</tt>. If you make use of them within a block of code,
|
1092
|
+
CoffeeScript won't try to perform the conversion.
|
1093
|
+
</p>
|
1094
|
+
|
1095
|
+
<p>
|
1096
|
+
<span id="existence" class="bookmark"></span>
|
1097
|
+
<b class="header">The Existential Operator</b>
|
1098
|
+
It's a little difficult to check for the existence of a variable in
|
1099
|
+
JavaScript. <tt>if (variable) ...</tt> comes close, but fails for zero,
|
1100
|
+
the empty string, and false. CoffeeScript's existential operator <tt>?</tt> returns true unless
|
1101
|
+
a variable is <b>null</b> or <b>undefined</b>, which makes it analogous
|
1102
|
+
to Ruby's <tt>nil?</tt>
|
1103
|
+
</p>
|
1104
|
+
<p>
|
1105
|
+
It can also be used for safer conditional assignment than <tt>||=</tt>
|
1106
|
+
provides, for cases where you may be handling numbers or strings.
|
1107
|
+
</p>
|
1108
|
+
<div class='code'><pre class="idle"><span class="FunctionName">solipsism</span><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>
|
1109
|
+
|
1110
|
+
speed <span class="Keyword">?</span><span class="Keyword">=</span> <span class="Number">140</span>
|
1111
|
+
|
1112
|
+
|
1113
|
+
|
1114
|
+
|
1115
|
+
|
1116
|
+
</pre><pre class="idle"><span class="Storage">var</span> solipsism, speed;
|
1117
|
+
<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>)) {
|
1118
|
+
solipsism <span class="Keyword">=</span> <span class="BuiltInConstant">true</span>;
|
1119
|
+
}
|
1120
|
+
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>;
|
1121
|
+
</pre><button onclick='javascript: var solipsism, speed;
|
1122
|
+
if ((typeof mind !== "undefined" && mind !== null) && !(typeof world !== "undefined" && world !== null)) {
|
1123
|
+
solipsism = true;
|
1124
|
+
}
|
1125
|
+
speed = (typeof speed !== "undefined" && speed !== null) ? speed : 140;
|
1126
|
+
;alert(speed);'>run: speed</button><br class='clear' /></div>
|
1127
|
+
<p>
|
1128
|
+
The accessor variant of the existential operator <tt>?.</tt> can be used to soak
|
1129
|
+
up null references in a chain of properties. Use it instead
|
1130
|
+
of the dot accessor <tt>.</tt> in cases where the base value may be <b>null</b>
|
1131
|
+
or <b>undefined</b>. If all of the properties exist then you'll get the expected
|
1132
|
+
result, if the chain is broken, <b>undefined</b> is returned instead of
|
1133
|
+
the <b>TypeError</b> that would be raised otherwise.
|
1134
|
+
</p>
|
1135
|
+
<div class='code'><pre class="idle">lottery.draw_winner()<span class="Keyword">?</span>.address<span class="Keyword">?</span>.zipcode
|
1136
|
+
</pre><pre class="idle"><span class="Storage">var</span> _a;
|
1137
|
+
(_a <span class="Keyword">=</span> lottery.draw_winner()) <span class="Keyword">==</span> undefined ? undefined : _a.address <span class="Keyword">==</span> undefined ? undefined : _a.address.zipcode;
|
1138
|
+
</pre><br class='clear' /></div>
|
1139
|
+
<p>
|
1140
|
+
Soaking up nulls is similar to Ruby's
|
1141
|
+
<a href="http://andand.rubyforge.org/">andand gem</a>, and to the
|
1142
|
+
<a href="http://groovy.codehaus.org/Operators#Operators-SafeNavigationOperator%28%3F.%29">safe navigation operator</a>
|
1143
|
+
in Groovy.
|
1144
|
+
</p>
|
1145
|
+
|
1146
|
+
<p>
|
1147
|
+
<span id="inheritance" class="bookmark"></span>
|
1148
|
+
<b class="header">Inheritance, and Calling Super from a Subclass</b>
|
1149
|
+
JavaScript's prototypal inheritance has always been a bit of a
|
1150
|
+
brain-bender, with a whole family tree of libraries that provide a cleaner
|
1151
|
+
syntax for classical inheritance on top of JavaScript's prototypes:
|
1152
|
+
<a href="http://code.google.com/p/base2/">Base2</a>,
|
1153
|
+
<a href="http://prototypejs.org/">Prototype.js</a>,
|
1154
|
+
<a href="http://jsclass.jcoglan.com/">JS.Class</a>, etc.
|
1155
|
+
The libraries provide syntactic sugar, but the built-in inheritance would
|
1156
|
+
be completely usable if it weren't for a couple of small exceptions:
|
1157
|
+
it's awkward to call <b>super</b> (the prototype object's
|
1158
|
+
implementation of the current function), and it's awkward to correctly
|
1159
|
+
set the prototype chain.
|
1160
|
+
</p>
|
1161
|
+
<p>
|
1162
|
+
CoffeeScript provides <tt>extends</tt>
|
1163
|
+
to help with prototype setup, <tt>::</tt> for quick access to an
|
1164
|
+
object's prototype, and converts <tt>super()</tt> into a call against
|
1165
|
+
the immediate ancestor's method of the same name.
|
1166
|
+
</p>
|
1167
|
+
<div class='code'><pre class="idle"><span class="FunctionName">Animal</span><span class="Keyword">:</span> <span class="Storage">-></span>
|
1168
|
+
|
1169
|
+
<span class="FunctionName">Animal::move</span><span class="Keyword">:</span> <span class="FunctionArgument">(</span><span class="FunctionArgument">meters</span><span class="FunctionArgument">)</span> <span class="Storage">-></span>
|
1170
|
+
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>
|
1171
|
+
|
1172
|
+
<span class="FunctionName">Snake</span><span class="Keyword">:</span> <span class="FunctionArgument">(</span><span class="FunctionArgument">name</span><span class="FunctionArgument">)</span> <span class="Storage">-></span>
|
1173
|
+
<span class="Variable">@name</span><span class="Keyword">:</span> name
|
1174
|
+
<span class="Variable">this</span>
|
1175
|
+
|
1176
|
+
Snake <span class="Variable">extends</span> Animal
|
1177
|
+
|
1178
|
+
<span class="FunctionName">Snake::move</span><span class="Keyword">:</span> <span class="Storage">-></span>
|
1179
|
+
alert <span class="String"><span class="String">"</span>Slithering...<span class="String">"</span></span>
|
1180
|
+
<span class="Variable">super</span> <span class="Number">5</span>
|
1181
|
+
|
1182
|
+
<span class="FunctionName">Horse</span><span class="Keyword">:</span> <span class="FunctionArgument">(</span><span class="FunctionArgument">name</span><span class="FunctionArgument">)</span> <span class="Storage">-></span>
|
1183
|
+
<span class="Variable">@name</span><span class="Keyword">:</span> name
|
1184
|
+
<span class="Variable">this</span>
|
1185
|
+
|
1186
|
+
Horse <span class="Variable">extends</span> Animal
|
1187
|
+
|
1188
|
+
<span class="FunctionName">Horse::move</span><span class="Keyword">:</span> <span class="Storage">-></span>
|
1189
|
+
alert <span class="String"><span class="String">"</span>Galloping...<span class="String">"</span></span>
|
1190
|
+
<span class="Variable">super</span> <span class="Number">45</span>
|
1191
|
+
|
1192
|
+
<span class="FunctionName">sam</span><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>
|
1193
|
+
<span class="FunctionName">tom</span><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>
|
1194
|
+
|
1195
|
+
sam.move()
|
1196
|
+
tom.move()
|
1197
|
+
|
1198
|
+
|
1199
|
+
|
1200
|
+
|
1201
|
+
</pre><pre class="idle"><span class="Storage">var</span> Animal, Horse, Snake, _a, _b, sam, tom;
|
1202
|
+
Animal <span class="Keyword">=</span> <span class="Storage">function</span> <span class="FunctionName">Animal</span>() { };
|
1203
|
+
<span class="LibraryClassType">Animal</span>.<span class="LibraryConstant">prototype</span>.<span class="FunctionName">move</span> = <span class="Storage">function</span> <span class="FunctionName">move</span>(<span class="FunctionArgument">meters</span>) {
|
1204
|
+
<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>);
|
1205
|
+
};
|
1206
|
+
Snake <span class="Keyword">=</span> <span class="Storage">function</span> <span class="FunctionName">Snake</span>(<span class="FunctionArgument">name</span>) {
|
1207
|
+
<span class="Variable">this</span>.<span class="LibraryConstant">name</span> <span class="Keyword">=</span> name;
|
1208
|
+
<span class="Keyword">return</span> <span class="Variable">this</span>;
|
1209
|
+
};
|
1210
|
+
<span class="FunctionName">_a</span> = <span class="Storage">function</span>(){};
|
1211
|
+
<span class="LibraryClassType">_a</span>.<span class="LibraryConstant">prototype</span> = Animal.<span class="LibraryConstant">prototype</span>;
|
1212
|
+
Snake.__superClass__ <span class="Keyword">=</span> Animal.<span class="LibraryConstant">prototype</span>;
|
1213
|
+
<span class="LibraryClassType">Snake</span>.<span class="LibraryConstant">prototype</span> = <span class="Keyword">new</span> <span class="TypeName">_a</span>();
|
1214
|
+
<span class="LibraryClassType">Snake</span>.<span class="LibraryConstant">prototype</span>.<span class="FunctionName">constructor</span> = Snake;
|
1215
|
+
<span class="LibraryClassType">Snake</span>.<span class="LibraryConstant">prototype</span>.<span class="FunctionName">move</span> = <span class="Storage">function</span> <span class="FunctionName">move</span>() {
|
1216
|
+
<span class="LibraryFunction">alert</span>(<span class="String"><span class="String">"</span>Slithering...<span class="String">"</span></span>);
|
1217
|
+
<span class="Keyword">return</span> Snake.__superClass__.move.<span class="LibraryFunction">call</span>(<span class="Variable">this</span>, <span class="Number">5</span>);
|
1218
|
+
};
|
1219
|
+
Horse <span class="Keyword">=</span> <span class="Storage">function</span> <span class="FunctionName">Horse</span>(<span class="FunctionArgument">name</span>) {
|
1220
|
+
<span class="Variable">this</span>.<span class="LibraryConstant">name</span> <span class="Keyword">=</span> name;
|
1221
|
+
<span class="Keyword">return</span> <span class="Variable">this</span>;
|
1222
|
+
};
|
1223
|
+
<span class="FunctionName">_b</span> = <span class="Storage">function</span>(){};
|
1224
|
+
<span class="LibraryClassType">_b</span>.<span class="LibraryConstant">prototype</span> = Animal.<span class="LibraryConstant">prototype</span>;
|
1225
|
+
Horse.__superClass__ <span class="Keyword">=</span> Animal.<span class="LibraryConstant">prototype</span>;
|
1226
|
+
<span class="LibraryClassType">Horse</span>.<span class="LibraryConstant">prototype</span> = <span class="Keyword">new</span> <span class="TypeName">_b</span>();
|
1227
|
+
<span class="LibraryClassType">Horse</span>.<span class="LibraryConstant">prototype</span>.<span class="FunctionName">constructor</span> = Horse;
|
1228
|
+
<span class="LibraryClassType">Horse</span>.<span class="LibraryConstant">prototype</span>.<span class="FunctionName">move</span> = <span class="Storage">function</span> <span class="FunctionName">move</span>() {
|
1229
|
+
<span class="LibraryFunction">alert</span>(<span class="String"><span class="String">"</span>Galloping...<span class="String">"</span></span>);
|
1230
|
+
<span class="Keyword">return</span> Horse.__superClass__.move.<span class="LibraryFunction">call</span>(<span class="Variable">this</span>, <span class="Number">45</span>);
|
1231
|
+
};
|
1232
|
+
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>);
|
1233
|
+
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>);
|
1234
|
+
sam.move();
|
1235
|
+
tom.move();
|
1236
|
+
</pre><button onclick='javascript: var Animal, Horse, Snake, _a, _b, sam, tom;
|
1237
|
+
Animal = function Animal() { };
|
1238
|
+
Animal.prototype.move = function move(meters) {
|
1239
|
+
return alert(this.name + " moved " + meters + "m.");
|
1240
|
+
};
|
1241
|
+
Snake = function Snake(name) {
|
1242
|
+
this.name = name;
|
1243
|
+
return this;
|
1244
|
+
};
|
1245
|
+
_a = function(){};
|
1246
|
+
_a.prototype = Animal.prototype;
|
1247
|
+
Snake.__superClass__ = Animal.prototype;
|
1248
|
+
Snake.prototype = new _a();
|
1249
|
+
Snake.prototype.constructor = Snake;
|
1250
|
+
Snake.prototype.move = function move() {
|
1251
|
+
alert("Slithering...");
|
1252
|
+
return Snake.__superClass__.move.call(this, 5);
|
1253
|
+
};
|
1254
|
+
Horse = function Horse(name) {
|
1255
|
+
this.name = name;
|
1256
|
+
return this;
|
1257
|
+
};
|
1258
|
+
_b = function(){};
|
1259
|
+
_b.prototype = Animal.prototype;
|
1260
|
+
Horse.__superClass__ = Animal.prototype;
|
1261
|
+
Horse.prototype = new _b();
|
1262
|
+
Horse.prototype.constructor = Horse;
|
1263
|
+
Horse.prototype.move = function move() {
|
1264
|
+
alert("Galloping...");
|
1265
|
+
return Horse.__superClass__.move.call(this, 45);
|
1266
|
+
};
|
1267
|
+
sam = new Snake("Sammy the Python");
|
1268
|
+
tom = new Horse("Tommy the Palomino");
|
1269
|
+
sam.move();
|
1270
|
+
tom.move();
|
1271
|
+
;'>run</button><br class='clear' /></div>
|
1272
|
+
|
1273
|
+
<p>
|
1274
|
+
<span id="pattern_matching" class="bookmark"></span>
|
1275
|
+
<b class="header">Pattern Matching (Destructuring Assignment)</b>
|
1276
|
+
To make extracting values from complex arrays and objects more convenient,
|
1277
|
+
CoffeeScript implements ECMAScript Harmony's proposed
|
1278
|
+
<a href="http://wiki.ecmascript.org/doku.php?id=harmony:destructuring">destructuring assignment</a>
|
1279
|
+
syntax. When you assign an array or object literal to a value, CoffeeScript
|
1280
|
+
breaks up and matches both sides against each other, assigning the values
|
1281
|
+
on the right to the variables on the left. In the simplest case, it can be
|
1282
|
+
used for parallel assignment:
|
1283
|
+
</p>
|
1284
|
+
<div class='code'><pre class="idle"><span class="FunctionName">bait</span><span class="Keyword">:</span> <span class="Number">1000</span>
|
1285
|
+
<span class="FunctionName">and_switch</span><span class="Keyword">:</span> <span class="Number">0</span>
|
1286
|
+
|
1287
|
+
[bait, and_switch]<span class="Keyword">:</span> [and_switch, bait]
|
1288
|
+
</pre><pre class="idle"><span class="Storage">var</span> _a, and_switch, bait;
|
1289
|
+
bait <span class="Keyword">=</span> <span class="Number">1000</span>;
|
1290
|
+
and_switch <span class="Keyword">=</span> <span class="Number">0</span>;
|
1291
|
+
_a <span class="Keyword">=</span> [and_switch, bait];
|
1292
|
+
bait <span class="Keyword">=</span> _a[<span class="Number">0</span>];
|
1293
|
+
and_switch <span class="Keyword">=</span> _a[<span class="Number">1</span>];
|
1294
|
+
</pre><button onclick='javascript: var _a, and_switch, bait;
|
1295
|
+
bait = 1000;
|
1296
|
+
and_switch = 0;
|
1297
|
+
_a = [and_switch, bait];
|
1298
|
+
bait = _a[0];
|
1299
|
+
and_switch = _a[1];
|
1300
|
+
;alert(bait);'>run: bait</button><br class='clear' /></div>
|
1301
|
+
<p>
|
1302
|
+
But it's also helpful for dealing with functions that return multiple
|
1303
|
+
values.
|
1304
|
+
</p>
|
1305
|
+
<div class='code'><pre class="idle"><span class="FunctionName">weather_report</span><span class="Keyword">:</span> <span class="FunctionArgument">(</span><span class="FunctionArgument">location</span><span class="FunctionArgument">)</span> <span class="Storage">-></span>
|
1306
|
+
<span class="Comment"><span class="Comment">#</span> Make an Ajax request to fetch the weather...</span>
|
1307
|
+
[location, <span class="Number">72</span>, <span class="String"><span class="String">"</span>Mostly Sunny<span class="String">"</span></span>]
|
1308
|
+
|
1309
|
+
[city, temp, forecast]<span class="Keyword">:</span> weather_report <span class="String"><span class="String">"</span>Berkeley, CA<span class="String">"</span></span>
|
1310
|
+
</pre><pre class="idle"><span class="Storage">var</span> _a, city, forecast, temp, weather_report;
|
1311
|
+
weather_report <span class="Keyword">=</span> <span class="Storage">function</span> <span class="FunctionName">weather_report</span>(<span class="FunctionArgument">location</span>) {
|
1312
|
+
<span class="Comment"><span class="Comment">//</span> Make an Ajax request to fetch the weather...</span>
|
1313
|
+
<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>];
|
1314
|
+
};
|
1315
|
+
_a <span class="Keyword">=</span> weather_report(<span class="String"><span class="String">"</span>Berkeley, CA<span class="String">"</span></span>);
|
1316
|
+
city <span class="Keyword">=</span> _a[<span class="Number">0</span>];
|
1317
|
+
temp <span class="Keyword">=</span> _a[<span class="Number">1</span>];
|
1318
|
+
forecast <span class="Keyword">=</span> _a[<span class="Number">2</span>];
|
1319
|
+
</pre><button onclick='javascript: var _a, city, forecast, temp, weather_report;
|
1320
|
+
weather_report = function weather_report(location) {
|
1321
|
+
// Make an Ajax request to fetch the weather...
|
1322
|
+
return [location, 72, "Mostly Sunny"];
|
1323
|
+
};
|
1324
|
+
_a = weather_report("Berkeley, CA");
|
1325
|
+
city = _a[0];
|
1326
|
+
temp = _a[1];
|
1327
|
+
forecast = _a[2];
|
1328
|
+
;alert(forecast);'>run: forecast</button><br class='clear' /></div>
|
1329
|
+
<p>
|
1330
|
+
Pattern matching can be used with any depth of array and object nesting,
|
1331
|
+
to help pull out deeply nested properties.
|
1332
|
+
</p>
|
1333
|
+
<div class='code'><pre class="idle"><span class="FunctionName">futurists</span><span class="Keyword">:</span> {
|
1334
|
+
<span class="FunctionName">sculptor</span><span class="Keyword">:</span> <span class="String"><span class="String">"</span>Umberto Boccioni<span class="String">"</span></span>
|
1335
|
+
<span class="FunctionName">painter</span><span class="Keyword">:</span> <span class="String"><span class="String">"</span>Vladimir Burliuk<span class="String">"</span></span>
|
1336
|
+
<span class="FunctionName">poet</span><span class="Keyword">:</span> {
|
1337
|
+
<span class="FunctionName">name</span><span class="Keyword">:</span> <span class="String"><span class="String">"</span>F.T. Marinetti<span class="String">"</span></span>
|
1338
|
+
<span class="FunctionName">address</span><span class="Keyword">:</span> [
|
1339
|
+
<span class="String"><span class="String">"</span>Via Roma 42R<span class="String">"</span></span>
|
1340
|
+
<span class="String"><span class="String">"</span>Bellagio, Italy 22021<span class="String">"</span></span>
|
1341
|
+
]
|
1342
|
+
}
|
1343
|
+
}
|
1344
|
+
|
1345
|
+
{<span class="FunctionName">poet</span><span class="Keyword">:</span> {<span class="FunctionName">name</span><span class="Keyword">:</span> poet, <span class="FunctionName">address</span><span class="Keyword">:</span> [street, city]}}<span class="Keyword">:</span> futurists
|
1346
|
+
</pre><pre class="idle"><span class="Storage">var</span> _a, _b, _c, city, futurists, poet, street;
|
1347
|
+
futurists <span class="Keyword">=</span> {
|
1348
|
+
sculptor: <span class="String"><span class="String">"</span>Umberto Boccioni<span class="String">"</span></span>,
|
1349
|
+
painter: <span class="String"><span class="String">"</span>Vladimir Burliuk<span class="String">"</span></span>,
|
1350
|
+
poet: {
|
1351
|
+
name: <span class="String"><span class="String">"</span>F.T. Marinetti<span class="String">"</span></span>,
|
1352
|
+
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>]
|
1353
|
+
}
|
1354
|
+
};
|
1355
|
+
_a <span class="Keyword">=</span> futurists;
|
1356
|
+
_b <span class="Keyword">=</span> _a.poet;
|
1357
|
+
poet <span class="Keyword">=</span> _b.<span class="LibraryConstant">name</span>;
|
1358
|
+
_c <span class="Keyword">=</span> _b.address;
|
1359
|
+
street <span class="Keyword">=</span> _c[<span class="Number">0</span>];
|
1360
|
+
city <span class="Keyword">=</span> _c[<span class="Number">1</span>];
|
1361
|
+
</pre><button onclick='javascript: var _a, _b, _c, city, futurists, poet, street;
|
1362
|
+
futurists = {
|
1363
|
+
sculptor: "Umberto Boccioni",
|
1364
|
+
painter: "Vladimir Burliuk",
|
1365
|
+
poet: {
|
1366
|
+
name: "F.T. Marinetti",
|
1367
|
+
address: ["Via Roma 42R", "Bellagio, Italy 22021"]
|
1368
|
+
}
|
1369
|
+
};
|
1370
|
+
_a = futurists;
|
1371
|
+
_b = _a.poet;
|
1372
|
+
poet = _b.name;
|
1373
|
+
_c = _b.address;
|
1374
|
+
street = _c[0];
|
1375
|
+
city = _c[1];
|
1376
|
+
;alert(poet + " — " + street);'>run: poet + " — " + street</button><br class='clear' /></div>
|
1377
|
+
|
1378
|
+
<p>
|
1379
|
+
<span id="fat_arrow" class="bookmark"></span>
|
1380
|
+
<b class="header">Function binding</b>
|
1381
|
+
The fat arrow <tt>=></tt> can be used to both define a function, and to bind
|
1382
|
+
it to the current value of <tt>this</tt>, right on the spot. This is helpful
|
1383
|
+
when using callback-based libraries like Prototype or jQuery, for creating
|
1384
|
+
iterator functions to pass to <tt>each</tt>, or event-handler functions
|
1385
|
+
to use with <tt>bind</tt>. Functions created with the fat arrow are able to access
|
1386
|
+
properties of the <tt>this</tt> where they're defined.
|
1387
|
+
</p>
|
1388
|
+
<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>
|
1389
|
+
<span class="Variable">@customer</span><span class="Keyword">:</span> customer
|
1390
|
+
<span class="Variable">@cart</span><span class="Keyword">:</span> cart
|
1391
|
+
|
1392
|
+
$(<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>
|
1393
|
+
<span class="Variable">@customer</span>.purchase <span class="Variable">@cart</span>
|
1394
|
+
</pre><pre class="idle"><span class="Storage">var</span> Account;
|
1395
|
+
Account <span class="Keyword">=</span> <span class="Storage">function</span> <span class="FunctionName">Account</span>(<span class="FunctionArgument">customer, cart</span>) {
|
1396
|
+
<span class="Variable">this</span>.customer <span class="Keyword">=</span> customer;
|
1397
|
+
<span class="Variable">this</span>.cart <span class="Keyword">=</span> cart;
|
1398
|
+
<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) {
|
1399
|
+
<span class="Storage">var</span> <span class="FunctionName">__func</span> = <span class="Storage">function</span>(<span class="FunctionArgument">event</span>) {
|
1400
|
+
<span class="Keyword">return</span> <span class="Variable">this</span>.customer.purchase(<span class="Variable">this</span>.cart);
|
1401
|
+
};
|
1402
|
+
<span class="Keyword">return</span> (<span class="Storage">function</span>() {
|
1403
|
+
<span class="Keyword">return</span> __func.<span class="LibraryFunction">apply</span>(__this, arguments);
|
1404
|
+
});
|
1405
|
+
})(<span class="Variable">this</span>));
|
1406
|
+
};
|
1407
|
+
</pre><br class='clear' /></div>
|
1408
|
+
|
1409
|
+
<p>
|
1410
|
+
<span id="embedded" class="bookmark"></span>
|
1411
|
+
<b class="header">Embedded JavaScript</b>
|
1412
|
+
Hopefully, you'll never need to use it, but if you ever need to intersperse
|
1413
|
+
snippets of JavaScript within your CoffeeScript, you can
|
1414
|
+
use backticks to pass it straight through.
|
1415
|
+
</p>
|
1416
|
+
<div class='code'><pre class="idle"><span class="FunctionName">hi</span><span class="Keyword">:</span> <span class="String"><span class="String">`</span>function() {</span>
|
1417
|
+
<span class="String"> return [document.title, "Hello JavaScript"].join(": ");</span>
|
1418
|
+
<span class="String">}<span class="String">`</span></span>
|
1419
|
+
|
1420
|
+
|
1421
|
+
</pre><pre class="idle"><span class="Storage">var</span> hi;
|
1422
|
+
<span class="FunctionName">hi</span> = <span class="Storage">function</span>() {
|
1423
|
+
<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>);
|
1424
|
+
};
|
1425
|
+
</pre><button onclick='javascript: var hi;
|
1426
|
+
hi = function() {
|
1427
|
+
return [document.title, "Hello JavaScript"].join(": ");
|
1428
|
+
};
|
1429
|
+
;alert(hi());'>run: hi()</button><br class='clear' /></div>
|
1430
|
+
|
1431
|
+
<p>
|
1432
|
+
<span id="switch" class="bookmark"></span>
|
1433
|
+
<b class="header">Switch/When/Else</b>
|
1434
|
+
<b>Switch</b> statements in JavaScript are rather broken. You can only
|
1435
|
+
do comparisons based on string equality, and need to remember to <b>break</b> at the end of
|
1436
|
+
every <b>case</b> statement to avoid accidentally falling through to
|
1437
|
+
the default case. CoffeeScript compiles <b>switch</b> statements into JavaScript if-else chains, allowing you to
|
1438
|
+
compare any object (via <b>===</b>), preventing fall-through, and resulting
|
1439
|
+
in a returnable, assignable expression. The format is: <tt>switch</tt> condition,
|
1440
|
+
<tt>when</tt> clauses, <tt>else</tt> the default case.
|
1441
|
+
</p>
|
1442
|
+
<p>
|
1443
|
+
As in Ruby, <b>switch</b> statements in CoffeeScript can take multiple
|
1444
|
+
values for each <b>when</b> clause. If any of the values match, the clause
|
1445
|
+
runs.
|
1446
|
+
</p>
|
1447
|
+
<div class='code'><pre class="idle"><span class="Keyword">switch</span> day
|
1448
|
+
<span class="Keyword">when</span> <span class="String"><span class="String">"</span>Mon<span class="String">"</span></span> <span class="Keyword">then</span> go_to_work()
|
1449
|
+
<span class="Keyword">when</span> <span class="String"><span class="String">"</span>Tue<span class="String">"</span></span> <span class="Keyword">then</span> go_to_the_park()
|
1450
|
+
<span class="Keyword">when</span> <span class="String"><span class="String">"</span>Thu<span class="String">"</span></span> <span class="Keyword">then</span> go_ice_fishing()
|
1451
|
+
<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>
|
1452
|
+
<span class="Keyword">if</span> day <span class="Keyword">is</span> bingo_day
|
1453
|
+
go_to_bingo()
|
1454
|
+
go_dancing()
|
1455
|
+
<span class="Keyword">when</span> <span class="String"><span class="String">"</span>Sun<span class="String">"</span></span> <span class="Keyword">then</span> go_to_church()
|
1456
|
+
<span class="Keyword">else</span> go_to_work()
|
1457
|
+
</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>) {
|
1458
|
+
go_to_work();
|
1459
|
+
} <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>) {
|
1460
|
+
go_to_the_park();
|
1461
|
+
} <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>) {
|
1462
|
+
go_ice_fishing();
|
1463
|
+
} <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>) {
|
1464
|
+
<span class="Keyword">if</span> (day <span class="Keyword">===</span> bingo_day) {
|
1465
|
+
go_to_bingo();
|
1466
|
+
go_dancing();
|
1467
|
+
}
|
1468
|
+
} <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>) {
|
1469
|
+
go_to_church();
|
1470
|
+
} <span class="Keyword">else</span> {
|
1471
|
+
go_to_work();
|
1472
|
+
}
|
1473
|
+
</pre><br class='clear' /></div>
|
1474
|
+
|
1475
|
+
<p>
|
1476
|
+
<span id="try" class="bookmark"></span>
|
1477
|
+
<b class="header">Try/Catch/Finally</b>
|
1478
|
+
Try/catch statements are just about the same as JavaScript (although
|
1479
|
+
they work as expressions).
|
1480
|
+
</p>
|
1481
|
+
<div class='code'><pre class="idle"><span class="Keyword">try</span>
|
1482
|
+
all_hell_breaks_loose()
|
1483
|
+
cats_and_dogs_living_together()
|
1484
|
+
<span class="Keyword">catch</span> error
|
1485
|
+
print error
|
1486
|
+
<span class="Keyword">finally</span>
|
1487
|
+
clean_up()
|
1488
|
+
</pre><pre class="idle"><span class="Keyword">try</span> {
|
1489
|
+
all_hell_breaks_loose();
|
1490
|
+
cats_and_dogs_living_together();
|
1491
|
+
} <span class="Keyword">catch</span> (error) {
|
1492
|
+
<span class="LibraryFunction">print</span>(error);
|
1493
|
+
} <span class="Keyword">finally</span> {
|
1494
|
+
clean_up();
|
1495
|
+
}
|
1496
|
+
</pre><br class='clear' /></div>
|
1497
|
+
|
1498
|
+
<p>
|
1499
|
+
<span id="comparisons" class="bookmark"></span>
|
1500
|
+
<b class="header">Chained Comparisons</b>
|
1501
|
+
CoffeeScript borrows
|
1502
|
+
<a href="http://docs.python.org/reference/expressions.html#notin">chained comparisons</a>
|
1503
|
+
from Python — making it easy to test if a value falls within a
|
1504
|
+
certain range.
|
1505
|
+
</p>
|
1506
|
+
<div class='code'><pre class="idle"><span class="FunctionName">cholesterol</span><span class="Keyword">:</span> <span class="Number">127</span>
|
1507
|
+
|
1508
|
+
<span class="FunctionName">healthy</span><span class="Keyword">:</span> <span class="Number">200</span> <span class="Keyword">></span> cholesterol <span class="Keyword">></span> <span class="Number">60</span>
|
1509
|
+
|
1510
|
+
|
1511
|
+
</pre><pre class="idle"><span class="Storage">var</span> cholesterol, healthy;
|
1512
|
+
cholesterol <span class="Keyword">=</span> <span class="Number">127</span>;
|
1513
|
+
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>);
|
1514
|
+
</pre><button onclick='javascript: var cholesterol, healthy;
|
1515
|
+
cholesterol = 127;
|
1516
|
+
healthy = (200 > cholesterol) && (cholesterol > 60);
|
1517
|
+
;alert(healthy);'>run: healthy</button><br class='clear' /></div>
|
1518
|
+
|
1519
|
+
<p>
|
1520
|
+
<span id="strings" class="bookmark"></span>
|
1521
|
+
<b class="header">Multiline Strings and Heredocs</b>
|
1522
|
+
Multiline strings are allowed in CoffeeScript.
|
1523
|
+
</p>
|
1524
|
+
<div class='code'><pre class="idle"><span class="FunctionName">moby_dick</span><span class="Keyword">:</span> <span class="String"><span class="String">"</span>Call me Ishmael. Some years ago --</span>
|
1525
|
+
<span class="String">never mind how long precisely -- having little</span>
|
1526
|
+
<span class="String">or no money in my purse, and nothing particular</span>
|
1527
|
+
<span class="String">to interest me on shore, I thought I would sail</span>
|
1528
|
+
<span class="String">about a little and see the watery part of the</span>
|
1529
|
+
<span class="String">world...<span class="String">"</span></span>
|
1530
|
+
|
1531
|
+
|
1532
|
+
</pre><pre class="idle"><span class="Storage">var</span> moby_dick;
|
1533
|
+
moby_dick <span class="Keyword">=</span> <span class="String"><span class="String">"</span>Call me Ishmael. Some years ago -- \</span>
|
1534
|
+
<span class="String">never mind how long precisely -- having little \</span>
|
1535
|
+
<span class="String">or no money in my purse, and nothing particular \</span>
|
1536
|
+
<span class="String">to interest me on shore, I thought I would sail \</span>
|
1537
|
+
<span class="String">about a little and see the watery part of the \</span>
|
1538
|
+
<span class="String">world...<span class="String">"</span></span>;
|
1539
|
+
</pre><button onclick='javascript: var moby_dick;
|
1540
|
+
moby_dick = "Call me Ishmael. Some years ago -- \
|
1541
|
+
never mind how long precisely -- having little \
|
1542
|
+
or no money in my purse, and nothing particular \
|
1543
|
+
to interest me on shore, I thought I would sail \
|
1544
|
+
about a little and see the watery part of the \
|
1545
|
+
world...";
|
1546
|
+
;alert(moby_dick);'>run: moby_dick</button><br class='clear' /></div>
|
1547
|
+
<p>
|
1548
|
+
Heredocs can be used to hold formatted or indentation-sensitive text
|
1549
|
+
(or, if you just don't feel like escaping quotes and apostrophes). The
|
1550
|
+
indentation level that begins the heredoc is maintained throughout, so
|
1551
|
+
you can keep it all aligned with the body of your code.
|
1552
|
+
</p>
|
1553
|
+
<div class='code'><pre class="idle"><span class="FunctionName">html</span><span class="Keyword">:</span> <span class="String"><span class="String">'''</span></span>
|
1554
|
+
<span class="String"> <strong></span>
|
1555
|
+
<span class="String"> cup of coffeescript</span>
|
1556
|
+
<span class="String"> </strong></span>
|
1557
|
+
<span class="String"> <span class="String">'''</span></span>
|
1558
|
+
</pre><pre class="idle"><span class="Storage">var</span> html;
|
1559
|
+
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>;
|
1560
|
+
</pre><br class='clear' /></div>
|
1561
|
+
|
1562
|
+
<h2>
|
1563
|
+
<span id="cake" class="bookmark"></span>
|
1564
|
+
Cake, and Cakefiles
|
1565
|
+
</h2>
|
1566
|
+
|
1567
|
+
<p>
|
1568
|
+
CoffeeScript includes a simple build system similar to Make and Rake. Naturally,
|
1569
|
+
it's called Cake, and is used for the build and test tasks for the CoffeeScript
|
1570
|
+
language itself. Tasks are defined in a file named <tt>Cakefile</tt>, and
|
1571
|
+
can be invoked by running <tt>cake taskname</tt> from within the directory.
|
1572
|
+
To print a list of all the tasks, just run <tt>cake</tt>.
|
1573
|
+
</p>
|
1574
|
+
|
1575
|
+
<p>
|
1576
|
+
Task definitions are written in CoffeeScript, so you can put arbitrary code
|
1577
|
+
in your Cakefile. Define a task with a name, a long description, and the
|
1578
|
+
function to invoke when the task is run. Here's a hypothetical task
|
1579
|
+
that uses the Node.js API.
|
1580
|
+
</p>
|
1581
|
+
<div class='code'><pre class="idle">process.mixin require <span class="String"><span class="String">'</span>assert<span class="String">'</span></span>
|
1582
|
+
|
1583
|
+
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>
|
1584
|
+
<span class="Keyword">for</span> test <span class="Keyword">in</span> test_files
|
1585
|
+
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
|
1586
|
+
</pre><pre class="idle">process.mixin(require(<span class="String"><span class="String">'</span>assert<span class="String">'</span></span>));
|
1587
|
+
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>() {
|
1588
|
+
<span class="Storage">var</span> _a, _b, _c, test;
|
1589
|
+
_a <span class="Keyword">=</span> []; _b <span class="Keyword">=</span> test_files;
|
1590
|
+
<span class="Keyword">for</span> (_c <span class="Keyword">=</span> <span class="Number">0</span>; _c <span class="Keyword"><</span> _b.<span class="LibraryConstant">length</span>; _c<span class="Keyword">++</span>) {
|
1591
|
+
test <span class="Keyword">=</span> _b[_c];
|
1592
|
+
_a.<span class="LibraryFunction">push</span>(fs.readFile(test, <span class="Storage">function</span>(err, code) {
|
1593
|
+
<span class="Keyword">return</span> <span class="LibraryFunction">eval</span>(coffee.<span class="LibraryFunction">compile</span>(code));
|
1594
|
+
}));
|
1595
|
+
}
|
1596
|
+
<span class="Keyword">return</span> _a;
|
1597
|
+
});
|
1598
|
+
</pre><br class='clear' /></div>
|
1599
|
+
|
1600
|
+
<h2>
|
1601
|
+
<span id="resources" class="bookmark"></span>
|
1602
|
+
Resources
|
1603
|
+
</h2>
|
1604
|
+
|
1605
|
+
<ul>
|
1606
|
+
<li>
|
1607
|
+
<a href="http://github.com/jashkenas/coffee-script/">Source Code</a><br />
|
1608
|
+
Use <tt>bin/coffee</tt> to test your changes,<br />
|
1609
|
+
<tt>bin/cake test</tt> to run the test suite,<br />
|
1610
|
+
<tt>bin/cake build</tt> to rebuild the CoffeeScript compiler, and <br />
|
1611
|
+
<tt>bin/cake build:parser</tt> to regenerate the Jison parser if you're
|
1612
|
+
working on the grammar.
|
1613
|
+
</li>
|
1614
|
+
<li>
|
1615
|
+
<a href="http://github.com/jashkenas/coffee-script/issues">CoffeeScript Issues</a><br />
|
1616
|
+
Bugs reports, feature requests, and general discussion all belong here.
|
1617
|
+
</li>
|
1618
|
+
<li>
|
1619
|
+
If you'd like to chat, stop by <tt>#coffeescript</tt> on Freenode.
|
1620
|
+
</li>
|
1621
|
+
</ul>
|
1622
|
+
|
1623
|
+
<h2>
|
1624
|
+
<span id="change_log" class="bookmark"></span>
|
1625
|
+
Change Log
|
1626
|
+
</h2>
|
1627
|
+
|
1628
|
+
<p>
|
1629
|
+
<b class="header" style="margin-top: 20px;">0.5.1</b>
|
1630
|
+
Improvements to null soaking with the existential operator, including
|
1631
|
+
soaks on indexed properties. Added conditions to <tt>while</tt> loops,
|
1632
|
+
so you can use them as filters with <tt>when</tt>, in the same manner as
|
1633
|
+
comprehensions.
|
1634
|
+
</p>
|
1635
|
+
|
1636
|
+
<p>
|
1637
|
+
<b class="header" style="margin-top: 20px;">0.5.0</b>
|
1638
|
+
CoffeeScript 0.5.0 is a major release, While there are no language changes,
|
1639
|
+
the Ruby compiler has been removed in favor of a self-hosting
|
1640
|
+
compiler written in pure CoffeeScript.
|
1641
|
+
</p>
|
1642
|
+
|
1643
|
+
<p>
|
1644
|
+
<b class="header" style="margin-top: 20px;">0.3.2</b>
|
1645
|
+
<tt>@property</tt> is now a shorthand for <tt>this.property</tt>.<br />
|
1646
|
+
Switched the default JavaScript engine from Narwhal to Node.js. Pass
|
1647
|
+
the <tt>--narwhal</tt> flag if you'd like to continue using it.
|
1648
|
+
</p>
|
1649
|
+
|
1650
|
+
<p>
|
1651
|
+
<b class="header" style="margin-top: 20px;">0.3.0</b>
|
1652
|
+
CoffeeScript 0.3 includes major syntax changes:
|
1653
|
+
<br />
|
1654
|
+
The function symbol was changed to
|
1655
|
+
<tt>-></tt>, and the bound function symbol is now <tt>=></tt>.
|
1656
|
+
<br />
|
1657
|
+
Parameter lists in function definitions must now be wrapped in parentheses.
|
1658
|
+
<br />
|
1659
|
+
Added property soaking, with the <tt>?.</tt> operator.
|
1660
|
+
<br />
|
1661
|
+
Made parentheses optional, when invoking functions with arguments.
|
1662
|
+
<br />
|
1663
|
+
Removed the obsolete block literal syntax.
|
1664
|
+
</p>
|
1665
|
+
|
1666
|
+
<p>
|
1667
|
+
<b class="header" style="margin-top: 20px;">0.2.6</b>
|
1668
|
+
Added Python-style chained comparisons, the conditional existence
|
1669
|
+
operator <tt>?=</tt>, and some examples from <i>Beautiful Code</i>.
|
1670
|
+
Bugfixes relating to statement-to-expression conversion, arguments-to-array
|
1671
|
+
conversion, and the TextMate syntax highlighter.
|
1672
|
+
</p>
|
1673
|
+
|
1674
|
+
<p>
|
1675
|
+
<b class="header" style="margin-top: 20px;">0.2.5</b>
|
1676
|
+
The conditions in switch statements can now take multiple values at once —
|
1677
|
+
If any of them are true, the case will run. Added the long arrow <tt>==></tt>,
|
1678
|
+
which defines and immediately binds a function to <tt>this</tt>. While loops can
|
1679
|
+
now be used as expressions, in the same way that comprehensions can. Splats
|
1680
|
+
can be used within pattern matches to soak up the rest of an array.
|
1681
|
+
</p>
|
1682
|
+
|
1683
|
+
<p>
|
1684
|
+
<b class="header" style="margin-top: 20px;">0.2.4</b>
|
1685
|
+
Added ECMAScript Harmony style destructuring assignment, for dealing with
|
1686
|
+
extracting values from nested arrays and objects. Added indentation-sensitive
|
1687
|
+
heredocs for nicely formatted strings or chunks of code.
|
1688
|
+
</p>
|
1689
|
+
|
1690
|
+
<p>
|
1691
|
+
<b class="header" style="margin-top: 20px;">0.2.3</b>
|
1692
|
+
Axed the unsatisfactory <tt>ino</tt> keyword, replacing it with <tt>of</tt> for
|
1693
|
+
object comprehensions. They now look like: <tt>for prop, value of object</tt>.
|
1694
|
+
</p>
|
1695
|
+
|
1696
|
+
<p>
|
1697
|
+
<b class="header" style="margin-top: 20px;">0.2.2</b>
|
1698
|
+
When performing a comprehension over an object, use <tt>ino</tt>, instead
|
1699
|
+
of <tt>in</tt>, which helps us generate smaller, more efficient code at
|
1700
|
+
compile time.
|
1701
|
+
<br />
|
1702
|
+
Added <tt>::</tt> as a shorthand for saying <tt>.prototype.</tt>
|
1703
|
+
<br />
|
1704
|
+
The "splat" symbol has been changed from a prefix asterisk <tt>*</tt>, to
|
1705
|
+
a postfix ellipsis <tt>...</tt>
|
1706
|
+
<br />
|
1707
|
+
Added JavaScript's <tt>in</tt> operator,
|
1708
|
+
empty <tt>return</tt> statements, and empty <tt>while</tt> loops.
|
1709
|
+
<br />
|
1710
|
+
Constructor functions that start with capital letters now include a
|
1711
|
+
safety check to make sure that the new instance of the object is returned.
|
1712
|
+
<br />
|
1713
|
+
The <tt>extends</tt> keyword now functions identically to <tt>goog.inherits</tt>
|
1714
|
+
in Google's Closure Library.
|
1715
|
+
</p>
|
1716
|
+
|
1717
|
+
<p>
|
1718
|
+
<b class="header" style="margin-top: 20px;">0.2.1</b>
|
1719
|
+
Arguments objects are now converted into real arrays when referenced.
|
1720
|
+
</p>
|
1721
|
+
|
1722
|
+
<p>
|
1723
|
+
<b class="header" style="margin-top: 20px;">0.2.0</b>
|
1724
|
+
Major release. Significant whitespace. Better statement-to-expression
|
1725
|
+
conversion. Splats. Splice literals. Object comprehensions. Blocks.
|
1726
|
+
The existential operator. Many thanks to all the folks who posted issues,
|
1727
|
+
with special thanks to
|
1728
|
+
<a href="http://github.com/kamatsu">Liam O'Connor-Davis</a> for whitespace
|
1729
|
+
and expression help.
|
1730
|
+
</p>
|
1731
|
+
|
1732
|
+
<p>
|
1733
|
+
<b class="header" style="margin-top: 20px;">0.1.6</b>
|
1734
|
+
Bugfix for running <tt>coffee --interactive</tt> and <tt>--run</tt>
|
1735
|
+
from outside of the CoffeeScript directory. Bugfix for nested
|
1736
|
+
function/if-statements.
|
1737
|
+
</p>
|
1738
|
+
|
1739
|
+
<p>
|
1740
|
+
<b class="header" style="margin-top: 20px;">0.1.5</b>
|
1741
|
+
Array slice literals and array comprehensions can now both take Ruby-style
|
1742
|
+
ranges to specify the start and end. JavaScript variable declaration is
|
1743
|
+
now pushed up to the top of the scope, making all assignment statements into
|
1744
|
+
expressions. You can use <tt>\</tt> to escape newlines.
|
1745
|
+
The <tt>coffee-script</tt> command is now called <tt>coffee</tt>.
|
1746
|
+
</p>
|
1747
|
+
|
1748
|
+
<p>
|
1749
|
+
<b class="header" style="margin-top: 20px;">0.1.4</b>
|
1750
|
+
The official CoffeeScript extension is now <tt>.coffee</tt> instead of
|
1751
|
+
<tt>.cs</tt>, which properly belongs to
|
1752
|
+
<a href="http://en.wikipedia.org/wiki/C_Sharp_(programming_language)">C#</a>.
|
1753
|
+
Due to popular demand, you can now also use <tt>=</tt> to assign. Unlike
|
1754
|
+
JavaScript, <tt>=</tt> can also be used within object literals, interchangeably
|
1755
|
+
with <tt>:</tt>. Made a grammatical fix for chained function calls
|
1756
|
+
like <tt>func(1)(2)(3)(4)</tt>. Inheritance and super no longer use
|
1757
|
+
<tt>__proto__</tt>, so they should be IE-compatible now.
|
1758
|
+
</p>
|
1759
|
+
|
1760
|
+
<p>
|
1761
|
+
<b class="header" style="margin-top: 20px;">0.1.3</b>
|
1762
|
+
The <tt>coffee</tt> command now includes <tt>--interactive</tt>,
|
1763
|
+
which launches an interactive CoffeeScript session, and <tt>--run</tt>,
|
1764
|
+
which directly compiles and executes a script. Both options depend on a
|
1765
|
+
working installation of Narwhal.
|
1766
|
+
The <tt>aint</tt> keyword has been replaced by <tt>isnt</tt>, which goes
|
1767
|
+
together a little smoother with <tt>is</tt>.
|
1768
|
+
Quoted strings are now allowed as identifiers within object literals: eg.
|
1769
|
+
<tt>{"5+5": 10}</tt>.
|
1770
|
+
All assignment operators now use a colon: <tt>+:</tt>, <tt>-:</tt>,
|
1771
|
+
<tt>*:</tt>, etc.
|
1772
|
+
</p>
|
1773
|
+
|
1774
|
+
<p>
|
1775
|
+
<b class="header" style="margin-top: 20px;">0.1.2</b>
|
1776
|
+
Fixed a bug with calling <tt>super()</tt> through more than one level of
|
1777
|
+
inheritance, with the re-addition of the <tt>extends</tt> keyword.
|
1778
|
+
Added experimental <a href="http://narwhaljs.org/">Narwhal</a>
|
1779
|
+
support (as a Tusk package), contributed by
|
1780
|
+
<a href="http://tlrobinson.net/">Tom Robinson</a>, including
|
1781
|
+
<b>bin/cs</b> as a CoffeeScript REPL and interpreter.
|
1782
|
+
New <tt>--no-wrap</tt> option to suppress the safety function
|
1783
|
+
wrapper.
|
1784
|
+
</p>
|
1785
|
+
|
1786
|
+
<p>
|
1787
|
+
<b class="header" style="margin-top: 20px;">0.1.1</b>
|
1788
|
+
Added <tt>instanceof</tt> and <tt>typeof</tt> as operators.
|
1789
|
+
</p>
|
1790
|
+
|
1791
|
+
<p>
|
1792
|
+
<b class="header" style="margin-top: 20px;">0.1.0</b>
|
1793
|
+
Initial CoffeeScript release.
|
1794
|
+
</p>
|
1795
|
+
|
1796
|
+
</div>
|
1797
|
+
|
1798
|
+
<script type="text/javascript" src="lib/rewriter.js"></script>
|
1799
|
+
<script type="text/javascript" src="lib/lexer.js"></script>
|
1800
|
+
<script type="text/javascript" src="lib/parser.js"></script>
|
1801
|
+
<script type="text/javascript" src="lib/scope.js"></script>
|
1802
|
+
<script type="text/javascript" src="lib/nodes.js"></script>
|
1803
|
+
<script type="text/javascript" src="lib/coffee-script.js"></script>
|
1804
|
+
|
1805
|
+
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
|
1806
|
+
|
1807
|
+
<script type="text/javascript">
|
1808
|
+
window.repl_compile = function() {
|
1809
|
+
var source = $('#repl_source').val();
|
1810
|
+
window.compiled_js = '';
|
1811
|
+
try {
|
1812
|
+
window.compiled_js = CoffeeScript.compile(source, {no_wrap: true});
|
1813
|
+
} catch(error) {
|
1814
|
+
alert(error);
|
1815
|
+
}
|
1816
|
+
$('#repl_results').html(window.compiled_js);
|
1817
|
+
};
|
1818
|
+
window.repl_run = function() {
|
1819
|
+
try {
|
1820
|
+
eval(window.compiled_js);
|
1821
|
+
} catch(error) {
|
1822
|
+
alert(error);
|
1823
|
+
}
|
1824
|
+
};
|
1825
|
+
|
1826
|
+
var nav = $('.navigation');
|
1827
|
+
var currentNav = null;
|
1828
|
+
var closeMenus = function() {
|
1829
|
+
if (currentNav) currentNav.removeClass('active');
|
1830
|
+
currentNav = null;
|
1831
|
+
};
|
1832
|
+
nav.click(function(e) {
|
1833
|
+
if (e.target.tagName.toLowerCase() == 'a') return;
|
1834
|
+
if (this !== (currentNav && currentNav[0])) {
|
1835
|
+
closeMenus();
|
1836
|
+
currentNav = $(this);
|
1837
|
+
currentNav.addClass('active');
|
1838
|
+
}
|
1839
|
+
return false;
|
1840
|
+
});
|
1841
|
+
$(document.body).click(function() {
|
1842
|
+
closeMenus();
|
1843
|
+
});
|
1844
|
+
</script>
|
1845
|
+
|
1846
|
+
</body>
|
1847
|
+
</html>
|