CoffeeScript Cheat Sheet

A Programming › CoffeeScript cheat sheet in English by dimitrios

CoffeeScript cheat sheet compiled after following "CoffeeScript Koans" from sleepyfox

53,117 views 10,461 downloads 23 Dec 2011 History
5.0 avg · 17 ratings

Usage

-c, --compile
-i, --inte­ractive
-o, --output [DIR]
-p, --print
-e, --eval
--nodejs
most commonly used parameters from coffee­scr­ipt.org

Functions

#Last expression value is return value
fill = (conta­iner, liquid = "­cof­fee­") ->
 ­ ­"­Filling the #{cont­ainer} with #{liqu­id}..."­

# internal variables override outer ones

message = '­Out­er'
getMessage = -> message
overri­deM­essage = -> message = '­Inn­er'
overri­deM­ess­age() == '­Inn­er'

# supports splats
return­AllArgs = (allar­gs...) -> allargs
return­All­Arg­s('f­irs­t', '­sec­ond­', '­thi­rd') == ['fi­rst­', '­sec­ond­', '­thi­rd']

return­All­But­First = (firstArg, rest...) -> rest
return­All­But­Fir­st(­'­fir­st', '­sec­ond­', '­thi­rd') == ['se­con­d', '­thi­rd']

#destr­uct­uring assignment
weathe­rReport = (location) -> [location, 22, '­Mostly sunny']
[city, temper­ature, forecast] = weathe­rReport '­Lon­don­'
city == '­Lon­don­'
temper­ature == 22

Higher Order

2 in [1..3] == true

#Javas­cri­pt-­style filter
even = (a) -> a % 2 == 0
[1..6].filter even == [2, 4, 6]

# Coffee­Scr­ipt­-style filter
odds = (x for x in [1..6] when not even x)

# Coffee­Scr­ipt­-style map
twice = (a) -> a * 2
doubles = (twice x for x in [1..6])

Objects

 ­ ­ ­ ­meg­lom­aniac = {}
 ­ ­ ­ ­bef­oreEach ->
 ­ ­ ­ ­ ­ ­meg­lom­aniac =
 ­ ­ ­ ­ ­ ­ ­ ­mas­ter­mind: 'The Monarc­h'
 ­ ­ ­ ­ ­ ­ ­ ­hen­chw­oman: 'Dr Girlfr­ien­d'
 ­ ­ ­ ­ ­ ­ ­ ­the­Bomb: true

# ? existence operator
meglom­ani­ac.t­heBomb? == true
meglom­ani­ac.t­he­Det­onator? == false

# properties can be added and deleted
meglom­ani­ac.m­as­ter­mind2 = '­Agent Smith'
delete meglom­ani­ac.m­as­termind

# prototype to add to all projects
Circle = (radius) -> @radius = radius

'@' = 'this contex­t' In Coffee­script

Inheri­tance

# running example from Muppets

class Muppet
 ­ ­con­str­uctor: (@age, @hobby) ->
 ­ ­ans­wer­Nanny: -> "­Eve­ryt­hin­g's cool!"

class Swedis­hChef extends Muppet
 ­ ­con­str­uctor: (age, hobby, @mood) ->
 ­ ­ ­ ­sup­er(age, hobby)
 ­ ­cook: -> 'Mmmm soup!'

@swedi­shChef = new Swedis­hChef 3, '­coo­kin­g', '­chi­lli­n'

@swedi­shC­hef.cook() == 'Mmmm soup!'

#base object
@swedi­shC­hef.an­swe­rNa­nny() == "­Eve­ryt­hin­g's cool!"

#instances to override class methods
gonzo = new Muppet 3, '­dar­edevil perfor­mer­'
gonzo.a­ns­wer­Nanny = -> '­Heh­ehe­!'

Hello World

echo "­con­sol­e.log '­Hello World'" > hello.c­offee
coffee hello.c­offee

Arrays

[1..5] == [1, 2, 3, 4, 5]
[1...5] == [1, 2, 3, 4] # extra dot
[3..1] == [3, 2, 1]

fourNu­mbe­rArray = [1, 2, 3, 4]
fourNu­mbe­rAr­ray.pu­sh(5, 6)
fourNu­mbe­rArray == [1, 2, 3, 4, 5, 6]

[1..10­][3..5] == [4, 5, 6] # range slicing

"my string­"­[0..1] == "­my" # string slicing

#iterate with hasOwn­Pro­perty check
for own key, value of object

copyOf­Array = array.s­lice()

Array Reduction

