0% found this document useful (0 votes)
56 views

Coffeescript

This document is a CoffeeScript cheat sheet that provides a concise reference to CoffeeScript syntax and features. It includes sections on usage, functions, arrays, objects, inheritance, and higher order functions. The cheat sheet covers topics such as CoffeeScript compilation, parameters, destructuring assignment, filtering, mapping, and reducing arrays. It also provides examples of CoffeeScript classes, inheritance, and overriding class methods.

Uploaded by

rizi110
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views

Coffeescript

This document is a CoffeeScript cheat sheet that provides a concise reference to CoffeeScript syntax and features. It includes sections on usage, functions, arrays, objects, inheritance, and higher order functions. The cheat sheet covers topics such as CoffeeScript compilation, parameters, destructuring assignment, filtering, mapping, and reducing arrays. It also provides examples of CoffeeScript classes, inheritance, and overriding class methods.

Uploaded by

rizi110
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

CoffeeScript Cheat Sheet

by Dimitrios Mistriotis (dimitrios) via cheatography.com/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, --inte​ractive [1...5] == [1, 2, 3, 4] # extra dot
Objects [3..1] == [3, 2, 1]
-o, --output [DIR]
fourNu​mbe​rArray = [1, 2, 3, 4]
​ ​ ​ ​meg​lom​aniac = {}
-p, --print
fourNu​mbe​rAr​ray.pu​sh(5, 6)
​ ​ ​ ​bef​oreEach ->
-e, --eval fourNu​mbe​rArray == [1, 2, 3, 4, 5, 6]
​ ​ ​ ​ ​ ​meg​lom​aniac =
--nodejs [1..10​][3..5] == [4, 5, 6] # range slicing
​ ​ ​ ​ ​ ​ ​ ​mas​ter​mind: 'The Monarch'
"my string​"​[0..1] == "​my" # string slicing
most commonly used parameters from ​ ​ ​ ​ ​ ​ ​ ​hen​chw​oman: 'Dr Girlfr​iend'
#iterate with hasOwn​Pro​perty check
coffee​scr​ipt.org ​ ​ ​ ​ ​ ​ ​ ​the​Bomb: true
for own key, value of object
# ? existence operator
copyOf​Array = array.s​lice()
Functions meglom​ani​ac.t​heBomb? == true
meglom​ani​ac.t​he​Det​onator? == false
#Last expression value is return value Array Reduction
# properties can be added and deleted
fill = (conta​iner, liquid = "​cof​fee​") ->
meglom​ani​ac.m​as​ter​mind2 = 'Agent Smith' # Javascript style
​ ​"​Filling the #{cont​ainer} with #{liqu​id}..."​
delete meglom​ani​ac.m​as​termind 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 # Coffee​scr​ipt​-style
getMessage = -> message
'@' = 'this context' In Coffee​script total = 0
overri​deM​essage = -> message = 'Inner'
sum = (a) -> total = total + a
overri​deM​ess​age() == 'Inner'
Inheri​tance sum x for x in [1..3]
# supports splats
total == 6
return​AllArgs = (allar​gs...) -> allargs # running example from Muppets
return​All​Arg​s('​first', 'second', 'third') == class Muppet MDN Docume​nta​tion:
['first', 'second', 'third'] ​ ​con​str​uctor: (@age, @hobby) -> http:/​/ti​nyu​rl.c​om​/ar​reduce
return​All​But​First = (firstArg, rest...) -> rest ​ ​ans​wer​Nanny: -> "​Eve​ryt​hing's cool!"
return​All​But​Fir​st(​'fi​rst', 'second', 'third') == class Swedis​hChef extends Muppet Credits
['second', 'third'] ​ ​con​str​uctor: (age, hobby, @mood) ->
This cheat sheet is (mostly) based on work
#destr​uct​uring assignment ​ ​ ​ ​sup​er(age, hobby)
of sleepyfox, which can be found here:
weathe​rReport = (location) -> [location, 22, ​ ​cook: -> 'Mmmm soup!'
https:​//g​ith​ub.c​om​/sl​eep​yfo​x/c​off​ees​cri​pt-​‐
'Mostly sunny'] @swedi​shChef = new Swedis​hChef 3,
koans
[city, temper​ature, forecast] = weathe​‐ 'cooking', 'chillin'
Coffee​Script @github:
rReport 'London' @swedi​shC​hef.cook() == 'Mmmm soup!'
http:/​/ja​shk​ena​s.g​ith​ub.c​om​/co​ffe​e-s​cript/
city == 'London' #base object
temper​ature == 22 @swedi​shC​hef.an​swe​rNa​nny() == "​Eve​ryt​‐
Misc
hing's cool!"
Official Site: http:/​/co​ffe​esc​rip​t.org/
Higher Order #instances to override class methods
Book: http:/​/pr​agp​rog.co​m/b​ook​/tb​cof​fee​/co​‐
gonzo = new Muppet 3, 'daredevil
2 in [1..3] == true ffe​escript
performer'
#Javas​cri​pt-​style filter Smooth Coffee​Script: http:/​/ti​ny.c​c/​smo​‐
gonzo.a​ns​wer​Nanny = -> 'Hehehe!'
even = (a) -> a % 2 == 0 othcs
[1..6].filter even == [2, 4, 6] Jitter (compiler after modifying files):
Hello World
# Coffee​Scr​ipt​-style filter https:​//g​ith​ub.c​om​/Tr​evo​rBu​rnh​am/​jitter
odds = (x for x in [1..6] when not even x) echo "​con​sol​e.log 'Hello World'​" > hello.c​‐
Codeschool Course:
# Coffee​Scr​ipt​-style map offee
http:/​/ww​w.c​ode​sch​ool.co​m/c​our​ses​/co​ffe​‐
twice = (a) -> a * 2 coffee hello.c​offee
escript

By Dimitrios Mistriotis Published 23rd December, 2011. Sponsored by ApolloPad.com


(dimitrios) Last updated 12th May, 2016. Everyone has a novel in them. Finish
cheatography.com/dimitrios/ Page 1 of 2. Yours!
https://apollopad.com
CoffeeScript Cheat Sheet
by Dimitrios Mistriotis (dimitrios) via cheatography.com/336/cs/167/

Logo

By Dimitrios Mistriotis Published 23rd December, 2011. Sponsored by ApolloPad.com


(dimitrios) Last updated 12th May, 2016. Everyone has a novel in them. Finish
cheatography.com/dimitrios/ Page 2 of 2. Yours!
https://apollopad.com

You might also like