Usage
-c, --compile |
-i, --interactive |
-o, --output [DIR] |
-p, --print |
-e, --eval |
--nodejs |
most commonly used parameters from coffeescript.org
Functions
#Last expression value is return value
fill = (container, liquid = "coffee") ->
"Filling the #{container} with #{liquid}..."
# internal variables override outer ones
message = 'Outer'
getMessage = -> message
overrideMessage = -> message = 'Inner'
overrideMessage() == 'Inner'
# supports splats
returnAllArgs = (allargs...) -> allargs
returnAllArgs('first', 'second', 'third') == ['first', 'second', 'third']
returnAllButFirst = (firstArg, rest...) -> rest
returnAllButFirst('first', 'second', 'third') == ['second', 'third']
#destructuring assignment
weatherReport = (location) -> [location, 22, 'Mostly sunny']
[city, temperature, forecast] = weatherReport 'London'
city == 'London'
temperature == 22 |
Higher Order
2 in [1..3] == true
#Javascript-style filter
even = (a) -> a % 2 == 0
[1..6].filter even == [2, 4, 6]
# CoffeeScript-style filter
odds = (x for x in [1..6] when not even x)
# CoffeeScript-style map
twice = (a) -> a * 2
doubles = (twice x for x in [1..6]) |
|
|
Objects
meglomaniac = {}
beforeEach ->
meglomaniac =
mastermind: 'The Monarch'
henchwoman: 'Dr Girlfriend'
theBomb: true
# ? existence operator
meglomaniac.theBomb? == true
meglomaniac.theDetonator? == false
# properties can be added and deleted
meglomaniac.mastermind2 = 'Agent Smith'
delete meglomaniac.mastermind
# prototype to add to all projects
Circle = (radius) -> @radius = radius
'@' = 'this context' In Coffeescript |
Inheritance
# running example from Muppets
class Muppet
constructor: (@age, @hobby) ->
answerNanny: -> "Everything's cool!"
class SwedishChef extends Muppet
constructor: (age, hobby, @mood) ->
super(age, hobby)
cook: -> 'Mmmm soup!'
@swedishChef = new SwedishChef 3, 'cooking', 'chillin'
@swedishChef.cook() == 'Mmmm soup!'
#base object
@swedishChef.answerNanny() == "Everything's cool!"
#instances to override class methods
gonzo = new Muppet 3, 'daredevil performer'
gonzo.answerNanny = -> 'Hehehe!' |
Hello World
echo "console.log 'Hello World'" > hello.coffee
coffee hello.coffee |
|
|
Arrays
[1..5] == [1, 2, 3, 4, 5]
[1...5] == [1, 2, 3, 4] # extra dot
[3..1] == [3, 2, 1]
fourNumberArray = [1, 2, 3, 4]
fourNumberArray.push(5, 6)
fourNumberArray == [1, 2, 3, 4, 5, 6]
[1..10][3..5] == [4, 5, 6] # range slicing
"my string"[0..1] == "my" # string slicing
#iterate with hasOwnProperty check
for own key, value of object
copyOfArray = array.slice() |
Array Reduction
# Javascript style
total = (i, a) -> i + a
reduction = [1..3].reduce total == 6
# Coffeescript-style
total = 0
sum = (a) -> total = total + a
sum x for x in [1..3]
total == 6 |
|
Created By
Metadata
Favourited By
and 12 more ...
Comments
DaveChild, 09:08 24 Dec 11
Good stuff, dimitrios. And excellent timing - I've been diving into Coffeescript and node.js recently, so this will come in handy :)
dimitrios, 14:03 31 Dec 11
Changing and adding bits and pieces every day, diverting from the koans to something separate. Hoping that something useful is in the making.
Chris 16:13 4 Jan 12
Without suitable indenting in the cheat sheet, this is rather useless.
dimitrios, 22:50 4 Jan 12
@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.
DaveChild, 08:36 5 Jan 12
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.
dimitrios, 15:09 5 Jan 12
@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 :-).
Josh 07:47 5 Jan 12
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.
dimitrios, 15:11 5 Jan 12
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.
Sela 07:47 5 Jan 12
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.
dimitrios, 15:17 5 Jan 12
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.
Sidharth Shah 07:47 5 Jan 12
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.
dimitrios, 15:22 5 Jan 12
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.
grifotv, 12:51 11 Apr 12
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.
Add a Comment
Related Cheat Sheets
More Cheat Sheets by dimitrios