CoffeeScript Cheat Sheet
by Dimitrios Mistriotis (dimitrios) via [Link]/336/cs/167/
Usage Higher Order (cont) Arrays
-c, --compile doubles = (twice x for x in [1..6]) [1..5] == [1, 2, 3, 4, 5]
-i, --interactive [1...5] == [1, 2, 3, 4] # extra dot
Objects [3..1] == [3, 2, 1]
-o, --output [DIR]
fourNumberArray = [1, 2, 3, 4]
meglomaniac = {}
-p, --print
fourNumberAr[Link]sh(5, 6)
beforeEach ->
-e, --eval fourNumberArray == [1, 2, 3, 4, 5, 6]
meglomaniac =
--nodejs [1..10][3..5] == [4, 5, 6] # range slicing
mastermind: 'The Monarch'
"my string"[0..1] == "my" # string slicing
most commonly used parameters from henchwoman: 'Dr Girlfriend'
#iterate with hasOwnProperty check
coffeescr[Link] theBomb: true
for own key, value of object
# ? existence operator
copyOfArray = array.slice()
Functions meglomaniac.theBomb? == true
meglomaniac.theDetonator? == false
#Last expression value is return value Array Reduction
# properties can be added and deleted
fill = (container, liquid = "coffee") ->
meglomaniac.mastermind2 = 'Agent Smith' # Javascript style
"Filling the #{container} with #{liquid}..."
delete meglomaniac.mastermind total = (i, a) -> i + a
# internal variables override outer ones
# prototype to add to all projects reduction = [1..3].reduce total == 6
message = 'Outer'
Circle = (radius) -> @radius = radius # Coffeescript-style
getMessage = -> message
'@' = 'this context' In Coffeescript total = 0
overrideMessage = -> message = 'Inner'
sum = (a) -> total = total + a
overrideMessage() == 'Inner'
Inheritance sum x for x in [1..3]
# supports splats
total == 6
returnAllArgs = (allargs...) -> allargs # running example from Muppets
returnAllArgs('first', 'second', 'third') == class Muppet MDN Documentation:
['first', 'second', 'third'] constructor: (@age, @hobby) -> http://tinyurl.com/arreduce
returnAllButFirst = (firstArg, rest...) -> rest answerNanny: -> "Everything's cool!"
returnAllButFirst('first', 'second', 'third') == class SwedishChef extends Muppet Credits
['second', 'third'] constructor: (age, hobby, @mood) ->
This cheat sheet is (mostly) based on work
#destructuring assignment super(age, hobby)
of sleepyfox, which can be found here:
weatherReport = (location) -> [location, 22, cook: -> 'Mmmm soup!'
https://github.com/sleepyfox/coffeescript-‐
'Mostly sunny'] @swedishChef = new SwedishChef 3,
koans
[city, temperature, forecast] = weathe‐ 'cooking', 'chillin'
CoffeeScript @github:
rReport 'London' @swedishC[Link]() == 'Mmmm soup!'
http://jashkenas.github.com/coffee-script/
city == 'London' #base object
temperature == 22 @swedishC[Link]swerNanny() == "Everyt‐
Misc
hing's cool!"
Official Site: http://coffeescrip[Link]/
Higher Order #instances to override class methods
Book: http://pragp[Link]m/book/tbcoffee/co‐
gonzo = new Muppet 3, 'daredevil
2 in [1..3] == true ffeescript
performer'
#Javascript-style filter Smooth CoffeeScript: http://tiny.cc/smo‐
gonzo.answerNanny = -> 'Hehehe!'
even = (a) -> a % 2 == 0 othcs
[1..6].filter even == [2, 4, 6] Jitter (compiler after modifying files):
Hello World
# CoffeeScript-style filter https://github.com/TrevorBurnham/jitter
odds = (x for x in [1..6] when not even x) echo "consol[Link] 'Hello World'" > hello.c‐
Codeschool Course:
# CoffeeScript-style map offee
http://www.codesch[Link]m/courses/coffe‐
twice = (a) -> a * 2 coffee hello.coffee
escript
By Dimitrios Mistriotis Published 23rd December, 2011. Sponsored by [Link]
(dimitrios) Last updated 12th May, 2016. Everyone has a novel in them. Finish
[Link]/dimitrios/ Page 1 of 2. Yours!
[Link]
CoffeeScript Cheat Sheet
by Dimitrios Mistriotis (dimitrios) via [Link]/336/cs/167/
Logo
By Dimitrios Mistriotis Published 23rd December, 2011. Sponsored by [Link]
(dimitrios) Last updated 12th May, 2016. Everyone has a novel in them. Finish
[Link]/dimitrios/ Page 2 of 2. Yours!
[Link]