This Talk
- Too many slides
- Some are for reference
- Save questions for the end
- Or talk fast
| z, ? | toggle help (this) |
| space, → | next slide |
| shift-space, ← | previous slide |
| d | toggle debug mode |
| ## <ret> | go to slide # |
| c, t | table of contents (vi) |
| f | toggle footer |
| r | reload slides |
| n | toggle notes |
| p | run preshow |
Brian Morearty
Jan. 5, 2012

Is CoffeeScript really so great? Why use it instead of good ol' JavaScript?


curl http://npmjs.org/install.sh | shnpm install -g coffee-script-g. This installs it globally.npm install -g git://github.com/rstacruz/js2coffee.git
# optional; must be *before* barista:
gem 'haml-rails'
# Only needed on Ruby 1.8:
gem "json"
gem "barista"bundle install
rails generate barista:install
Generates a config file
app/coffeescripts using the .coffee extension.public/javascripts.
Don't use .js.coffee
:coffeescript.%p hello, world.
:coffeescript
$ ->
initialize()
therubyracer to your Gemfile and add rake barista:brew
to your Capistrano script.therubyracer on prod and generate on the fly there.js2coffee to the Rescue$ js2coffee < emc2.js > emc2.coffee
coffee2js?Think hard.



+
+

=

(But it seems most heavily influenced by Ruby.)
pi = 3.14159console.log calc result
compiles to:
console.log(calc(result));redAlert()
fib = (n) ->
return n if n in [0,1]
fib(n-1) + fib(n-2)
result = fib(10)clearFlash = ->
$('.flash').html('')greeting = 'allo'
result = "You had me at #{greeting}"name = 'Inigo Montoya'
result = """
Hello. My name is #{name}.
You killed my father.
Prepare to die.
"""
Only double-quoted heredocs have string interpolation.
if milk.spilled()
$('#message').html("Don't cry")
result(speed) = ->
if speed > 65
"ticket"
else
"you're good"loadData = (page = 1) ->
$(data).load("/data?page=#{page}")movie = 'School of Rock'
compiles to:
var movie;
movie = 'School of Rock';cache = undefined
store: (value) ->
cache = value
retrieve: ->
cache
"This behavior is effectively identical to Ruby's scope for local variables."
All Output Code Is Wrapped in an Anonymous Function
// generated JavaScript goes in anon func
(function() {
# ur codez go hear
})();
# I want to access this from other files
window.regions = ['West','East',
'North','South']ifreturn true if foundunless and postfix unlessdie() unless lives > 0At least, as much as possible. break, continue, and return are not converted to expressions.
return.
if/then/else Is an Expressionlanguage = if serverside then 'ruby' else 'coffeescript'for Loop Syntax Is...for movie in ['The Princess Bride',
'West Side Story']
record movie
Use 'for own key, value of object' to loop over 'hasOwnProperty()' properties.
result = [ 1..5 ]
result = [ 1...5 ]
Like Ruby, two dots includes the last value and three dots excludes it.
@propertythis.propertythenif hasKey then enter()
?" Operator.nil?if expression
# not null, undefined, false,
# "", 0, or NaN
if expression?
# neither null nor undefined[oldMax, max] = [max, newMax]
[firstName, lastName] = getName()?."try or andand Gemperson = null
result = typeof(person?.name)
If ?. is called on null or undefined it returns undefined instead of raising TypeError. "Accessor variant of the existential operator."
person = {}
oldestKid = person.kids?[0]?.name?.first
result = typeof(oldestKid)http://coffeescript.org/ (try the playground)
http://js2coffee.org/ (two-way playground)
https://github.com/Sutto/barista
Brian Morearty
http://ilikestuffblog.com/
http://bmorearty.github.com/coffeescript-talk/
brian@morearty.org
@BMorearty on Twitter and GitHub
Cartoons by Brian, Rosey, and Elizabeth