Going over the basics of CoffeeScript. Given at BarCampRochester and BuffaloJS.
RRCoffeeScript@qrushBarCampRoc Apr 2013Spartan JavaScript
View Slide
JavaScript as alanguage isNotVerbose
Good JS isOO
Good JS isConcise
CoffeeScript is“Just JS”
CoffeeScript writesGood JS
CoffeeScript isSpartan!
a new battle!http://www.flickr.com/photos/pshab/1578426589/
the closest I’vefelt to thepower I had 20years ago inSmalltalk~Ward Cunningham
it was the firsttime Iexperiencedlanguage envy~DHH
I useboth~Brendan Eich
function foo() {}
function foo() funcfoo() function foo(function foo() funcfoo() function foo(function foo() funcfoo() function foo(function foo() func
this
foo = function() {return 1 + 1;}
foo = () ->1 + 1
bar = function(x) {return x + 1;}
bar = (x) ->x + 1
_.bind(fn, obj)http://underscorejs.org/#bindhttp://coffeescript.org#fat_arrow
baz = () =>this.foo()
foz = () =>@foo()
$ ->new ItemsRouter()Backbone.history.start()
morefeatures
indentsarescope
laststatementsimplicitlyreturn
all varslocal bydefault
easierobjectsyntax
title: "Sparta!"author:name: "Leonidas"handle: "theking"apples: 1
stringinterpolation
var html = "" +this.get("title") +"";
"#{@get("title")}"
inheritanceactuallyworks
class Item extends Backbone.Modelurl: ->"/events/#{@id}"
loops are lesspainful
nums = [4, 5, 6]for i in numsconsole.log i456
comprehensionscompactcode
nums = [1, 2, 3][ 1, 2, 3 ]sq = (n * n for n in nums)[ 1, 4, 9 ]
conditionalsand loopscan beinline
console.log("Sparta!") if trueSparta!console.log("Persia!") unless falseSparta!
x = 66x += 1 while x < 10[ 7, 8, 9, 10 ]x10
getexistential
spear?falsespear = -> "Throw!"[Function]spear?true
window.bcx ?= {}{}
startWaiting: ->@waitTimeout ?= setTimeout =>@$container.addClass "waiting", 300stopWaiting: ->if @waitTimeout?clearTimeout @waitTimeout@waitTimeout = null@$container.removeClass "waiting"
meet yourenemies
making coffee• Rails:http://guides.rubyonrails.org/asset_pipeline.html• Jekyll & Rake:https://gist.github.com/4496420• Node:npm install coffee• Anything else: Less.apphttp://incident57.com/less/
debuggingis notterrible
http://alexspeller.com/
somesillykeywords
yes, true, onno, false, off
more info• http://coffeescript.org• http://coffeescript.codeschool.com/• http://robots.thoughtbot.com/post/9251081564/coffeescript-spartan-javascript