9. OS X
Install Homebrew
brew install node
curl http://npmjs.org/install.sh | sh
npm install coffee-script
coffee -v
http://mxcl.github.com/homebrew/
10. Linux*
install node and npm
(http://joyeur.com/2010/12/10/installing-node-and-npm/ )
npm install coffee-script
coffee -v
(* Windoze people can uze these instructions on Cygwin)
21. book = "I Am America (And So Can You!)"
var book;
book = "I Am America (And So Can You!)";
JavaScript
http://www.amazon.com/Am-America-So-Can-You/dp/0446580503
34. CoffeeScript JavaScript
==, is ===
!=, isnt !===
and &&
or ||
of in
in (no equivalent)
@, this this
true, yes, on true
false, no, off false
35. goodStudent = yes if grade in ['A', 'B']
stop() unless greenLight is on
if (book is "1984")
removeFromAllKindles()
36. Existential operator
believer = if aliens? then yes else no
console.log "I Believe" if believer
options ?= {}
options or= defaults
customer?.contactInfo?.emailAddress
39. Loops return a value
a = [1, 2, 3, 4, 5]
timesTwo = for val in a
val * 2
=> [2, 4, 6, 8, 10 ]
40. Loops do not create scope..
countdown = [10..0]
for num in countdown
break if errorDetected()
if num is 0
console.log 'Blast-off!'
else
console.log "Aborted with #{num} seconds left!"
42. Iterating objects with "for
own" (hasOwnProperty)
Human = ->
Human::species = 'Homo-Sapiens'
ceo = new Human()
ceo.name = "Chris D'Agostino"
ceo.company = 'Near Infinity'
ceo.yearFounded = 2002
for own prop, val of ceo
console.log "#{prop} = #{val}"
48. Array Comprehensions
foods = ['pizza', 'soda', 'beer']
consume food for food in foods
consume food if food is 'beer' for food in foods
consume food for food in foods when food is 'beer'
49. nums = [1..10]
doubles = (n * 2 for n in nums)
squares = (n * n for n in nums)
evens = (n for n in nums when n % 2 is 0)
evensSquared = (n * n for n in evens)
57. Functions are bound or unbound
Functions create scope
Variable declarations are pushed to the top of the
closest scope
Variables are not visible outside declared scope
58. weirdAl = function() {
a = a + 1;
// What's a's value at this point?
var a = 10;
return a;
}
result = weirdAl();
JavaScript
69. Prototypal inheritance
var Animal = function(name, species) {
this.name = name;
this.species = species;
}
Animal.prototype.eat = function(food) { ... }
Animal.prototype.sleep = function() { ... }
Animal.prototype.play = function() { ... }
var felix = new Animal('Felix', 'Feline');
felix.sleep();
var fido = new Animal('Fido', 'Canine');
fido.play();
fido.eat();
JavaScript
70. class Bike
constructor: (@brand) ->
@gear = 1
changeGear: (newGear) ->
@gear = newGear
currentGear: ->
@gear
status: ->
"#{@brand} cruising in gear #{@gear}"
71. class MtnBike extends Bike
changeGear: (newGear) ->
# do something special for mtn bikes...
super newGear
cruiser = new Bike("Schwinn")
cruiser.changeGear(4)
console.log cruiser.status()
mtn = new MtnBike("Specialized")
mtn.changeGear(8)
console.log mtn.status()