2. CoffeeScriptis
• “A littlelanguangethat compiles into
JavaScript”
• Easiertoreadandwritethan JavaScript
• SyntacticallyclosetoRubyand Python
• Easytointegratewithexistingcode
• Just JavaScript afterall
3. Main Features
• Significant whitespace and highly expressive
• Bound functional literal and class sugar for prototypes
• Loop, comprehensions and both
• An existential operator
• Splats (variadic parameters)
• Default Parameters
• Heredocs, heregexes
• Automatic lexical scoping
4. Functions
square = (x) ->x * x
(input arguments) -> output definition
var square = function(x) {
return x * x;
}
8. Whitespace
var today;
today = 'tuesday';
if (today === 'tuesday') {
console.log('UXEG');
} else {
console.log('noop');
}
9. Loops and comprehensions
list = ['a', 'b', 'c']
console.log letter for letter in list
process =
good: 'good'
bad: 'bad'
console.log name for name of process
console.logi for i in [0..10]
10. List comprehensions
vari, letter, list, name, process, _i, _j, _len;
list = ['a', 'b', 'c'];
for (_i = 0, _len = list.length; _i< _len; _i++) {
letter = list[_i];
console.log(letter);
}
process = { good: 'good', bad: 'bad’ };
for (name in process) {
console.log(name);
}
for (i = _j = 0; _j<= 10; i = ++_j) {
console.log(i);
}
19. Building
coffee – a command line tool
cake – a simple build tool
coffee -w -o lib/ -jmergedSource.js-csrc/
-w – to watch for changes while we write
-o – to specify output location
-j – to merge all files into one
-c – to specify input location