MENU
CoffeeScript
Alexa's Ranking: 324,036
CoffeeScript attempts to shorten and simplify the syntax of JavaScript.
# Assignment:number
= 42opposite = true# Conditions:number = -42 if opposite# Functions:square = (x) -> x * x# Arrays:list = [1, 2, 3, 4, 5]# Objects:math =
root:
Math.sqrt
square: square
cube:
(x) -> x * square x# Splats:race = (winner, runners...) ->
print winner, runners# Existence:alert "I knew it!" if elvis?# Array comprehensions:cubes = (math.cube num for num in list)
compiles onto:
// Generated by CoffeeScript 2.5.1(function() { // Assignment:
var cubes, list, math, num, number, opposite, race, square;
number = 42;
opposite = true;
if (opposite) { // Conditions:
number = -42;
} // Functions:
square = function(x) {
return x * x;
}; // Arrays:
list = [1, 2, 3, 4, 5]; // Objects:
math = {
root: Math.sqrt,
square: square,
cube: function(x) {
return x * square(x);
}
}; // Splats:
race = function(winner, ...runners) {
return print(winner, runners);
};
if (typeof elvis !== "undefined" && elvis !== null) { // Existence:
alert("I knew it!");
} // Array comprehensions:
cubes = (function() {
var i, len, results;
results = [];
for (i = 0, len = list.length; i < len; i++) {
num = list[i];
results.push(math.cube(num));
}
return results;
})();}).call(this);
To install the CoffeeScript compiler:
> npm install --global coffeescript
To watch for changes in .coffee files in src/ and automatically compile them onto lib/:
>coffee --watch --compile --output lib/ src/