From 980a663c1952f0fafde07eaf8d454c0446833979 Mon Sep 17 00:00:00 2001
From: Jeremy Ashkenas
- Sometimes you'd like to pass a block comment through to the generated - JavaScript. For example, when you need to embed a licensing header at - the top of a file. Block comments, which mirror the synax for heredocs, + Sometimes you'd like to pass a block comment through to the generated + JavaScript. For example, when you need to embed a licensing header at + the top of a file. Block comments, which mirror the synax for heredocs, are preserved in the generated code.
<%= code_for('block_comment') %> @@ -806,8 +805,8 @@ coffee --print app/scripts/*.coffee > concatenation.js
- CoffeeScript includes a simple build system similar to
- Make and
+ CoffeeScript includes a simple build system similar to
+ Make and
Rake. Naturally,
it's called Cake, and is used for the build and test tasks for the CoffeeScript
language itself. Tasks are defined in a file named Cakefile, and
diff --git a/documentation/js/aliases.js b/documentation/js/aliases.js
index f5c56086..6f8a0800 100644
--- a/documentation/js/aliases.js
+++ b/documentation/js/aliases.js
@@ -1,17 +1,15 @@
-(function(){
- var volume, winner;
- if (ignition === true) {
- launch();
- }
- if (band !== SpinalTap) {
- volume = 10;
- }
- if (!(answer === false)) {
- letTheWildRumpusBegin();
- }
- car.speed < limit ? accelerate() : null;
- if (47 === pick || 92 === pick || 13 === pick) {
- winner = true;
- }
- print("My name is " + this.name);
-})();
+var volume, winner;
+if (ignition === true) {
+ launch();
+}
+if (band !== SpinalTap) {
+ volume = 10;
+}
+if (!(answer === false)) {
+ letTheWildRumpusBegin();
+}
+car.speed < limit ? accelerate() : null;
+if (47 === pick || 92 === pick || 13 === pick) {
+ winner = true;
+}
+print("My name is " + this.name);
\ No newline at end of file
diff --git a/documentation/js/array_comprehensions.js b/documentation/js/array_comprehensions.js
index d0dbb18c..29b68327 100644
--- a/documentation/js/array_comprehensions.js
+++ b/documentation/js/array_comprehensions.js
@@ -1,24 +1,22 @@
-(function(){
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, food, lunch, roid, roid2;
- lunch = (function() {
- _a = []; _c = ['toast', 'cheese', 'wine'];
- for (_b = 0, _d = _c.length; _b < _d; _b++) {
- food = _c[_b];
- _a.push(eat(food));
- }
- return _a;
- })();
- _f = asteroids;
- for (_e = 0, _g = _f.length; _e < _g; _e++) {
- roid = _f[_e];
- _i = asteroids;
- for (_h = 0, _j = _i.length; _h < _j; _h++) {
- roid2 = _i[_h];
- if (roid !== roid2) {
- if (roid.overlaps(roid2)) {
- roid.explode();
- }
+var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, food, lunch, roid, roid2;
+lunch = (function() {
+ _a = []; _c = ['toast', 'cheese', 'wine'];
+ for (_b = 0, _d = _c.length; _b < _d; _b++) {
+ food = _c[_b];
+ _a.push(eat(food));
+ }
+ return _a;
+})();
+_f = asteroids;
+for (_e = 0, _g = _f.length; _e < _g; _e++) {
+ roid = _f[_e];
+ _i = asteroids;
+ for (_h = 0, _j = _i.length; _h < _j; _h++) {
+ roid2 = _i[_h];
+ if (roid !== roid2) {
+ if (roid.overlaps(roid2)) {
+ roid.explode();
}
}
}
-})();
+}
\ No newline at end of file
diff --git a/documentation/js/assignment.js b/documentation/js/assignment.js
index 5dbd5e24..106a6bff 100644
--- a/documentation/js/assignment.js
+++ b/documentation/js/assignment.js
@@ -1,5 +1,3 @@
-(function(){
- var difficulty, greeting;
- greeting = "Hello CoffeeScript";
- difficulty = 0.5;
-})();
+var difficulty, greeting;
+greeting = "Hello CoffeeScript";
+difficulty = 0.5;
\ No newline at end of file
diff --git a/documentation/js/block_comment.js b/documentation/js/block_comment.js
index c7c89d61..953e8b72 100644
--- a/documentation/js/block_comment.js
+++ b/documentation/js/block_comment.js
@@ -1,6 +1,4 @@
-(function(){
- /*
- CoffeeScript Compiler v0.7.2
- Released under the MIT License
- */
-})();
+/*
+CoffeeScript Compiler v0.7.2
+Released under the MIT License
+*/
\ No newline at end of file
diff --git a/documentation/js/cake_tasks.js b/documentation/js/cake_tasks.js
index d53e9e86..9086cde7 100644
--- a/documentation/js/cake_tasks.js
+++ b/documentation/js/cake_tasks.js
@@ -1,12 +1,10 @@
-(function(){
- var fs;
- fs = require('fs');
- option('-o', '--output [DIR]', 'directory for compiled code');
- task('build:parser', 'rebuild the Jison parser', function() {
- var code, dir;
- require('jison');
- code = require('./lib/grammar').parser.generate();
- dir = options.output || 'lib';
- return fs.writeFile(("" + dir + "/parser.js"), code);
- });
-})();
+var fs;
+fs = require('fs');
+option('-o', '--output [DIR]', 'directory for compiled code');
+task('build:parser', 'rebuild the Jison parser', function() {
+ var code, dir;
+ require('jison');
+ code = require('./lib/grammar').parser.generate();
+ dir = options.output || 'lib';
+ return fs.writeFile(("" + dir + "/parser.js"), code);
+});
\ No newline at end of file
diff --git a/documentation/js/classes.js b/documentation/js/classes.js
index a4fccd95..463ce6ca 100644
--- a/documentation/js/classes.js
+++ b/documentation/js/classes.js
@@ -1,37 +1,36 @@
-(function(){
- var Animal, Horse, Snake, sam, tom;
- var __extends = function(child, parent) {
- var ctor = function(){ };
+var Animal, Horse, Snake, sam, tom;
+var __extends = function(child, parent) {
+ var ctor = function(){};
ctor.prototype = parent.prototype;
child.prototype = new ctor();
child.prototype.constructor = child;
if (typeof parent.extended === "function") parent.extended(child);
child.__superClass__ = parent.prototype;
};
- Animal = function() {};
- Animal.prototype.move = function(meters) {
- return alert(this.name + " moved " + meters + "m.");
- };
- Snake = function(name) {
- this.name = name;
- return this;
- };
- __extends(Snake, Animal);
- Snake.prototype.move = function() {
- alert("Slithering...");
- return Snake.__superClass__.move.call(this, 5);
- };
- Horse = function(name) {
- this.name = name;
- return this;
- };
- __extends(Horse, Animal);
- Horse.prototype.move = function() {
- alert("Galloping...");
- return Horse.__superClass__.move.call(this, 45);
- };
- sam = new Snake("Sammy the Python");
- tom = new Horse("Tommy the Palomino");
- sam.move();
- tom.move();
-})();
+Animal = function(_a) {
+ this.name = _a;
+ return this;
+};
+Animal.prototype.move = function(meters) {
+ return alert(this.name + " moved " + meters + "m.");
+};
+Snake = function() {
+ return Animal.apply(this, arguments);
+};
+__extends(Snake, Animal);
+Snake.prototype.move = function() {
+ alert("Slithering...");
+ return Snake.__superClass__.move.call(this, 5);
+};
+Horse = function() {
+ return Animal.apply(this, arguments);
+};
+__extends(Horse, Animal);
+Horse.prototype.move = function() {
+ alert("Galloping...");
+ return Horse.__superClass__.move.call(this, 45);
+};
+sam = new Snake("Sammy the Python");
+tom = new Horse("Tommy the Palomino");
+sam.move();
+tom.move();
\ No newline at end of file
diff --git a/documentation/js/comparisons.js b/documentation/js/comparisons.js
index f7570581..e1ce0cc0 100644
--- a/documentation/js/comparisons.js
+++ b/documentation/js/comparisons.js
@@ -1,5 +1,3 @@
-(function(){
- var cholesterol, healthy;
- cholesterol = 127;
- healthy = (200 > cholesterol) && (cholesterol > 60);
-})();
+var cholesterol, healthy;
+cholesterol = 127;
+healthy = (200 > cholesterol) && (cholesterol > 60);
\ No newline at end of file
diff --git a/documentation/js/conditionals.js b/documentation/js/conditionals.js
index a16a20d6..d410542d 100644
--- a/documentation/js/conditionals.js
+++ b/documentation/js/conditionals.js
@@ -1,12 +1,12 @@
-(function(){
- var date, mood, options;
- if (singing) {
- mood = greatlyImproved;
- }
- if (happy && knowsIt) {
- clapsHands();
- chaChaCha();
- }
- date = friday ? sue : jill;
- options = options || defaultOptions;
-})();
+var date, mood, options;
+if (singing) {
+ mood = greatlyImproved;
+}
+if (happy && knowsIt) {
+ clapsHands();
+ chaChaCha();
+} else {
+ showIt();
+}
+date = friday ? sue : jill;
+options = options || defaults;
\ No newline at end of file
diff --git a/documentation/js/embedded.js b/documentation/js/embedded.js
index 878acd86..3db2b0fb 100644
--- a/documentation/js/embedded.js
+++ b/documentation/js/embedded.js
@@ -1,6 +1,4 @@
-(function(){
- var hi;
- hi = function() {
+var hi;
+hi = function() {
return [document.title, "Hello JavaScript"].join(": ");
-};
-})();
+};
\ No newline at end of file
diff --git a/documentation/js/existence.js b/documentation/js/existence.js
index f2c353d2..f48fdc59 100644
--- a/documentation/js/existence.js
+++ b/documentation/js/existence.js
@@ -1,7 +1,5 @@
-(function(){
- var solipsism, speed;
- if ((typeof mind !== "undefined" && mind !== null) && !(typeof world !== "undefined" && world !== null)) {
- solipsism = true;
- }
- speed = (typeof speed !== "undefined" && speed !== null) ? speed : 140;
-})();
+var solipsism, speed;
+if ((typeof mind !== "undefined" && mind !== null) && !(typeof world !== "undefined" && world !== null)) {
+ solipsism = true;
+}
+speed = (typeof speed !== "undefined" && speed !== null) ? speed : 140;
\ No newline at end of file
diff --git a/documentation/js/expressions.js b/documentation/js/expressions.js
index 2e21f24a..9dd8947d 100644
--- a/documentation/js/expressions.js
+++ b/documentation/js/expressions.js
@@ -1,13 +1,11 @@
-(function(){
- var eldest, grade;
- grade = function(student) {
- if (student.excellentWork) {
- return "A+";
- } else if (student.okayStuff) {
- return student.triedHard ? "B" : "B-";
- } else {
- return "C";
- }
- };
- eldest = 24 > 21 ? "Liz" : "Ike";
-})();
+var eldest, grade;
+grade = function(student) {
+ if (student.excellentWork) {
+ return "A+";
+ } else if (student.okayStuff) {
+ return student.triedHard ? "B" : "B-";
+ } else {
+ return "C";
+ }
+};
+eldest = 24 > 21 ? "Liz" : "Ike";
\ No newline at end of file
diff --git a/documentation/js/expressions_assignment.js b/documentation/js/expressions_assignment.js
index 1f6d7991..e75323d5 100644
--- a/documentation/js/expressions_assignment.js
+++ b/documentation/js/expressions_assignment.js
@@ -1,4 +1,2 @@
-(function(){
- var one, six, three, two;
- six = (one = 1) + (two = 2) + (three = 3);
-})();
+var one, six, three, two;
+six = (one = 1) + (two = 2) + (three = 3);
\ No newline at end of file
diff --git a/documentation/js/expressions_comprehension.js b/documentation/js/expressions_comprehension.js
index 5f15e5f4..39d2e660 100644
--- a/documentation/js/expressions_comprehension.js
+++ b/documentation/js/expressions_comprehension.js
@@ -1,12 +1,11 @@
-(function(){
- var _a, _b, globals, name;
- var __hasProp = Object.prototype.hasOwnProperty;
- globals = (function() {
- _a = []; _b = window;
- for (name in _b) {
- if (!__hasProp.call(_b, name)) continue;
- _a.push(name);
- }
- return _a;
- })().slice(0, 10);
-})();
+var _a, _b, _c, globals, name;
+var __hasProp = Object.prototype.hasOwnProperty;
+globals = (function() {
+ _b = []; _c = window;
+ for (name in _c) {
+ if (!__hasProp.call(_c, name)) continue;
+ _a = _c[name];
+ _b.push(name);
+ }
+ return _b;
+})().slice(0, 10);
\ No newline at end of file
diff --git a/documentation/js/expressions_try.js b/documentation/js/expressions_try.js
index a6bb2bca..19a9506c 100644
--- a/documentation/js/expressions_try.js
+++ b/documentation/js/expressions_try.js
@@ -1,9 +1,7 @@
-(function(){
- alert((function() {
- try {
- return nonexistent / undefined;
- } catch (error) {
- return "And the error is ... " + error;
- }
- })());
-})();
+alert((function() {
+ try {
+ return nonexistent / undefined;
+ } catch (error) {
+ return "And the error is ... " + error;
+ }
+})());
\ No newline at end of file
diff --git a/documentation/js/fat_arrow.js b/documentation/js/fat_arrow.js
index 0481518f..41744ed5 100644
--- a/documentation/js/fat_arrow.js
+++ b/documentation/js/fat_arrow.js
@@ -1,15 +1,11 @@
-(function(){
- var Account;
- Account = function(customer, cart) {
- this.customer = customer;
- this.cart = cart;
- return $('.shopping_cart').bind('click', (function(__this) {
- var __func = function(event) {
- return this.customer.purchase(this.cart);
- };
- return (function() {
- return __func.apply(__this, arguments);
- });
- })(this));
+var Account;
+var __bind = function(func, context) {
+ return function(){ return func.apply(context, arguments); };
};
-})();
+Account = function(customer, cart) {
+ this.customer = customer;
+ this.cart = cart;
+ return $('.shopping_cart').bind('click', __bind(function(event) {
+ return this.customer.purchase(this.cart);
+ }, this));
+};
\ No newline at end of file
diff --git a/documentation/js/functions.js b/documentation/js/functions.js
index 2678a166..ec38bc78 100644
--- a/documentation/js/functions.js
+++ b/documentation/js/functions.js
@@ -1,9 +1,7 @@
-(function(){
- var cube, square;
- square = function(x) {
- return x * x;
- };
- cube = function(x) {
- return square(x) * x;
- };
-})();
+var cube, square;
+square = function(x) {
+ return x * x;
+};
+cube = function(x) {
+ return square(x) * x;
+};
\ No newline at end of file
diff --git a/documentation/js/heredocs.js b/documentation/js/heredocs.js
index bc9f2d6b..0cbcb2aa 100644
--- a/documentation/js/heredocs.js
+++ b/documentation/js/heredocs.js
@@ -1,4 +1,2 @@
-(function(){
- var html;
- html = '\n cup of coffeescript\n';
-})();
+var html;
+html = '\n cup of coffeescript\n';
\ No newline at end of file
diff --git a/documentation/js/interpolation.js b/documentation/js/interpolation.js
index 369d5c8a..6b4e124a 100644
--- a/documentation/js/interpolation.js
+++ b/documentation/js/interpolation.js
@@ -1,5 +1,3 @@
-(function(){
- var author, quote;
- author = "Wittgenstein";
- quote = ("A picture is a fact. -- " + author);
-})();
+var author, quote;
+author = "Wittgenstein";
+quote = ("A picture is a fact. -- " + author);
\ No newline at end of file
diff --git a/documentation/js/interpolation_expression.js b/documentation/js/interpolation_expression.js
index fe7e5239..9651e488 100644
--- a/documentation/js/interpolation_expression.js
+++ b/documentation/js/interpolation_expression.js
@@ -1,6 +1,4 @@
-(function(){
- var dates, sentence, sep;
- sentence = ("" + (22 / 7) + " is a decent approximation of π");
- sep = "[.\\/\\- ]";
- dates = (new RegExp(("\\d+" + sep + "\\d+" + sep + "\\d+"), "g"));
-})();
+var dates, sentence, sep;
+sentence = ("" + (22 / 7) + " is a decent approximation of π");
+sep = "[.\\/\\- ]";
+dates = /\d+$sep\d+$sep\d+/g;
\ No newline at end of file
diff --git a/documentation/js/multiple_return_values.js b/documentation/js/multiple_return_values.js
index 3345d42e..f46b21dc 100644
--- a/documentation/js/multiple_return_values.js
+++ b/documentation/js/multiple_return_values.js
@@ -1,10 +1,8 @@
-(function(){
- var _a, city, forecast, temp, weatherReport;
- weatherReport = function(location) {
- return [location, 72, "Mostly Sunny"];
- };
- _a = weatherReport("Berkeley, CA");
- city = _a[0];
- temp = _a[1];
- forecast = _a[2];
-})();
+var _a, city, forecast, temp, weatherReport;
+weatherReport = function(location) {
+ return [location, 72, "Mostly Sunny"];
+};
+_a = weatherReport("Berkeley, CA");
+city = _a[0];
+temp = _a[1];
+forecast = _a[2];
\ No newline at end of file
diff --git a/documentation/js/object_comprehensions.js b/documentation/js/object_comprehensions.js
index bd637117..b5505f3e 100644
--- a/documentation/js/object_comprehensions.js
+++ b/documentation/js/object_comprehensions.js
@@ -1,18 +1,16 @@
-(function(){
- var _a, _b, age, ages, child, yearsOld;
- var __hasProp = Object.prototype.hasOwnProperty;
- yearsOld = {
- max: 10,
- ida: 9,
- tim: 11
- };
- ages = (function() {
- _a = []; _b = yearsOld;
- for (child in _b) {
- if (!__hasProp.call(_b, child)) continue;
- age = _b[child];
- _a.push(child + " is " + age);
- }
- return _a;
- })();
-})();
+var _a, _b, age, ages, child, yearsOld;
+var __hasProp = Object.prototype.hasOwnProperty;
+yearsOld = {
+ max: 10,
+ ida: 9,
+ tim: 11
+};
+ages = (function() {
+ _a = []; _b = yearsOld;
+ for (child in _b) {
+ if (!__hasProp.call(_b, child)) continue;
+ age = _b[child];
+ _a.push(child + " is " + age);
+ }
+ return _a;
+})();
\ No newline at end of file
diff --git a/documentation/js/object_extraction.js b/documentation/js/object_extraction.js
index eee7e448..b935daa4 100644
--- a/documentation/js/object_extraction.js
+++ b/documentation/js/object_extraction.js
@@ -1,17 +1,15 @@
-(function(){
- var _a, _b, _c, city, futurists, name, street;
- futurists = {
- sculptor: "Umberto Boccioni",
- painter: "Vladimir Burliuk",
- poet: {
- name: "F.T. Marinetti",
- address: ["Via Roma 42R", "Bellagio, Italy 22021"]
- }
- };
- _a = futurists;
- _b = _a.poet;
- name = _b.name;
- _c = _b.address;
- street = _c[0];
- city = _c[1];
-})();
+var _a, _b, _c, city, futurists, name, street;
+futurists = {
+ sculptor: "Umberto Boccioni",
+ painter: "Vladimir Burliuk",
+ poet: {
+ name: "F.T. Marinetti",
+ address: ["Via Roma 42R", "Bellagio, Italy 22021"]
+ }
+};
+_a = futurists;
+_b = _a.poet;
+name = _b.name;
+_c = _b.address;
+street = _c[0];
+city = _c[1];
\ No newline at end of file
diff --git a/documentation/js/objects_and_arrays.js b/documentation/js/objects_and_arrays.js
index 2906a153..66cc9011 100644
--- a/documentation/js/objects_and_arrays.js
+++ b/documentation/js/objects_and_arrays.js
@@ -1,10 +1,12 @@
-(function(){
- var ages, matrix, song;
- song = ["do", "re", "mi", "fa", "so"];
- ages = {
- max: 10,
- ida: 9,
- tim: 11
- };
- matrix = [1, 0, 1, 0, 0, 1, 1, 1, 0];
-})();
+var ages, matrix, singers, song;
+song = ["do", "re", "mi", "fa", "so"];
+singers = {
+ Jagger: 'Rock',
+ Elvis: 'Roll'
+};
+ages = {
+ max: 10,
+ ida: 9,
+ tim: 11
+};
+matrix = [1, 0, 1, 0, 0, 1, 1, 1, 0];
\ No newline at end of file
diff --git a/documentation/js/objects_reserved.js b/documentation/js/objects_reserved.js
index 57552aff..6fd6fea2 100644
--- a/documentation/js/objects_reserved.js
+++ b/documentation/js/objects_reserved.js
@@ -1,5 +1,3 @@
-(function(){
- $('.account').css({
- 'class': 'active'
- });
-})();
+$('.account').css({
+ 'class': 'active'
+});
\ No newline at end of file
diff --git a/documentation/js/overview.js b/documentation/js/overview.js
index dea69bcc..cfb3aba7 100644
--- a/documentation/js/overview.js
+++ b/documentation/js/overview.js
@@ -1,37 +1,34 @@
-(function(){
- var _a, _b, _c, _d, cubes, list, math, num, number, opposite, race, square;
- var __slice = Array.prototype.slice;
- number = 42;
- opposite = true;
- if (opposite) {
- number = -42;
+var _a, _b, _c, _d, cubes, list, math, num, number, opposite, race, square;
+var __slice = Array.prototype.slice;
+number = 42;
+opposite = true;
+if (opposite) {
+ number = -42;
+}
+square = function(x) {
+ return x * x;
+};
+list = [1, 2, 3, 4, 5];
+math = {
+ root: Math.sqrt,
+ square: square,
+ cube: function(x) {
+ return x * square(x);
}
- square = function(x) {
- return x * x;
- };
- list = [1, 2, 3, 4, 5];
- math = {
- root: Math.sqrt,
- square: square,
- cube: function(x) {
- return x * square(x);
- }
- };
- race = function(winner) {
- var runners;
- var _a = arguments.length, _b = _a >= 2;
- runners = __slice.call(arguments, 1, _a - 0);
- return print(winner, runners);
- };
- if (typeof elvis !== "undefined" && elvis !== null) {
- alert("I knew it!");
+};
+race = function(winner) {
+ var runners;
+ runners = __slice.call(arguments, 1);
+ return print(winner, runners);
+};
+if (typeof elvis !== "undefined" && elvis !== null) {
+ alert("I knew it!");
+}
+cubes = (function() {
+ _a = []; _c = list;
+ for (_b = 0, _d = _c.length; _b < _d; _b++) {
+ num = _c[_b];
+ _a.push(math.cube(num));
}
- cubes = (function() {
- _a = []; _c = list;
- for (_b = 0, _d = _c.length; _b < _d; _b++) {
- num = _c[_b];
- _a.push(math.cube(num));
- }
- return _a;
- })();
-})();
+ return _a;
+})();
\ No newline at end of file
diff --git a/documentation/js/parallel_assignment.js b/documentation/js/parallel_assignment.js
index c231e3f3..f1f7b923 100644
--- a/documentation/js/parallel_assignment.js
+++ b/documentation/js/parallel_assignment.js
@@ -1,8 +1,6 @@
-(function(){
- var _a, theBait, theSwitch;
- theBait = 1000;
- theSwitch = 0;
- _a = [theSwitch, theBait];
- theBait = _a[0];
- theSwitch = _a[1];
-})();
+var _a, theBait, theSwitch;
+theBait = 1000;
+theSwitch = 0;
+_a = [theSwitch, theBait];
+theBait = _a[0];
+theSwitch = _a[1];
\ No newline at end of file
diff --git a/documentation/js/patterns_and_splats.js b/documentation/js/patterns_and_splats.js
index 7aa30dc5..dfe1081f 100644
--- a/documentation/js/patterns_and_splats.js
+++ b/documentation/js/patterns_and_splats.js
@@ -1,9 +1,7 @@
-(function(){
- var _a, close, contents, open, tag;
- var __slice = Array.prototype.slice;
- tag = " CoffeeScript on the left, compiled JavaScript output on the right.
For a longer CoffeeScript example, check out
@@ -441,8 +437,8 @@ coffee --print app/scripts/*.coffee > concatenation.js
Functions are defined by a list of parameters, an arrow, and the
function body. The empty function looks like this: ->
If you'd like to assign a function literal to a variable, but not have
it be named, just wrap the function definition in parentheses:
@@ -472,15 +467,14 @@ cube = function(x) {
mathy things. While colons are preferred, the two may be used interchangeably,
even within object literals.
All declaration of new variables is pushed up to the top of the nearest
lexical scope, so that assignment may always be performed within expressions.
@@ -495,42 +489,50 @@ difficulty = 0.5;
assigning local variables, and can be moved around freely. Feel free to mix
and match the two styles.
In JavaScript, you can't use reserved words, like class, as properties
of an object, without quoting them as strings. CoffeeScript notices and quotes
them for you, so you don't have to worry about it (say, when using jQuery).
Notice how the all of the variable declarations have been pushed up to
the top of the closest scope, the first time they appear.
@@ -611,15 +610,17 @@ inner = changeNumbers();
is no explicit ternary statement in CoffeeScript — you simply use
a regular if statement inline.
You can assign a variable to a half-expression to perform an operation
@@ -675,13 +678,13 @@ options = options || d
@@ -782,8 +782,8 @@ alert("The Field: " + rest);
sell() until supply > demand
# Nursery Rhyme
-num: 6
-lyrics: while num: - 1
+num = 6
+lyrics = while num -= 1
num + " little monkeys, jumping on the bed.
One fell out and bumped his head."
For readability, the until keyword is equivalent to while not,
and the loop keyword is equivalent to while true.
@@ -843,7 +842,7 @@ One fell out and bumped his head.");
would use a loop, each/forEach, map, or select/filter.
Comprehensions can also be used to iterate over the keys and values in
an object. Use of to signal comprehension over the properties of
an object instead of the values in an array.
@@ -975,11 +972,11 @@ ages = (function() {
the slice, and the second is the index of the last one. Three dots signify
a range that excludes the end.
The same syntax can be used with assignment to replace a segment of an
array with new values (to splice it).
@@ -1017,7 +1012,7 @@ numbers.splice.apply(numbers, [3, 6 - 3 + 1].concat([-3, -4, -5, -6]));
pushed down into each possible branch of execution, in the function
below.
Even though functions will always return their final value, it's both possible
and encouraged to return early from a function body writing out the explicit
@@ -1058,12 +1052,11 @@ eldest = 24 > 21 ? "Liz" : "Ike";
Because variable declarations occur at the top of scope, assignment can
be used within expressions, even for variables that haven't been seen before:
Things that would otherwise be statements in JavaScript, when used
as part of an expression in CoffeeScript, are converted into expressions
@@ -1072,28 +1065,29 @@ six = (one = 1) + (two = 2) + (three = 3);
As well as silly things, like passing a try/catch statement directly
into a function call:
@@ -1117,8 +1111,7 @@ globals = (function() {
} catch (error) {
return "And the error is ... " + error;
}
-})());
-;'>run
There are a handful of statements in JavaScript that can't be meaningfully
converted into expressions, namely break, continue,
@@ -1139,7 +1132,7 @@ globals = (function() {
It can also be used for safer conditional assignment than ||=
provides, for cases where you may be handling numbers or strings.
The accessor variant of the existential operator ?. can be used to soak
up null references in a chain of properties. Use it instead
@@ -1199,27 +1191,23 @@ speed = (typeof speed !== "undefined" && speed !== null) ? speed : 140;
in a single assignable expression.
If structuring your prototypes classically isn't your cup of tea, CoffeeScript
provides a couple of lower-level conveniences. The extends operator
@@ -1305,15 +1294,14 @@ tom.move();
quick access to an object's prototype, and super()
is converted into a call against the immediate ancestor's method of the same name.
Finally, you may assign Class-level (static) properties within a class
definition by using
But it's also helpful for dealing with functions that return multiple
values.
Pattern matching can be used with any depth of array and object nesting,
to help pull out deeply nested properties.
Pattern matching can even be combined with splats.
@@ -1465,24 +1447,22 @@ close = _a[_a.length - 1];
to use with bind. Functions created with the fat arrow are able to access
properties of the this where they're defined.
@@ -1498,20 +1478,19 @@ close = _a[_a.length - 1];
snippets of JavaScript within your CoffeeScript, you can
use backticks to pass it straight through.
@@ -1588,9 +1567,9 @@ return [document.title, "Hello JavaScript"].join(": ");
from Python — making it easy to test if a value falls within a
certain range.
@@ -1608,41 +1586,39 @@ healthy = (200 > cholesterol) && (cholesterol > 60);
is included in CoffeeScript. Simple variables can be included by marking
them with a dollar sign.
And arbitrary expressions can be interpolated by using brackets ${ ... }
Multiline Strings, Heredocs, and Block Comments
Multiline strings are allowed in CoffeeScript.
Heredocs can be used to hold formatted or indentation-sensitive text
(or, if you just don't feel like escaping quotes and apostrophes). The
indentation level that begins the heredoc is maintained throughout, so
you can keep it all aligned with the body of your code.
- Sometimes you'd like to pass a block comment through to the generated
- JavaScript. For example, when you need to embed a licensing header at
- the top of a file. Block comments, which mirror the synax for heredocs,
+ Sometimes you'd like to pass a block comment through to the generated
+ JavaScript. For example, when you need to embed a licensing header at
+ the top of a file. Block comments, which mirror the synax for heredocs,
are preserved in the generated code.
- CoffeeScript includes a simple build system similar to
- Make and
+ CoffeeScript includes a simple build system similar to
+ Make and
Rake. Naturally,
it's called Cake, and is used for the build and test tasks for the CoffeeScript
language itself. Tasks are defined in a file named Cakefile, and
@@ -1721,15 +1696,15 @@ html = '<
be made available in the options object. Here's a task that uses
the Node.js API to rebuild CoffeeScript's parser:
# Assignment:
-number: 42
-opposite: true
+number = 42
+opposite = true
# Conditions:
-number: -42 if opposite
+number = -42 if opposite
# Functions:
-square: (x) -> x * x
+square = (x) -> x * x
# Arrays:
-list: [1, 2, 3, 4, 5]
+list = [1, 2, 3, 4, 5]
# Objects:
-math: {
+math =
root: Math.sqrt
square: square
cube: (x) -> x * square x
-}
# Splats:
-race: (winner, runners...) ->
+race = (winner, runners...) ->
print winner, runners
# Existence:
alert "I knew it!" if elvis?
# Array comprehensions:
-cubes: math.cube num for num in list
+cubes = math.cube num for num in list
var _a, _b, _c, _d, cubes, list, math, num, number, opposite, race, square;
var __slice = Array.prototype.slice;
number = 42;
@@ -174,8 +173,7 @@ math = {
};
race = function(winner) {
var runners;
- var _a = arguments.length, _b = _a >= 2;
- runners = __slice.call(arguments, 1, _a - 0);
+ runners = __slice.call(arguments, 1);
return print(winner, runners);
};
if (typeof elvis !== "undefined" && elvis !== null) {
@@ -209,8 +207,7 @@ math = {
};
race = function(winner) {
var runners;
- var _a = arguments.length, _b = _a >= 2;
- runners = __slice.call(arguments, 1, _a - 0);
+ runners = __slice.call(arguments, 1);
return print(winner, runners);
};
if (typeof elvis !== "undefined" && elvis !== null) {
@@ -223,8 +220,7 @@ cubes = (function() {
_a.push(math.cube(num));
}
return _a;
-})();
-;alert(cubes);'>run: cubes
square: (x) -> x * x
-cube: (x) -> square(x) * x
+
square = (x) -> x * x
+cube = (x) -> square(x) * x
var cube, square;
square = function(x) {
return x * x;
@@ -456,8 +452,7 @@ square = function(x) {
};
cube = function(x) {
return square(x) * x;
-};
-;alert(cube(5));'>run: cube(5)
greeting: "Hello CoffeeScript"
-difficulty: 0.5
+
greeting = "Hello CoffeeScript"
+difficulty = 0.5
var difficulty, greeting;
greeting = "Hello CoffeeScript";
difficulty = 0.5;
song: ["do", "re", "mi", "fa", "so"]
+
song = ["do", "re", "mi", "fa", "so"]
-ages: {
+singers = {Jagger: 'Rock', Elvis: 'Roll'}
+
+ages =
max: 10
ida: 9
tim: 11
-}
-matrix: [
+matrix = [
1, 0, 1
0, 0, 1
1, 1, 0
]
-
var ages, matrix, song;
+
var ages, matrix, singers, song;
song = ["do", "re", "mi", "fa", "so"];
+singers = {
+ Jagger: 'Rock',
+ Elvis: 'Roll'
+};
ages = {
max: 10,
ida: 9,
tim: 11
};
matrix = [1, 0, 1, 0, 0, 1, 1, 1, 0];
-
$('.account').css {class: 'active'}
+
$('.account').css class: 'active'
$('.account').css({
'class': 'active'
});
@@ -543,18 +545,17 @@ matrix = [1, 0, 1, 0, 0, 1, 1, 1, 0];
are properly declared within lexical scope — you never need to write
var yourself.
-
outer: 1
-changeNumbers: ->
- inner: -1
- outer: 10
-inner: changeNumbers()
+
outer = 1
+changeNumbers = ->
+ inner = -1
+ outer = 10
+inner = changeNumbers()
var changeNumbers, inner, outer;
outer = 1;
changeNumbers = function() {
var inner;
inner = -1;
- outer = 10;
- return outer;
+ return (outer = 10);
};
inner = changeNumbers();
mood: greatlyImproved if singing
+
mood = greatlyImproved if singing
if happy and knowsIt
clapsHands()
chaChaCha()
+else
+ showIt()
-date: if friday then sue else jill
+date = if friday then sue else jill
-options: or defaultOptions
+options = or defaults
var date, mood, options;
if (singing) {
mood = greatlyImproved;
@@ -627,9 +628,11 @@ options: or defaultOpt
if (happy && knowsIt) {
clapsHands();
chaChaCha();
+} else {
+ showIt();
}
date = friday ? sue : jill;
-options = options || defaultOptions;
+options = options || defaults;
launch() if ignition is on
-volume: 10 if band isnt SpinalTap
+volume = 10 if band isnt SpinalTap
letTheWildRumpusBegin() unless answer is no
if car.speed < limit then accelerate()
-winner: yes if pick in [47, 92, 13]
+winner = yes if pick in [47, 92, 13]
print "My name is " + @name
var volume, winner;
@@ -709,14 +712,14 @@ car.speed < limit ? accelerate() : gold: silver: rest: "unknown"
+
gold = silver = rest = "unknown"
-awardMedals: (first, second, rest...) ->
- gold: first
- silver: second
- rest: rest
+awardMedals = (first, second, others...) ->
+ gold = first
+ silver = second
+ rest = others
-contenders: [
+contenders = [
"Michael Phelps"
"Liu Xiang"
"Yao Ming"
@@ -738,12 +741,11 @@ alert "The Field: var __slice = Array.prototype.slice;
gold = (silver = (rest = "unknown"));
awardMedals = function(first, second) {
- var _a = arguments.length, _b = _a >= 3;
- rest = __slice.call(arguments, 2, _a - 0);
+ var others;
+ others = __slice.call(arguments, 2);
gold = first;
silver = second;
- rest = rest;
- return rest;
+ return (rest = others);
};
contenders = ["Michael Phelps", "Liu Xiang", "Yao Ming", "Allyson Felix", "Shawn Johnson", "Roman Sebrle", "Guo Jingjing", "Tyson Gay", "Asafa Powell", "Usain Bolt"];
awardMedals.apply(this, contenders);
@@ -754,19 +756,17 @@ awardMedals.apply(th
var __slice = Array.prototype.slice;
gold = (silver = (rest = "unknown"));
awardMedals = function(first, second) {
- var _a = arguments.length, _b = _a >= 3;
- rest = __slice.call(arguments, 2, _a - 0);
+ var others;
+ others = __slice.call(arguments, 2);
gold = first;
silver = second;
- rest = rest;
- return rest;
+ return (rest = others);
};
contenders = ["Michael Phelps", "Liu Xiang", "Yao Ming", "Allyson Felix", "Shawn Johnson", "Roman Sebrle", "Guo Jingjing", "Tyson Gay", "Asafa Powell", "Usain Bolt"];
awardMedals.apply(this, contenders);
alert("Gold: " + gold);
alert("Silver: " + silver);
-alert("The Field: " + rest);
-;'>run
var _a, lyrics, num;
@@ -821,8 +821,7 @@ lyrics = (function() {
One fell out and bumped his head.");
}
return _a;
-})();
-;alert(lyrics.join("\n"));'>run: lyrics.join("\n")
# Eat lunch.
-lunch: eat food for food in ['toast', 'cheese', 'wine']
+lunch = eat food for food in ['toast', 'cheese', 'wine']
# Naive collision detection.
for roid in asteroids
@@ -877,12 +876,12 @@ _f = asteroids;
in fixed-size increments, you can use a range to specify the start and
end of your comprehension.
-
countdown: num for num in [10..1]
+
countdown = num for num in [10..1]
-deliverEggs: ->
+deliverEggs = ->
for i in [0...eggs.length] by 12
- dozen: eggs[i...i+12]
- deliver new eggCarton(dozen)
+ dozen = eggs[i...i+12]
+ deliver new eggCarton dozen
var _a, countdown, deliverEggs, num;
countdown = (function() {
_a = [];
@@ -920,16 +919,15 @@ deliverEggs = function() {
})());
}
return _b;
-};
-;alert(countdown);'>run: countdown
yearsOld: {max: 10, ida: 9, tim: 11}
+
yearsOld = max: 10, ida: 9, tim: 11
-ages: for child, age of yearsOld
+ages = for child, age of yearsOld
child + " is " + age
var _a, _b, age, ages, child, yearsOld;
var __hasProp = Object.prototype.hasOwnProperty;
@@ -962,8 +960,7 @@ ages = (function() {
_a.push(child + " is " + age);
}
return _a;
-})();
-;alert(ages.join(", "));'>run: ages.join(", ")
numbers: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
+
numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
-threeToSix: numbers[3..6]
+threeToSix = numbers[3..6]
-copy: numbers[0...numbers.length]
+copy = numbers[0...numbers.length]
var copy, numbers, threeToSix;
numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
@@ -988,15 +985,14 @@ copy = numbers.slice<
numbers: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
+
numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
-numbers[3..6]: [-3, -4, -5, -6]
+numbers[3..6] = [-3, -4, -5, -6]
var numbers;
@@ -1004,8 +1000,7 @@ numbers = [0, apply(numbers, [3, 6 - 3 + 1].concat([-3, -4, -5, -6]));
grade: (student) ->
+
grade = (student) ->
if student.excellentWork
"A+"
else if student.okayStuff
@@ -1025,7 +1020,7 @@ numbers.splice.apply(numbers, [3, 6 - 3 + 1].concat([-3, -4, -5, -6]));
else
"C"
-eldest: if 24 > 21 then "Liz" else "Ike"
+eldest = if 24 > 21 then "Liz" else "Ike"
var eldest, grade;
grade = function(student) {
if (student.excellentWork) {
@@ -1047,8 +1042,7 @@ grade = function(student) {
return "C";
}
};
-eldest = 24 > 21 ? "Liz" : "Ike";
-;alert(eldest);'>run: eldest
six: (one: 1) + (two: 2) + (three: 3)
+
six = (one = 1) + (two = 2) + (three = 3)
var one, six, three, two;
six = (one = 1) + (two = 2) + (three = 3);
# The first ten global properties.
-globals: (name for name of window)[0...10]
-
var _a, _b, globals, name;
+globals = (name for name of window)[0...10]
+
var _a, _b, _c, globals, name;
var __hasProp = Object.prototype.hasOwnProperty;
globals = (function() {
- _a = []; _b = window;
- for (name in _b) {
- if (!__hasProp.call(_b, name)) continue;
- _a.push(name);
+ _b = []; _c = window;
+ for (name in _c) {
+ if (!__hasProp.call(_c, name)) continue;
+ _a = _c[name];
+ _b.push(name);
}
- return _a;
+ return _b;
})().slice(0, 10);
-
solipsism: true if mind? and not world?
+
solipsism = true if mind? and not world?
speed ?= 140
@@ -1156,8 +1149,7 @@ speed = (typeof speed
if ((typeof mind !== "undefined" && mind !== null) && !(typeof world !== "undefined" && world !== null)) {
solipsism = true;
}
-speed = (typeof speed !== "undefined" && speed !== null) ? speed : 140;
-;alert(speed);'>run: speed
class Animal
+ constructor: (@name) ->
+
move: (meters) ->
alert @name + " moved " + meters + "m."
class Snake extends Animal
- constructor: (name) ->
- @name: name
-
move: ->
alert "Slithering..."
super 5
class Horse extends Animal
- constructor: (name) ->
- @name: name
-
move: ->
alert "Galloping..."
super 45
-sam: new Snake "Sammy the Python"
-tom: new Horse "Tommy the Palomino"
+sam = new Snake "Sammy the Python"
+tom = new Horse "Tommy the Palomino"
sam.move()
tom.move()
@@ -1229,29 +1217,30 @@ tom.move()
var Animal, Horse, Snake, sam, tom;
var __extends = function(child, parent) {
- var ctor = function(){ };
- ctor.prototype = parent.prototype;
- child.prototype = new ctor();
- child.prototype.constructor = child;
- if (typeof parent.extended === "function") parent.extended(child);
- child.__superClass__ = parent.prototype;
+ var ctor = function(){};
+ ctor.prototype = parent.prototype;
+ child.prototype = new ctor();
+ child.prototype.constructor = child;
+ if (typeof parent.extended === "function") parent.extended(child);
+ child.__superClass__ = parent.prototype;
+ };
+Animal = function(_a) {
+ this.name = _a;
+ return this;
};
-Animal = function() {};
Animal.prototype.move = function(meters) {
return alert(this.name + " moved " + meters + "m.");
};
-Snake = function(name) {
- this.name = name;
- return this;
+Snake = function() {
+ return Animal.apply(this, arguments);
};
__extends(Snake, Animal);
Snake.prototype.move = function() {
alert("Slithering...");
return Snake.__superClass__.move.call(this, 5);
};
-Horse = function(name) {
- this.name = name;
- return this;
+Horse = function() {
+ return Animal.apply(this, arguments);
};
__extends(Horse, Animal);
Horse.prototype.move = function() {
@@ -1264,29 +1253,30 @@ sam.move();
tom.move();
String::dasherize: ->
- this.replace(/_/g, "-")
+
String::dasherize = ->
+ this.replace /_/g, "-"
String.prototype.dasherize = function() {
return this.replace(/_/g, "-");
};
@property: value
@@ -1330,10 +1318,10 @@ tom.move();
on the right to the variables on the left. In the simplest case, it can be
used for parallel assignment:
theBait: 1000
-theSwitch: 0
+
theBait = 1000
+theSwitch = 0
-[theBait, theSwitch]: [theSwitch, theBait]
+[theBait, theSwitch] = [theSwitch, theBait]
var _a, theBait, theSwitch;
theBait = 1000;
theSwitch = 0;
@@ -1345,17 +1333,16 @@ theBait = 1000;
theSwitch = 0;
_a = [theSwitch, theBait];
theBait = _a[0];
-theSwitch = _a[1];
-;alert(theBait);'>run: theBait
weatherReport: (location) ->
+
weatherReport = (location) ->
# Make an Ajax request to fetch the weather...
[location, 72, "Mostly Sunny"]
-[city, temp, forecast]: weatherReport "Berkeley, CA"
+[city, temp, forecast] = weatherReport "Berkeley, CA"
var _a, city, forecast, temp, weatherReport;
weatherReport = function(location) {
return [location, 72, "Mostly Sunny"];
@@ -1371,25 +1358,22 @@ weatherReport = function(location) {
_a = weatherReport("Berkeley, CA");
city = _a[0];
temp = _a[1];
-forecast = _a[2];
-;alert(forecast);'>run: forecast
futurists: {
+
futurists =
sculptor: "Umberto Boccioni"
painter: "Vladimir Burliuk"
- poet: {
+ poet:
name: "F.T. Marinetti"
address: [
"Via Roma 42R"
"Bellagio, Italy 22021"
]
- }
-}
-{poet: {name, address: [street, city]}}: futurists
+{poet: {name, address: [street, city]}} = futurists
var _a, _b, _c, city, futurists, name, street;
futurists = {
sculptor: "Umberto Boccioni",
@@ -1419,14 +1403,13 @@ _b = _a.poet;
name = _b.name;
_c = _b.address;
street = _c[0];
-city = _c[1];
-;alert(name + " — " + street);'>run: name + " — " + street
tag: "<impossible>"
+
tag = "<impossible>"
-[open, contents..., close]: tag.split("")
+[open, contents..., close] = tag.split("")
@@ -1444,8 +1427,7 @@ tag = "
Account: (customer, cart) ->
- @customer: customer
- @cart: cart
+
Account = (customer, cart) ->
+ @customer = customer
+ @cart = cart
$('.shopping_cart').bind 'click', (event) =>
@customer.purchase @cart
var Account;
+var __bind = function(func, context) {
+ return function(){ return func.apply(context, arguments); };
+ };
Account = function(customer, cart) {
this.customer = customer;
this.cart = cart;
- return $('.shopping_cart').bind('click', (function(__this) {
- var __func = function(event) {
- return this.customer.purchase(this.cart);
- };
- return (function() {
- return __func.apply(__this, arguments);
- });
- })(this));
+ return $('.shopping_cart').bind('click', __bind(function(event) {
+ return this.customer.purchase(this.cart);
+ }, this));
};
hi: `function() {
+
hi = `function() {
return [document.title, "Hello JavaScript"].join(": ");
}`
var hi;
hi = function() {
-return [document.title, "Hello JavaScript"].join(": ");
+ return [document.title, "Hello JavaScript"].join(": ");
};
cholesterol: 127
+
cholesterol = 127
-healthy: 200 > cholesterol > 60
+healthy = 200 > cholesterol > 60
var cholesterol, healthy;
@@ -1598,8 +1577,7 @@ cholesterol = 127;
healthy = (200 > cholesterol) && (cholesterol > 60);
author: "Wittgenstein"
-quote: "A picture is a fact. -- $author"
+
author = "Wittgenstein"
+quote = "A picture is a fact. -- #author"
var author, quote;
author = "Wittgenstein";
quote = ("A picture is a fact. -- " + author);
Interpolation works the same way within regular expressions.
sentence: "${ 22 / 7 } is a decent approximation of π"
+
sentence = "#{ 22 / 7 } is a decent approximation of π"
-sep: "[.\\/\\- ]"
-dates: /\d+$sep\d+$sep\d+/g
+sep = "[.\\/\\- ]"
+dates = /\d+$sep\d+$sep\d+/g
var dates, sentence, sep;
sentence = ("" + (22 / 7) + " is a decent approximation of π");
sep = "[.\\/\\- ]";
-dates = (new RegExp(("\\d+" + sep + "\\d+" + sep + "\\d+"), "g"));
+dates = /\d+$sep\d+$sep\d+/g;
mobyDick: "Call me Ishmael. Some years ago --
+
mobyDick = "Call me Ishmael. Some years ago --
never mind how long precisely -- having little
or no money in my purse, and nothing particular
to interest me on shore, I thought I would sail
@@ -1663,19 +1639,18 @@ never mind how long precisely -- having little \
or no money in my purse, and nothing particular \
to interest me on shore, I thought I would sail \
about a little and see the watery part of the \
-world...";
-;alert(mobyDick);'>run: mobyDick
html: '''
- <strong>
- cup of coffeescript
- </strong>
- '''
+
html = '''
+ <strong>
+ cup of coffeescript
+ </strong>
+ '''
var html;
html = '<strong>\n cup of coffeescript\n</strong>';
###
@@ -1704,8 +1679,8 @@ html = '<
fs: require 'fs'
+
fs = require 'fs'
option '-o', '--output [DIR]', 'directory for compiled code'
task 'build:parser', 'rebuild the Jison parser', ->
require 'jison'
- code: require('./lib/grammar').parser.generate()
- dir: options.output or 'lib'
- fs.writeFile "$dir/parser.js", code
+ code = require('./lib/grammar').parser.generate()
+ dir = options.output or 'lib'
+ fs.writeFile "#dir/parser.js", code
var fs;
fs = require('fs');
option('-o', '--output [DIR]', 'directory for compiled code');