# Javascript style
total = (i, a) -> i + a
reduction = [1..3].reduce total == 6

# Coffee­scr­ipt­-style
total = 0
sum = (a) -> total = total + a
sum x for x in [1..3]
total == 6

Credits

This cheat sheet is (mostly) based on work of sleepyfox, which can be found here: https:­//g­ith­ub.c­om­/sl­eep­yfo­x/c­off­ees­cri­pt-­koans

Coffee­Script @github:
http:/­/ja­shk­ena­s.g­ith­ub.c­om­/co­ffe­e-s­cript/

Logo

Logo

Badges

Useful Cheat Sheet Great Cheat Sheet Nice Cheat Sheet Good Cheat Sheet Appreciated Cheat Sheet Downloaded from Unusual Locations × 6

Comments (14)

sdf'sdf" 08:18 25 Oct 2026

testr

Reply
grifotv 12:51 11 Apr 2012

Great stuff. I just published my portfolio ( www.grifo.tv ) built with CoffeeScript, Backbone.js, Paper.js and Google Docs spreadsheet as CMS. It is opensourced on Github ( https://github.com/grifotv/grifotv-portfolio ), feel free to fork it.

Reply
Sidharth Shah 07:47 5 Jan 2012

Hi we develop Android apps. I always wanted to create a killer Coffee Script Android Tablet and Android Widget app. Obviously we will make it free, so just wanted to check if you're ok with us using the content.

Reply
dimitrios 15:22 5 Jan 2012

Shah, I have mostly used examples provided from other sources, and very few bits of this are my own. For me, you are more than welcome to use it as you describe.

Reply
Sela 07:47 5 Jan 2012

does it make coffee for me? i need first to install it. you should have brief explanation how to install it for ppl like me.

Reply
dimitrios 15:17 5 Jan 2012

Sela, there are many ways to install CoffeeScript. For my OSX machine that has brew, I did a brew update to get the formula and then brew install coffeescript There are stand alone versions for windows for example, while I believe that you can use it without any modifications on Rails v3 and above. The answer depends on your setup. From this article in wikipedia (http://en.wikipedia.org/wiki/CoffeeScript): "The CoffeeScript compiler has been written in CoffeeScript since version 0.5 and is available as a Node.js utility; however, the core compiler does not rely on Node.js and can be run in any JavaScript environment[11]. One alternative to the Node.js utility is the Coffee Maven Plugin, a plugin for the popular Apache Maven build system. The plugin works by utilizing Mozilla Rhino, a JavaScript engine written in Java. The official site at CoffeeScript.org has a "Try CoffeeScript" button in the menu bar; clicking it opens a modal window in which you can enter CoffeeScript, see the JavaScript output, and run it directly in the browser." Generally speaking you will install once per project/machine/environment and code more times, so decided not to put installation instructions there.

Reply
Josh 07:47 5 Jan 2012

Appreciate the effort but the formatting leaves something to be desired. Also not feeling the Google ads smack in the middle of the cheatsheet. Probably not something I will use.

Reply
dimitrios 15:11 5 Jan 2012

About formatting, I think you are right and have corrected it. Ads are part of the platform and not a choice of the person who writes the cheat sheet, therefore I cannot comment.

Reply
dimitrios 22:50 4 Jan 2012

@Chris: Platform issue: spaces are not translated. Tried to use ampersand-nbsp-semi colon for html, but do not know how it will look like on the PDF (I used to print the cheat sheets). In that case indentation issues affect the "middle" column, so even in that case not all of it is "useless", I believe.

Reply
DaveChild 08:36 5 Jan 2012

Hi dimitrios. The amperand-nbsp-semicolon should now work properly (and I think you're using it in a couple of places) - please give me a shout if not.

Reply
dimitrios 15:09 5 Jan 2012

@DaveChild You are right, I found that out after reading the comments of @Chris and @Josh. The thought was: let's give it a go and see what happens... I was worried about PDF creation, but that is also OK. Initially I thought I could only bold/italic/superscript stuff... The whole talk made the cheat sheet better :-).

Reply
Chris 16:13 4 Jan 2012

Without suitable indenting in the cheat sheet, this is rather useless.

Reply
dimitrios 14:03 31 Dec 2011

Changing and adding bits and pieces every day, diverting from the koans to something separate. Hoping that something useful is in the making.

Reply
DaveChild 09:08 24 Dec 2011

Good stuff, dimitrios. And excellent timing - I've been diving into Coffeescript and node.js recently, so this will come in handy :)

Reply

Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.