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,967 @@
|
|
1
|
+
<%
|
2
|
+
require 'uv'
|
3
|
+
def code_for(file, executable=false)
|
4
|
+
@stripper ||= /(\A\(function\(\)\{\n|\}\)\(\);\Z|^ )/
|
5
|
+
return '' unless File.exists?("documentation/js/#{file}.js")
|
6
|
+
cs = File.read("documentation/coffee/#{file}.coffee")
|
7
|
+
js = File.read("documentation/js/#{file}.js").gsub(@stripper, '')
|
8
|
+
cshtml = Uv.parse(cs, 'xhtml', 'coffeescript', false, 'idle', false)
|
9
|
+
jshtml = Uv.parse(js, 'xhtml', 'javascript', false, 'idle', false)
|
10
|
+
append = executable == true ? '' : "alert(#{executable});"
|
11
|
+
run = executable == true ? 'run' : "run: #{executable}"
|
12
|
+
button = executable ? "<button onclick='javascript: #{js};#{append}'>#{run}</button>" : ''
|
13
|
+
"<div class='code'>#{cshtml}#{jshtml}#{button}<br class='clear' /></div>"
|
14
|
+
end
|
15
|
+
%>
|
16
|
+
|
17
|
+
|
18
|
+
<!DOCTYPE html>
|
19
|
+
<html>
|
20
|
+
<head>
|
21
|
+
<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
|
22
|
+
<title>CoffeeScript</title>
|
23
|
+
<link rel="stylesheet" type="text/css" href="documentation/css/docs.css" />
|
24
|
+
<link rel="stylesheet" type="text/css" href="documentation/css/idle.css" />
|
25
|
+
</head>
|
26
|
+
<body>
|
27
|
+
|
28
|
+
<div id="fadeout"></div>
|
29
|
+
|
30
|
+
<div id="flybar">
|
31
|
+
<a id="logo" href="#top"> </a>
|
32
|
+
<div class="navigation">
|
33
|
+
<div class="button">
|
34
|
+
Table of Contents
|
35
|
+
</div>
|
36
|
+
<div class="contents">
|
37
|
+
<a href="#overview">Mini Overview</a>
|
38
|
+
<a href="#installation">Installation and Usage</a>
|
39
|
+
<a href="#language">Language Reference</a>
|
40
|
+
<a href="#whitespace">Significant Whitespace</a>
|
41
|
+
<a href="#functions">Functions and Invocation</a>
|
42
|
+
<a href="#assignment">Assignment</a>
|
43
|
+
<a href="#objects_and_arrays">Objects and Arrays</a>
|
44
|
+
<a href="#lexical_scope">Lexical Scoping and Variable Safety</a>
|
45
|
+
<a href="#conditionals">Conditionals, Ternaries, and Conditional Assignment</a>
|
46
|
+
<a href="#aliases">Aliases</a>
|
47
|
+
<a href="#splats">Splats...</a>
|
48
|
+
<a href="#arguments">Arguments are Arrays</a>
|
49
|
+
<a href="#while">While Loops</a>
|
50
|
+
<a href="#comprehensions">Comprehensions (Arrays, Objects, and Ranges)</a>
|
51
|
+
<a href="#slice_splice">Array Slicing and Splicing with Ranges</a>
|
52
|
+
<a href="#expressions">Everything is an Expression</a>
|
53
|
+
<a href="#existence">The Existential Operator</a>
|
54
|
+
<a href="#inheritance">Inheritance, and Calling Super from a Subclass</a>
|
55
|
+
<a href="#pattern_matching">Pattern Matching</a>
|
56
|
+
<a href="#fat_arrow">Function Binding</a>
|
57
|
+
<a href="#embedded">Embedded JavaScript</a>
|
58
|
+
<a href="#switch">Switch/When/Else</a>
|
59
|
+
<a href="#try">Try/Catch/Finally</a>
|
60
|
+
<a href="#comparisons">Chained Comparisons</a>
|
61
|
+
<a href="#strings">Multiline Strings and Heredocs</a>
|
62
|
+
<a href="#cake">Cake, and Cakefiles</a>
|
63
|
+
<a href="#resources">Resources</a>
|
64
|
+
<a href="#change_log">Change Log</a>
|
65
|
+
</div>
|
66
|
+
</div>
|
67
|
+
<div class="navigation try">
|
68
|
+
<div class="button">
|
69
|
+
Try CoffeeScript
|
70
|
+
</div>
|
71
|
+
<div class="contents repl_wrapper">
|
72
|
+
<div class="code">
|
73
|
+
<textarea id="repl_source">reverse: (string) ->
|
74
|
+
string.split('').reverse().join ''
|
75
|
+
|
76
|
+
alert reverse '!tpircseeffoC'</textarea>
|
77
|
+
<pre id="repl_results"></pre>
|
78
|
+
<button class="compile" onclick="javascript: repl_compile();">compile</button>
|
79
|
+
<button class="run" onclick="javascript: repl_run();">run</button>
|
80
|
+
<br class="clear" />
|
81
|
+
</div>
|
82
|
+
</div>
|
83
|
+
</div>
|
84
|
+
</div>
|
85
|
+
|
86
|
+
<div class="container">
|
87
|
+
<span class="bookmark" id="top"></span>
|
88
|
+
|
89
|
+
<p>
|
90
|
+
CoffeeScript is a little language that compiles into JavaScript. Think
|
91
|
+
of it as JavaScript's less ostentatious kid brother — the same genes,
|
92
|
+
roughly the same height, but a different sense of style. Apart from a handful of
|
93
|
+
bonus goodies, statements in CoffeeScript correspond one-to-one with their
|
94
|
+
equivalent in JavaScript, it's just another way of saying it.
|
95
|
+
</p>
|
96
|
+
|
97
|
+
<p>
|
98
|
+
<b>Disclaimer:</b>
|
99
|
+
CoffeeScript is just for fun. Until it reaches 1.0, <i>there are no guarantees
|
100
|
+
that the syntax won't change between versions.</i> That said,
|
101
|
+
it compiles into clean JavaScript (the good parts) that can use existing
|
102
|
+
JavaScript libraries seamlessly, and passes through
|
103
|
+
<a href="http://www.jslint.com/">JSLint</a> without warnings. The compiled
|
104
|
+
output is quite readable — pretty-printed, with comments
|
105
|
+
preserved intact.
|
106
|
+
</p>
|
107
|
+
|
108
|
+
<p>
|
109
|
+
<b>Latest Version:</b>
|
110
|
+
<a href="http://github.com/jashkenas/coffee-script/tarball/0.5.1">0.5.1</a>
|
111
|
+
</p>
|
112
|
+
|
113
|
+
<h2>
|
114
|
+
<span id="overview" class="bookmark"></span>
|
115
|
+
Mini Overview
|
116
|
+
</h2>
|
117
|
+
|
118
|
+
<p><i>CoffeeScript on the left, compiled JavaScript output on the right.</i></p>
|
119
|
+
|
120
|
+
<%= code_for('overview', 'cubed_list') %>
|
121
|
+
|
122
|
+
<p>
|
123
|
+
For a longer CoffeeScript example, check out
|
124
|
+
<a href="documentation/underscore.html">Underscore.coffee</a>, a port
|
125
|
+
of the <a href="http://documentcloud.github.com/underscore/">Underscore.js</a>
|
126
|
+
library of helper functions. Underscore.coffee can pass the entire Underscore.js
|
127
|
+
test suite. The CoffeeScript version is faster than the original for a number
|
128
|
+
of methods (in general, due to the speed of CoffeeScript's array comprehensions), and
|
129
|
+
after being minified and gzipped, is only 241 bytes larger than the original
|
130
|
+
JavaScript version.
|
131
|
+
Additional examples are included in the source repository, inside the
|
132
|
+
<a href="http://github.com/jashkenas/coffee-script/tree/master/examples/">examples</a> folder.
|
133
|
+
</p>
|
134
|
+
|
135
|
+
<h2>
|
136
|
+
<span id="installation" class="bookmark"></span>
|
137
|
+
Installation and Usage
|
138
|
+
</h2>
|
139
|
+
|
140
|
+
<p>
|
141
|
+
The CoffeeScript compiler is written in pure CoffeeScript, using a
|
142
|
+
<a href="http://github.com/jashkenas/coffee-script/blob/master/src/grammar.coffee">small DSL</a>
|
143
|
+
on top of the <a href="http://github.com/zaach/jison">Jison parser generator</a>, and is available
|
144
|
+
as a <a href="http://nodejs.org/">Node.js</a> utility. The core compiler however,
|
145
|
+
does not depend on Node, and can be run in other server-side-JavaScript environments,
|
146
|
+
or in the browser (see "Try CoffeeScript", above). This may be helpful,
|
147
|
+
as Node only run on flavors of nix, and not Windows, for the time being.
|
148
|
+
</p>
|
149
|
+
|
150
|
+
<p>
|
151
|
+
To install, first make sure you have a working version of
|
152
|
+
<a href="http://nodejs.org/">Node.js</a>, 0.1.30 or higher. Then clone the CoffeeScript
|
153
|
+
<a href="http://github.com/jashkenas/coffee-script">source repository</a>
|
154
|
+
from GitHub, or download the latest
|
155
|
+
release: <a href="http://github.com/jashkenas/coffee-script/tarball/0.5.1">0.5.1</a>.
|
156
|
+
To install the CoffeeScript compiler system-wide
|
157
|
+
under <tt>/usr/local</tt>, open the directory and run:
|
158
|
+
</p>
|
159
|
+
|
160
|
+
<pre>
|
161
|
+
sudo bin/cake install</pre>
|
162
|
+
|
163
|
+
<p>
|
164
|
+
This provides the <tt>coffee</tt> command, which can
|
165
|
+
be used to compile CoffeeScript <tt>.coffee</tt> files into JavaScript, as
|
166
|
+
well as debug them. The <tt>coffee</tt>
|
167
|
+
command also provides direct evaluation and an interactive REPL.
|
168
|
+
When compiling to JavaScript, <tt>coffee</tt> writes the output
|
169
|
+
as <tt>.js</tt> files in the same directory by default, but output
|
170
|
+
can be customized with the following options:
|
171
|
+
</p>
|
172
|
+
|
173
|
+
<table>
|
174
|
+
<tr>
|
175
|
+
<td width="25%"><code>-i, --interactive</code></td>
|
176
|
+
<td>
|
177
|
+
Launch an interactive CoffeeScript session to try short snippets.
|
178
|
+
</td>
|
179
|
+
</tr>
|
180
|
+
<tr>
|
181
|
+
<td><code>-r, --run</code></td>
|
182
|
+
<td>
|
183
|
+
Compile and execute a given CoffeeScript without saving the intermediate
|
184
|
+
JavaScript.
|
185
|
+
</td>
|
186
|
+
</tr>
|
187
|
+
<tr>
|
188
|
+
<td><code>-o, --output [DIR]</code></td>
|
189
|
+
<td>
|
190
|
+
Write out all compiled JavaScript files into the specified directory.
|
191
|
+
</td>
|
192
|
+
</tr>
|
193
|
+
<tr>
|
194
|
+
<td><code>-w, --watch</code></td>
|
195
|
+
<td>
|
196
|
+
Watch the modification times of the coffee-scripts, recompiling as
|
197
|
+
soon as a change occurs.
|
198
|
+
</td>
|
199
|
+
</tr>
|
200
|
+
<tr>
|
201
|
+
<td><code>-p, --print</code></td>
|
202
|
+
<td>
|
203
|
+
Instead of writing out the JavaScript as a file, print it
|
204
|
+
directly to <b>stdout</b>.
|
205
|
+
</td>
|
206
|
+
</tr>
|
207
|
+
<tr>
|
208
|
+
<td><code>-l, --lint</code></td>
|
209
|
+
<td>
|
210
|
+
If the <tt>jsl</tt>
|
211
|
+
(<a href="http://www.javascriptlint.com/">JavaScript Lint</a>)
|
212
|
+
command is installed, use it
|
213
|
+
to check the compilation of a CoffeeScript file. (Handy in
|
214
|
+
conjunction with <tt>--watch</tt>)
|
215
|
+
</td>
|
216
|
+
</tr>
|
217
|
+
<tr>
|
218
|
+
<td><code>-e, --eval</code></td>
|
219
|
+
<td>
|
220
|
+
Compile and print a little snippet of CoffeeScript directly from the
|
221
|
+
command line. For example:<br /><tt>coffee -e "square: (x) -> x * x"</tt>
|
222
|
+
</td>
|
223
|
+
</tr>
|
224
|
+
<tr>
|
225
|
+
<td><code>-n, --no-wrap</code></td>
|
226
|
+
<td>
|
227
|
+
Compile the JavaScript without the top-level function safety wrapper.
|
228
|
+
(Used for CoffeeScript as a Node.js module.)
|
229
|
+
</td>
|
230
|
+
</tr>
|
231
|
+
<tr>
|
232
|
+
<td><code>-t, --tokens</code></td>
|
233
|
+
<td>
|
234
|
+
Instead of parsing the CoffeeScript, just lex it, and print out the
|
235
|
+
token stream: <tt>[IDENTIFIER square] [ASSIGN :] [PARAM_START (]</tt> ...
|
236
|
+
</td>
|
237
|
+
</tr>
|
238
|
+
<tr>
|
239
|
+
<td><code>-tr, --tree</code></td>
|
240
|
+
<td>
|
241
|
+
Instead of compiling the CoffeeScript, just lex and parse it, and print
|
242
|
+
out the parse tree:
|
243
|
+
<pre class="no_bar">
|
244
|
+
Expressions
|
245
|
+
Assign
|
246
|
+
Value "square"
|
247
|
+
Code "x"
|
248
|
+
Op *
|
249
|
+
Value "x"
|
250
|
+
Value "x"</pre>
|
251
|
+
</td>
|
252
|
+
</tr>
|
253
|
+
</table>
|
254
|
+
|
255
|
+
<p>
|
256
|
+
<b>Examples:</b>
|
257
|
+
</p>
|
258
|
+
|
259
|
+
<pre>
|
260
|
+
coffee path/to/script.coffee
|
261
|
+
coffee --interactive
|
262
|
+
coffee --watch --lint experimental.coffee
|
263
|
+
coffee --print app/scripts/*.coffee > concatenation.js</pre>
|
264
|
+
|
265
|
+
<h2>
|
266
|
+
<span id="language" class="bookmark"></span>
|
267
|
+
Language Reference
|
268
|
+
</h2>
|
269
|
+
|
270
|
+
<p>
|
271
|
+
<i>
|
272
|
+
This reference is structured so that it can be read from top to bottom,
|
273
|
+
if you like. Later sections use ideas and syntax previously introduced.
|
274
|
+
Familiarity with JavaScript is assumed.
|
275
|
+
In all of the following examples, the source CoffeeScript is provided on
|
276
|
+
the left, and the direct compilation into JavaScript is on the right.
|
277
|
+
</i>
|
278
|
+
</p>
|
279
|
+
|
280
|
+
<p>
|
281
|
+
<i>
|
282
|
+
Many of the examples can be run (where it makes sense) by pressing the "run"
|
283
|
+
button towards the bottom right. You can also paste examples into
|
284
|
+
"Try CoffeeScript" in the toolbar, and play with them from there.
|
285
|
+
</i>
|
286
|
+
<p>
|
287
|
+
<span id="whitespace" class="bookmark"></span>
|
288
|
+
<b class="header">Significant Whitespace</b>
|
289
|
+
CoffeeScript uses Python-style significant whitespace: You don't need to
|
290
|
+
use semicolons <tt>;</tt> to terminate expressions, ending
|
291
|
+
the line will do just as well. Semicolons can still be used to fit
|
292
|
+
multiple expressions onto a single line. Instead of using curly braces
|
293
|
+
<tt>{ }</tt> to delimit blocks of code (like <a href="#functions">functions</a>,
|
294
|
+
<a href="#conditionals">if-statements</a>,
|
295
|
+
<a href="#switch">switch</a>, and <a href="#try">try/catch</a>),
|
296
|
+
use indentation.
|
297
|
+
</p>
|
298
|
+
|
299
|
+
<p>
|
300
|
+
You don't need to use parentheses to invoke a function if you're passing
|
301
|
+
arguments:<br /><tt>print "coffee"</tt>
|
302
|
+
</p>
|
303
|
+
|
304
|
+
<p>
|
305
|
+
You can use newlines to break up your expression into smaller pieces,
|
306
|
+
as long as CoffeeScript can determine that the line hasn't finished yet,
|
307
|
+
because it ends with an operator or a dot.
|
308
|
+
</p>
|
309
|
+
|
310
|
+
<p>
|
311
|
+
<span id="functions" class="bookmark"></span>
|
312
|
+
<b class="header">Functions and Invocation</b>
|
313
|
+
Functions are defined by a list of parameters, an arrow, and the
|
314
|
+
function body. The empty function looks like this: <tt>-></tt> All
|
315
|
+
functions in CoffeeScript are named by default, for easier debugging.
|
316
|
+
</p>
|
317
|
+
<%= code_for('functions', 'cube(5)') %>
|
318
|
+
<p>
|
319
|
+
If you'd like to assign a function literal to a variable, but not have
|
320
|
+
it be named, just wrap the function definition in parentheses:
|
321
|
+
<tt>((x) -> x * x)</tt>
|
322
|
+
</p>
|
323
|
+
|
324
|
+
<p>
|
325
|
+
<span id="assignment" class="bookmark"></span>
|
326
|
+
<b class="header">Assignment</b>
|
327
|
+
Use a colon <tt>:</tt> to assign, as in
|
328
|
+
<a href="http://json.org">JSON</a>. Equal signs are only needed for
|
329
|
+
mathy things. While colons are preferred, the two may be used interchangeably,
|
330
|
+
even within object literals.
|
331
|
+
</p>
|
332
|
+
<%= code_for('assignment', 'greeting') %>
|
333
|
+
<p>
|
334
|
+
All declaration of new variables is pushed up to the top of the nearest
|
335
|
+
lexical scope, so that assignment may always be performed within expressions.
|
336
|
+
</p>
|
337
|
+
|
338
|
+
<p>
|
339
|
+
<span id="objects_and_arrays" class="bookmark"></span>
|
340
|
+
<b class="header">Objects and Arrays</b>
|
341
|
+
Object and Array literals look very similar to their JavaScript cousins.
|
342
|
+
When you spread out each assignment on a separate line, the commas are
|
343
|
+
optional. In this way, assigning object properties looks the same as
|
344
|
+
assigning local variables, and can be moved around freely. Feel free to mix
|
345
|
+
and match the two styles.
|
346
|
+
</p>
|
347
|
+
<%= code_for('objects_and_arrays', 'song.join(",")') %>
|
348
|
+
|
349
|
+
<p>
|
350
|
+
<span id="lexical_scope" class="bookmark"></span>
|
351
|
+
<b class="header">Lexical Scoping and Variable Safety</b>
|
352
|
+
The CoffeeScript compiler takes care to make sure that all of your variables
|
353
|
+
are properly declared within lexical scope — you never need to write
|
354
|
+
<tt>var</tt> yourself.
|
355
|
+
</p>
|
356
|
+
<%= code_for('scope', 'new_num') %>
|
357
|
+
<p>
|
358
|
+
Notice how the all of the variable declarations have been pushed up to
|
359
|
+
the top of the closest scope, the first time they appear.
|
360
|
+
<b>num</b> is not redeclared within the inner function, because it's
|
361
|
+
already in scope; the <b>new_num</b> within the function, on the other hand,
|
362
|
+
should not be able to change the value of the external variable of the same name, and
|
363
|
+
therefore has a declaration of its own.
|
364
|
+
</p>
|
365
|
+
<p>
|
366
|
+
Although suppressed within this documentation for clarity, all
|
367
|
+
CoffeeScript output is wrapped in an anonymous function:
|
368
|
+
<tt>(function(){ ... })();</tt> This safety wrapper, combined with the
|
369
|
+
automatic generation of the <tt>var</tt> keyword, make it exceedingly difficult
|
370
|
+
to pollute the global namespace by accident.
|
371
|
+
</p>
|
372
|
+
<p>
|
373
|
+
If you'd like to create top-level variables for other scripts to use,
|
374
|
+
attach them as properties on <b>window</b>, or on the <b>exports</b>
|
375
|
+
object in CommonJS. The <b>existential operator</b> (below), gives you a
|
376
|
+
reliable way to figure out where to add them, if you're targeting both
|
377
|
+
CommonJS and the browser: <tt>root: exports ? this</tt>
|
378
|
+
</p>
|
379
|
+
|
380
|
+
<p>
|
381
|
+
<span id="conditionals" class="bookmark"></span>
|
382
|
+
<b class="header">Conditionals, Ternaries, and Conditional Assignment</b>
|
383
|
+
<b>If/else</b> statements can be written without the use of parentheses and
|
384
|
+
curly brackets. As with functions and other block expressions,
|
385
|
+
multi-line conditionals are delimited by indentation. There's also a handy
|
386
|
+
postfix form, with the <tt>if</tt> or <tt>unless</tt> at the end.
|
387
|
+
</p>
|
388
|
+
<p>
|
389
|
+
CoffeeScript will compile <b>if</b> statements using the ternary operator
|
390
|
+
when possible, to make it easier to use the result as an expression.
|
391
|
+
</p>
|
392
|
+
<%= code_for('conditionals') %>
|
393
|
+
<p>
|
394
|
+
The conditional assignment operators are included: <tt>||=</tt>,
|
395
|
+
which only assigns a value to a variable if the variable's current value
|
396
|
+
is falsy, and <tt>&&=</tt>, which only replaces the value of
|
397
|
+
truthy variables.
|
398
|
+
</p>
|
399
|
+
|
400
|
+
<p>
|
401
|
+
<span id="aliases" class="bookmark"></span>
|
402
|
+
<b class="header">Aliases</b>
|
403
|
+
Because the <tt>==</tt> operator frequently causes undesirable coercion,
|
404
|
+
is intransitive, and has a different meaning than in other languages,
|
405
|
+
CoffeeScript compiles <tt>==</tt> into <tt>===</tt>, and <tt>!=</tt> into
|
406
|
+
<tt>!==</tt>.
|
407
|
+
In addition, <tt>is</tt> compiles into <tt>===</tt>,
|
408
|
+
and <tt>isnt</tt> into <tt>!==</tt>.
|
409
|
+
</p>
|
410
|
+
<p>
|
411
|
+
You can use <tt>not</tt> as an alias for <tt>!</tt>.
|
412
|
+
</p>
|
413
|
+
<p>
|
414
|
+
For logic, <tt>and</tt> compiles to <tt>&&</tt>, and <tt>or</tt>
|
415
|
+
into <tt>||</tt>.
|
416
|
+
</p>
|
417
|
+
<p>
|
418
|
+
Instead of a newline or semicolon, <tt>then</tt> can be used to separate
|
419
|
+
conditions from expressions, in <b>while</b>,
|
420
|
+
<b>if</b>/<b>else</b>, and <b>switch</b>/<b>when</b> statements.
|
421
|
+
</p>
|
422
|
+
<p>
|
423
|
+
As in <a href="http://yaml.org/">YAML</a>, <tt>on</tt> and <tt>yes</tt>
|
424
|
+
are the same as boolean <tt>true</tt>, while <tt>off</tt> and <tt>no</tt> are boolean <tt>false</tt>.
|
425
|
+
</p>
|
426
|
+
<p>
|
427
|
+
For single-line statements, <tt>unless</tt> can be used as the inverse of <tt>if</tt>.
|
428
|
+
</p>
|
429
|
+
<p>
|
430
|
+
As a shortcut for <tt>this.property</tt>, you can use <tt>@property</tt>.
|
431
|
+
</p>
|
432
|
+
<%= code_for('aliases') %>
|
433
|
+
|
434
|
+
<p>
|
435
|
+
<span id="splats" class="bookmark"></span>
|
436
|
+
<b class="header">Splats...</b>
|
437
|
+
The JavaScript <b>arguments object</b> is a useful way to work with
|
438
|
+
functions that accept variable numbers of arguments. CoffeeScript provides
|
439
|
+
splats <tt>...</tt>, both for function definition as well as invocation,
|
440
|
+
making variable numbers of arguments a little bit more palatable.
|
441
|
+
</p>
|
442
|
+
<%= code_for('splats', true) %>
|
443
|
+
|
444
|
+
<p>
|
445
|
+
<span id="arguments" class="bookmark"></span>
|
446
|
+
<b class="header">Arguments are Arrays</b>
|
447
|
+
If you reference the <b>arguments object</b> directly, it will be converted
|
448
|
+
into a real Array, making all of the
|
449
|
+
<a href="https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Objects/Array">Array methods</a>
|
450
|
+
available.
|
451
|
+
</p>
|
452
|
+
<%= code_for('arguments', true) %>
|
453
|
+
|
454
|
+
<p>
|
455
|
+
<span id="while" class="bookmark"></span>
|
456
|
+
<b class="header">While Loops</b>
|
457
|
+
The only low-level loop that CoffeeScript provides is the <b>while</b> loop. The
|
458
|
+
main difference from JavaScript is that the <b>while</b> loop can be used
|
459
|
+
as an expression, returning an array containing the result of each iteration
|
460
|
+
through the loop.
|
461
|
+
</p>
|
462
|
+
<%= code_for('while', 'lyrics.join("\n")') %>
|
463
|
+
<p>
|
464
|
+
Other JavaScript loops, such as <b>for</b> loops and <b>do-while</b> loops
|
465
|
+
can be mimicked by variations on <b>while</b>, but the hope is that you
|
466
|
+
won't need to do that with CoffeeScript, either because you're using
|
467
|
+
<b>each</b> (<b>forEach</b>) style iterators, or...
|
468
|
+
</p>
|
469
|
+
|
470
|
+
<p>
|
471
|
+
<span id="comprehensions" class="bookmark"></span>
|
472
|
+
<b class="header">Comprehensions (Arrays, Objects, and Ranges)</b>
|
473
|
+
For your looping needs, CoffeeScript provides array comprehensions
|
474
|
+
similar to Python's. They replace (and compile into) <b>for</b> loops, with
|
475
|
+
optional guard clauses and the value of the current array index.
|
476
|
+
Unlike for loops, array comprehensions are expressions, and can be returned
|
477
|
+
and assigned. They should be able to handle most places where you otherwise
|
478
|
+
would use a loop, <b>each</b>/<b>forEach</b>, <b>map</b>, or <b>select</b>/<b>filter</b>.
|
479
|
+
</p>
|
480
|
+
<%= code_for('array_comprehensions') %>
|
481
|
+
<p>
|
482
|
+
If you know the start and end of your loop, or would like to step through
|
483
|
+
in fixed-size increments, you can use a range to specify the start and
|
484
|
+
end of your comprehension. (The long line-breaking "for" definitions in
|
485
|
+
the compiled JS below allow ranges to count downwards, as well as upwards).
|
486
|
+
</p>
|
487
|
+
<%= code_for('range_comprehensions', 'countdown') %>
|
488
|
+
<p>
|
489
|
+
Comprehensions can also be used to iterate over the keys and values in
|
490
|
+
an object. Use <tt>of</tt> to signal comprehension over the properties of
|
491
|
+
an object instead of the values in an array.
|
492
|
+
</p>
|
493
|
+
<%= code_for('object_comprehensions', 'ages.join(", ")') %>
|
494
|
+
|
495
|
+
<p>
|
496
|
+
<span id="slice_splice" class="bookmark"></span>
|
497
|
+
<b class="header">Array Slicing and Splicing with Ranges</b>
|
498
|
+
CoffeeScript borrows Ruby's
|
499
|
+
<a href="http://ruby-doc.org/core/classes/Range.html">range syntax</a>
|
500
|
+
for extracting slices of arrays. With two dots (<tt>3..5</tt>), the range
|
501
|
+
is inclusive: the first argument is the index of the first element in
|
502
|
+
the slice, and the second is the index of the last one. Three dots signify
|
503
|
+
a range that excludes the end.
|
504
|
+
</p>
|
505
|
+
<%= code_for('slices', 'numbers_copy') %>
|
506
|
+
<p>
|
507
|
+
The same syntax can be used with assignment to replace a segment of an
|
508
|
+
array with new values (to splice it).
|
509
|
+
</p>
|
510
|
+
<%= code_for('splices', 'numbers') %>
|
511
|
+
|
512
|
+
<p>
|
513
|
+
<span id="expressions" class="bookmark"></span>
|
514
|
+
<b class="header">Everything is an Expression (at least, as much as possible)</b>
|
515
|
+
You might have noticed how even though we don't add return statements
|
516
|
+
to CoffeeScript functions, they nonetheless return their final value.
|
517
|
+
The CoffeeScript compiler tries to make sure that all statements in the
|
518
|
+
language can be used as expressions. Watch how the <tt>return</tt> gets
|
519
|
+
pushed down into each possible branch of execution, in the function
|
520
|
+
below.
|
521
|
+
</p>
|
522
|
+
<%= code_for('expressions', 'eldest') %>
|
523
|
+
<p>
|
524
|
+
Even though functions will always return their final value, it's both possible
|
525
|
+
and encouraged to return early from a function body writing out the explicit
|
526
|
+
return (<tt>return value</tt>), when you know that you're done.
|
527
|
+
</p>
|
528
|
+
<p>
|
529
|
+
Because variable declarations occur at the top of scope, assignment can
|
530
|
+
be used within expressions, even for variables that haven't been seen before:
|
531
|
+
</p>
|
532
|
+
<%= code_for('expressions_assignment', 'six') %>
|
533
|
+
<p>
|
534
|
+
Things that would otherwise be statements in JavaScript, when used
|
535
|
+
as part of an expression in CoffeeScript, are converted into expressions
|
536
|
+
by wrapping them in a closure. This lets you do useful things, like assign
|
537
|
+
the result of a comprehension to a variable:
|
538
|
+
</p>
|
539
|
+
<%= code_for('expressions_comprehension', 'globals') %>
|
540
|
+
<p>
|
541
|
+
As well as silly things, like passing a <b>try/catch</b> statement directly
|
542
|
+
into a function call:
|
543
|
+
</p>
|
544
|
+
<%= code_for('expressions_try', true) %>
|
545
|
+
<p>
|
546
|
+
There are a handful of statements in JavaScript that can't be meaningfully
|
547
|
+
converted into expressions, namely <tt>break</tt>, <tt>continue</tt>,
|
548
|
+
and <tt>return</tt>. If you make use of them within a block of code,
|
549
|
+
CoffeeScript won't try to perform the conversion.
|
550
|
+
</p>
|
551
|
+
|
552
|
+
<p>
|
553
|
+
<span id="existence" class="bookmark"></span>
|
554
|
+
<b class="header">The Existential Operator</b>
|
555
|
+
It's a little difficult to check for the existence of a variable in
|
556
|
+
JavaScript. <tt>if (variable) ...</tt> comes close, but fails for zero,
|
557
|
+
the empty string, and false. CoffeeScript's existential operator <tt>?</tt> returns true unless
|
558
|
+
a variable is <b>null</b> or <b>undefined</b>, which makes it analogous
|
559
|
+
to Ruby's <tt>nil?</tt>
|
560
|
+
</p>
|
561
|
+
<p>
|
562
|
+
It can also be used for safer conditional assignment than <tt>||=</tt>
|
563
|
+
provides, for cases where you may be handling numbers or strings.
|
564
|
+
</p>
|
565
|
+
<%= code_for('existence', 'speed') %>
|
566
|
+
<p>
|
567
|
+
The accessor variant of the existential operator <tt>?.</tt> can be used to soak
|
568
|
+
up null references in a chain of properties. Use it instead
|
569
|
+
of the dot accessor <tt>.</tt> in cases where the base value may be <b>null</b>
|
570
|
+
or <b>undefined</b>. If all of the properties exist then you'll get the expected
|
571
|
+
result, if the chain is broken, <b>undefined</b> is returned instead of
|
572
|
+
the <b>TypeError</b> that would be raised otherwise.
|
573
|
+
</p>
|
574
|
+
<%= code_for('soaks') %>
|
575
|
+
<p>
|
576
|
+
Soaking up nulls is similar to Ruby's
|
577
|
+
<a href="http://andand.rubyforge.org/">andand gem</a>, and to the
|
578
|
+
<a href="http://groovy.codehaus.org/Operators#Operators-SafeNavigationOperator%28%3F.%29">safe navigation operator</a>
|
579
|
+
in Groovy.
|
580
|
+
</p>
|
581
|
+
|
582
|
+
<p>
|
583
|
+
<span id="inheritance" class="bookmark"></span>
|
584
|
+
<b class="header">Inheritance, and Calling Super from a Subclass</b>
|
585
|
+
JavaScript's prototypal inheritance has always been a bit of a
|
586
|
+
brain-bender, with a whole family tree of libraries that provide a cleaner
|
587
|
+
syntax for classical inheritance on top of JavaScript's prototypes:
|
588
|
+
<a href="http://code.google.com/p/base2/">Base2</a>,
|
589
|
+
<a href="http://prototypejs.org/">Prototype.js</a>,
|
590
|
+
<a href="http://jsclass.jcoglan.com/">JS.Class</a>, etc.
|
591
|
+
The libraries provide syntactic sugar, but the built-in inheritance would
|
592
|
+
be completely usable if it weren't for a couple of small exceptions:
|
593
|
+
it's awkward to call <b>super</b> (the prototype object's
|
594
|
+
implementation of the current function), and it's awkward to correctly
|
595
|
+
set the prototype chain.
|
596
|
+
</p>
|
597
|
+
<p>
|
598
|
+
CoffeeScript provides <tt>extends</tt>
|
599
|
+
to help with prototype setup, <tt>::</tt> for quick access to an
|
600
|
+
object's prototype, and converts <tt>super()</tt> into a call against
|
601
|
+
the immediate ancestor's method of the same name.
|
602
|
+
</p>
|
603
|
+
<%= code_for('super', true) %>
|
604
|
+
|
605
|
+
<p>
|
606
|
+
<span id="pattern_matching" class="bookmark"></span>
|
607
|
+
<b class="header">Pattern Matching (Destructuring Assignment)</b>
|
608
|
+
To make extracting values from complex arrays and objects more convenient,
|
609
|
+
CoffeeScript implements ECMAScript Harmony's proposed
|
610
|
+
<a href="http://wiki.ecmascript.org/doku.php?id=harmony:destructuring">destructuring assignment</a>
|
611
|
+
syntax. When you assign an array or object literal to a value, CoffeeScript
|
612
|
+
breaks up and matches both sides against each other, assigning the values
|
613
|
+
on the right to the variables on the left. In the simplest case, it can be
|
614
|
+
used for parallel assignment:
|
615
|
+
</p>
|
616
|
+
<%= code_for('parallel_assignment', 'bait') %>
|
617
|
+
<p>
|
618
|
+
But it's also helpful for dealing with functions that return multiple
|
619
|
+
values.
|
620
|
+
</p>
|
621
|
+
<%= code_for('multiple_return_values', 'forecast') %>
|
622
|
+
<p>
|
623
|
+
Pattern matching can be used with any depth of array and object nesting,
|
624
|
+
to help pull out deeply nested properties.
|
625
|
+
</p>
|
626
|
+
<%= code_for('object_extraction', 'poet + " — " + street') %>
|
627
|
+
|
628
|
+
<p>
|
629
|
+
<span id="fat_arrow" class="bookmark"></span>
|
630
|
+
<b class="header">Function binding</b>
|
631
|
+
The fat arrow <tt>=></tt> can be used to both define a function, and to bind
|
632
|
+
it to the current value of <tt>this</tt>, right on the spot. This is helpful
|
633
|
+
when using callback-based libraries like Prototype or jQuery, for creating
|
634
|
+
iterator functions to pass to <tt>each</tt>, or event-handler functions
|
635
|
+
to use with <tt>bind</tt>. Functions created with the fat arrow are able to access
|
636
|
+
properties of the <tt>this</tt> where they're defined.
|
637
|
+
</p>
|
638
|
+
<%= code_for('fat_arrow') %>
|
639
|
+
|
640
|
+
<p>
|
641
|
+
<span id="embedded" class="bookmark"></span>
|
642
|
+
<b class="header">Embedded JavaScript</b>
|
643
|
+
Hopefully, you'll never need to use it, but if you ever need to intersperse
|
644
|
+
snippets of JavaScript within your CoffeeScript, you can
|
645
|
+
use backticks to pass it straight through.
|
646
|
+
</p>
|
647
|
+
<%= code_for('embedded', 'hi()') %>
|
648
|
+
|
649
|
+
<p>
|
650
|
+
<span id="switch" class="bookmark"></span>
|
651
|
+
<b class="header">Switch/When/Else</b>
|
652
|
+
<b>Switch</b> statements in JavaScript are rather broken. You can only
|
653
|
+
do comparisons based on string equality, and need to remember to <b>break</b> at the end of
|
654
|
+
every <b>case</b> statement to avoid accidentally falling through to
|
655
|
+
the default case. CoffeeScript compiles <b>switch</b> statements into JavaScript if-else chains, allowing you to
|
656
|
+
compare any object (via <b>===</b>), preventing fall-through, and resulting
|
657
|
+
in a returnable, assignable expression. The format is: <tt>switch</tt> condition,
|
658
|
+
<tt>when</tt> clauses, <tt>else</tt> the default case.
|
659
|
+
</p>
|
660
|
+
<p>
|
661
|
+
As in Ruby, <b>switch</b> statements in CoffeeScript can take multiple
|
662
|
+
values for each <b>when</b> clause. If any of the values match, the clause
|
663
|
+
runs.
|
664
|
+
</p>
|
665
|
+
<%= code_for('switch') %>
|
666
|
+
|
667
|
+
<p>
|
668
|
+
<span id="try" class="bookmark"></span>
|
669
|
+
<b class="header">Try/Catch/Finally</b>
|
670
|
+
Try/catch statements are just about the same as JavaScript (although
|
671
|
+
they work as expressions).
|
672
|
+
</p>
|
673
|
+
<%= code_for('try') %>
|
674
|
+
|
675
|
+
<p>
|
676
|
+
<span id="comparisons" class="bookmark"></span>
|
677
|
+
<b class="header">Chained Comparisons</b>
|
678
|
+
CoffeeScript borrows
|
679
|
+
<a href="http://docs.python.org/reference/expressions.html#notin">chained comparisons</a>
|
680
|
+
from Python — making it easy to test if a value falls within a
|
681
|
+
certain range.
|
682
|
+
</p>
|
683
|
+
<%= code_for('comparisons', 'healthy') %>
|
684
|
+
|
685
|
+
<p>
|
686
|
+
<span id="strings" class="bookmark"></span>
|
687
|
+
<b class="header">Multiline Strings and Heredocs</b>
|
688
|
+
Multiline strings are allowed in CoffeeScript.
|
689
|
+
</p>
|
690
|
+
<%= code_for('strings', 'moby_dick') %>
|
691
|
+
<p>
|
692
|
+
Heredocs can be used to hold formatted or indentation-sensitive text
|
693
|
+
(or, if you just don't feel like escaping quotes and apostrophes). The
|
694
|
+
indentation level that begins the heredoc is maintained throughout, so
|
695
|
+
you can keep it all aligned with the body of your code.
|
696
|
+
</p>
|
697
|
+
<%= code_for('heredocs') %>
|
698
|
+
|
699
|
+
<h2>
|
700
|
+
<span id="cake" class="bookmark"></span>
|
701
|
+
Cake, and Cakefiles
|
702
|
+
</h2>
|
703
|
+
|
704
|
+
<p>
|
705
|
+
CoffeeScript includes a simple build system similar to Make and Rake. Naturally,
|
706
|
+
it's called Cake, and is used for the build and test tasks for the CoffeeScript
|
707
|
+
language itself. Tasks are defined in a file named <tt>Cakefile</tt>, and
|
708
|
+
can be invoked by running <tt>cake taskname</tt> from within the directory.
|
709
|
+
To print a list of all the tasks, just run <tt>cake</tt>.
|
710
|
+
</p>
|
711
|
+
|
712
|
+
<p>
|
713
|
+
Task definitions are written in CoffeeScript, so you can put arbitrary code
|
714
|
+
in your Cakefile. Define a task with a name, a long description, and the
|
715
|
+
function to invoke when the task is run. Here's a hypothetical task
|
716
|
+
that uses the Node.js API.
|
717
|
+
</p>
|
718
|
+
<%= code_for('cake_tasks') %>
|
719
|
+
|
720
|
+
<h2>
|
721
|
+
<span id="resources" class="bookmark"></span>
|
722
|
+
Resources
|
723
|
+
</h2>
|
724
|
+
|
725
|
+
<ul>
|
726
|
+
<li>
|
727
|
+
<a href="http://github.com/jashkenas/coffee-script/">Source Code</a><br />
|
728
|
+
Use <tt>bin/coffee</tt> to test your changes,<br />
|
729
|
+
<tt>bin/cake test</tt> to run the test suite,<br />
|
730
|
+
<tt>bin/cake build</tt> to rebuild the CoffeeScript compiler, and <br />
|
731
|
+
<tt>bin/cake build:parser</tt> to regenerate the Jison parser if you're
|
732
|
+
working on the grammar.
|
733
|
+
</li>
|
734
|
+
<li>
|
735
|
+
<a href="http://github.com/jashkenas/coffee-script/issues">CoffeeScript Issues</a><br />
|
736
|
+
Bugs reports, feature requests, and general discussion all belong here.
|
737
|
+
</li>
|
738
|
+
<li>
|
739
|
+
If you'd like to chat, stop by <tt>#coffeescript</tt> on Freenode.
|
740
|
+
</li>
|
741
|
+
</ul>
|
742
|
+
|
743
|
+
<h2>
|
744
|
+
<span id="change_log" class="bookmark"></span>
|
745
|
+
Change Log
|
746
|
+
</h2>
|
747
|
+
|
748
|
+
<p>
|
749
|
+
<b class="header" style="margin-top: 20px;">0.5.1</b>
|
750
|
+
Improvements to null soaking with the existential operator, including
|
751
|
+
soaks on indexed properties. Added conditions to <tt>while</tt> loops,
|
752
|
+
so you can use them as filters with <tt>when</tt>, in the same manner as
|
753
|
+
comprehensions.
|
754
|
+
</p>
|
755
|
+
|
756
|
+
<p>
|
757
|
+
<b class="header" style="margin-top: 20px;">0.5.0</b>
|
758
|
+
CoffeeScript 0.5.0 is a major release, While there are no language changes,
|
759
|
+
the Ruby compiler has been removed in favor of a self-hosting
|
760
|
+
compiler written in pure CoffeeScript.
|
761
|
+
</p>
|
762
|
+
|
763
|
+
<p>
|
764
|
+
<b class="header" style="margin-top: 20px;">0.3.2</b>
|
765
|
+
<tt>@property</tt> is now a shorthand for <tt>this.property</tt>.<br />
|
766
|
+
Switched the default JavaScript engine from Narwhal to Node.js. Pass
|
767
|
+
the <tt>--narwhal</tt> flag if you'd like to continue using it.
|
768
|
+
</p>
|
769
|
+
|
770
|
+
<p>
|
771
|
+
<b class="header" style="margin-top: 20px;">0.3.0</b>
|
772
|
+
CoffeeScript 0.3 includes major syntax changes:
|
773
|
+
<br />
|
774
|
+
The function symbol was changed to
|
775
|
+
<tt>-></tt>, and the bound function symbol is now <tt>=></tt>.
|
776
|
+
<br />
|
777
|
+
Parameter lists in function definitions must now be wrapped in parentheses.
|
778
|
+
<br />
|
779
|
+
Added property soaking, with the <tt>?.</tt> operator.
|
780
|
+
<br />
|
781
|
+
Made parentheses optional, when invoking functions with arguments.
|
782
|
+
<br />
|
783
|
+
Removed the obsolete block literal syntax.
|
784
|
+
</p>
|
785
|
+
|
786
|
+
<p>
|
787
|
+
<b class="header" style="margin-top: 20px;">0.2.6</b>
|
788
|
+
Added Python-style chained comparisons, the conditional existence
|
789
|
+
operator <tt>?=</tt>, and some examples from <i>Beautiful Code</i>.
|
790
|
+
Bugfixes relating to statement-to-expression conversion, arguments-to-array
|
791
|
+
conversion, and the TextMate syntax highlighter.
|
792
|
+
</p>
|
793
|
+
|
794
|
+
<p>
|
795
|
+
<b class="header" style="margin-top: 20px;">0.2.5</b>
|
796
|
+
The conditions in switch statements can now take multiple values at once —
|
797
|
+
If any of them are true, the case will run. Added the long arrow <tt>==></tt>,
|
798
|
+
which defines and immediately binds a function to <tt>this</tt>. While loops can
|
799
|
+
now be used as expressions, in the same way that comprehensions can. Splats
|
800
|
+
can be used within pattern matches to soak up the rest of an array.
|
801
|
+
</p>
|
802
|
+
|
803
|
+
<p>
|
804
|
+
<b class="header" style="margin-top: 20px;">0.2.4</b>
|
805
|
+
Added ECMAScript Harmony style destructuring assignment, for dealing with
|
806
|
+
extracting values from nested arrays and objects. Added indentation-sensitive
|
807
|
+
heredocs for nicely formatted strings or chunks of code.
|
808
|
+
</p>
|
809
|
+
|
810
|
+
<p>
|
811
|
+
<b class="header" style="margin-top: 20px;">0.2.3</b>
|
812
|
+
Axed the unsatisfactory <tt>ino</tt> keyword, replacing it with <tt>of</tt> for
|
813
|
+
object comprehensions. They now look like: <tt>for prop, value of object</tt>.
|
814
|
+
</p>
|
815
|
+
|
816
|
+
<p>
|
817
|
+
<b class="header" style="margin-top: 20px;">0.2.2</b>
|
818
|
+
When performing a comprehension over an object, use <tt>ino</tt>, instead
|
819
|
+
of <tt>in</tt>, which helps us generate smaller, more efficient code at
|
820
|
+
compile time.
|
821
|
+
<br />
|
822
|
+
Added <tt>::</tt> as a shorthand for saying <tt>.prototype.</tt>
|
823
|
+
<br />
|
824
|
+
The "splat" symbol has been changed from a prefix asterisk <tt>*</tt>, to
|
825
|
+
a postfix ellipsis <tt>...</tt>
|
826
|
+
<br />
|
827
|
+
Added JavaScript's <tt>in</tt> operator,
|
828
|
+
empty <tt>return</tt> statements, and empty <tt>while</tt> loops.
|
829
|
+
<br />
|
830
|
+
Constructor functions that start with capital letters now include a
|
831
|
+
safety check to make sure that the new instance of the object is returned.
|
832
|
+
<br />
|
833
|
+
The <tt>extends</tt> keyword now functions identically to <tt>goog.inherits</tt>
|
834
|
+
in Google's Closure Library.
|
835
|
+
</p>
|
836
|
+
|
837
|
+
<p>
|
838
|
+
<b class="header" style="margin-top: 20px;">0.2.1</b>
|
839
|
+
Arguments objects are now converted into real arrays when referenced.
|
840
|
+
</p>
|
841
|
+
|
842
|
+
<p>
|
843
|
+
<b class="header" style="margin-top: 20px;">0.2.0</b>
|
844
|
+
Major release. Significant whitespace. Better statement-to-expression
|
845
|
+
conversion. Splats. Splice literals. Object comprehensions. Blocks.
|
846
|
+
The existential operator. Many thanks to all the folks who posted issues,
|
847
|
+
with special thanks to
|
848
|
+
<a href="http://github.com/kamatsu">Liam O'Connor-Davis</a> for whitespace
|
849
|
+
and expression help.
|
850
|
+
</p>
|
851
|
+
|
852
|
+
<p>
|
853
|
+
<b class="header" style="margin-top: 20px;">0.1.6</b>
|
854
|
+
Bugfix for running <tt>coffee --interactive</tt> and <tt>--run</tt>
|
855
|
+
from outside of the CoffeeScript directory. Bugfix for nested
|
856
|
+
function/if-statements.
|
857
|
+
</p>
|
858
|
+
|
859
|
+
<p>
|
860
|
+
<b class="header" style="margin-top: 20px;">0.1.5</b>
|
861
|
+
Array slice literals and array comprehensions can now both take Ruby-style
|
862
|
+
ranges to specify the start and end. JavaScript variable declaration is
|
863
|
+
now pushed up to the top of the scope, making all assignment statements into
|
864
|
+
expressions. You can use <tt>\</tt> to escape newlines.
|
865
|
+
The <tt>coffee-script</tt> command is now called <tt>coffee</tt>.
|
866
|
+
</p>
|
867
|
+
|
868
|
+
<p>
|
869
|
+
<b class="header" style="margin-top: 20px;">0.1.4</b>
|
870
|
+
The official CoffeeScript extension is now <tt>.coffee</tt> instead of
|
871
|
+
<tt>.cs</tt>, which properly belongs to
|
872
|
+
<a href="http://en.wikipedia.org/wiki/C_Sharp_(programming_language)">C#</a>.
|
873
|
+
Due to popular demand, you can now also use <tt>=</tt> to assign. Unlike
|
874
|
+
JavaScript, <tt>=</tt> can also be used within object literals, interchangeably
|
875
|
+
with <tt>:</tt>. Made a grammatical fix for chained function calls
|
876
|
+
like <tt>func(1)(2)(3)(4)</tt>. Inheritance and super no longer use
|
877
|
+
<tt>__proto__</tt>, so they should be IE-compatible now.
|
878
|
+
</p>
|
879
|
+
|
880
|
+
<p>
|
881
|
+
<b class="header" style="margin-top: 20px;">0.1.3</b>
|
882
|
+
The <tt>coffee</tt> command now includes <tt>--interactive</tt>,
|
883
|
+
which launches an interactive CoffeeScript session, and <tt>--run</tt>,
|
884
|
+
which directly compiles and executes a script. Both options depend on a
|
885
|
+
working installation of Narwhal.
|
886
|
+
The <tt>aint</tt> keyword has been replaced by <tt>isnt</tt>, which goes
|
887
|
+
together a little smoother with <tt>is</tt>.
|
888
|
+
Quoted strings are now allowed as identifiers within object literals: eg.
|
889
|
+
<tt>{"5+5": 10}</tt>.
|
890
|
+
All assignment operators now use a colon: <tt>+:</tt>, <tt>-:</tt>,
|
891
|
+
<tt>*:</tt>, etc.
|
892
|
+
</p>
|
893
|
+
|
894
|
+
<p>
|
895
|
+
<b class="header" style="margin-top: 20px;">0.1.2</b>
|
896
|
+
Fixed a bug with calling <tt>super()</tt> through more than one level of
|
897
|
+
inheritance, with the re-addition of the <tt>extends</tt> keyword.
|
898
|
+
Added experimental <a href="http://narwhaljs.org/">Narwhal</a>
|
899
|
+
support (as a Tusk package), contributed by
|
900
|
+
<a href="http://tlrobinson.net/">Tom Robinson</a>, including
|
901
|
+
<b>bin/cs</b> as a CoffeeScript REPL and interpreter.
|
902
|
+
New <tt>--no-wrap</tt> option to suppress the safety function
|
903
|
+
wrapper.
|
904
|
+
</p>
|
905
|
+
|
906
|
+
<p>
|
907
|
+
<b class="header" style="margin-top: 20px;">0.1.1</b>
|
908
|
+
Added <tt>instanceof</tt> and <tt>typeof</tt> as operators.
|
909
|
+
</p>
|
910
|
+
|
911
|
+
<p>
|
912
|
+
<b class="header" style="margin-top: 20px;">0.1.0</b>
|
913
|
+
Initial CoffeeScript release.
|
914
|
+
</p>
|
915
|
+
|
916
|
+
</div>
|
917
|
+
|
918
|
+
<script type="text/javascript" src="lib/rewriter.js"></script>
|
919
|
+
<script type="text/javascript" src="lib/lexer.js"></script>
|
920
|
+
<script type="text/javascript" src="lib/parser.js"></script>
|
921
|
+
<script type="text/javascript" src="lib/scope.js"></script>
|
922
|
+
<script type="text/javascript" src="lib/nodes.js"></script>
|
923
|
+
<script type="text/javascript" src="lib/coffee-script.js"></script>
|
924
|
+
|
925
|
+
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
|
926
|
+
|
927
|
+
<script type="text/javascript">
|
928
|
+
window.repl_compile = function() {
|
929
|
+
var source = $('#repl_source').val();
|
930
|
+
window.compiled_js = '';
|
931
|
+
try {
|
932
|
+
window.compiled_js = CoffeeScript.compile(source, {no_wrap: true});
|
933
|
+
} catch(error) {
|
934
|
+
alert(error);
|
935
|
+
}
|
936
|
+
$('#repl_results').html(window.compiled_js);
|
937
|
+
};
|
938
|
+
window.repl_run = function() {
|
939
|
+
try {
|
940
|
+
eval(window.compiled_js);
|
941
|
+
} catch(error) {
|
942
|
+
alert(error);
|
943
|
+
}
|
944
|
+
};
|
945
|
+
|
946
|
+
var nav = $('.navigation');
|
947
|
+
var currentNav = null;
|
948
|
+
var closeMenus = function() {
|
949
|
+
if (currentNav) currentNav.removeClass('active');
|
950
|
+
currentNav = null;
|
951
|
+
};
|
952
|
+
nav.click(function(e) {
|
953
|
+
if (e.target.tagName.toLowerCase() == 'a') return;
|
954
|
+
if (this !== (currentNav && currentNav[0])) {
|
955
|
+
closeMenus();
|
956
|
+
currentNav = $(this);
|
957
|
+
currentNav.addClass('active');
|
958
|
+
}
|
959
|
+
return false;
|
960
|
+
});
|
961
|
+
$(document.body).click(function() {
|
962
|
+
closeMenus();
|
963
|
+
});
|
964
|
+
</script>
|
965
|
+
|
966
|
+
</body>
|
967
|
+
</html>
|