2. What is CoffeeScript?
• Exposes the good parts
• Transpiles directly to JS
• Removes / Adds new syntax
• Enhances readability, and productivity
• Clearly expresses intents
3. Agenda
• An Emotional Assessment
• Dive Into Features
• Real World Experiences
• Challenges / Critiques
19. Vars are long gone
var count, increment;
JAVASCRIPT
count = 0;
increment = function() {
var total;
count += 1;
return total = "The total is: " + count;
};
count = 0
COFFEESCRIPT
increment = ->
count += 1
total = "The total is: " + count
36. Operator Comparison
COFFEESCRIPT
JAVASCRIPT
is
===
true is true
isnt
!==
@name isnt "randall"
not
!
and
&&
or
||
true yes
on
true
false no
off
false
relax() if not working()
exercise() unless @tired and @sleepy
happy() if @happy or @energy
> 50
40. No more awkward ‘+’
JAVASCRIPT
"Hey, " + person.name + " is " + person.age + " years young.";
COFFEESCRIPT
"Hey, #{person.name} is #{person.age} years young."
48. No Control Expression
COFFEESCRIPT
energyLevel = switch
when @energy < 10 then "exhausted"
when @energy < 20 then "okay"
when @energy < 40 then "caffeinated"
else "bouncing off walls"
JAVASCRIPT
var energyLevel;
energyLevel = (function() {
switch (false) {
case !(this.energy < 10):
return "exhausted";
case !(this.energy < 20):
return "okay";
case !(this.energy < 40):
return "caffeinated";
default:
return "bouncing off walls";
}
}).call(this);
50. For Loops
person =
name: "Toby Ho"
language: "javascript"
likes: ["node", "testem", "jazz"]
COFFEESCRIPT
for like, num in person.likes
console.log "#{num + 1}. #{like}"
var like, num, _i, _len, _ref;
=>
1. node
2. testem
3. jazz
JAVASCRIPT
_ref = person.likes;
for (num = _i = 0, _len = _ref.length; _i < _len; num = ++_i) {
like = _ref[num];
console.log("" + (num + 1) + ". " + like);
}
51. Property Iteration
heros = { john: "resig", ryan: "dahl" };
COFFEESCRIPT
for first, last of heros
console.log first, last
var first, last;
JAVASCRIPT
for (first in heros) {
last = heros[first];
console.log(first, last);
}
=>
john resig
ryan dahl
52. Array ‘in’ checking
person =
name: "Toby Ho"
dislikes: ["frameworks", "long plane rides"]
COFFEESCRIPT
if "coffeescript" in person.dislikes then "hater" else "friend :-)"
var __indexOf = [].indexOf || function(item) { for (var i = 0, l
= this.length; i < l; i++) { if (i in this && this[i] === item)
return i; } return -1; };
if (__indexOf.call(this.dislikes, "coffeescript") >= 0) {
"hater";
} else {
"friend :-)";
}
JAVASCRIPT
53. Comprehensions
{100 Objects}
COFFEESCRIPT
nums = (num: i for i in [1..100])
var i, nums;
=>
JAVASCRIPT
nums = (function() {
var _i, _results;
_results = [];
for (i = _i = 1; _i <= 100; i = ++_i) {
_results.push({
num: i
});
}
return _results;
})();
-------------------------[{
num: 1
},{
num: 2
}]
54. What was missed?
•
• Block Regular Expressions
Some Operators
•
• Chained Comparisons
Trailing Commas
•
• Reserved Words
Literate Coffeescript
•
• Classes, Inheritance, & Super
Block Strings