-
No semicolons, use new line
-
No curly braces, use indentation
-
No parentheses required to invoke function
function1 function2 param1, param2 = function1(function2(param1, param2));
- HOWEVER - use them usually, better readability, and
@someFunction
very is different to@someFunction()
- HOWEVER - use them usually, better readability, and
-
Functions return the final line
-
==
becomes===
-
!=
becomes!==
-
is
becomes===
-
isnt
becomes!==
-
not
alias to!
-
and
alias to&&
-
or
alias to||
-
on
andyes
alias fortrue
-
off
andno
alias forfalse
-
unless
is inverse ofif
-
@
alias tothis
-
@property
alias tothis.property
- Single quotes = Literal
- Double quotes = Allow for interpolated values, eg:
"The variable is #{variable} - awesome"
mobyDick = "Call me Ishmael. Some years ago --
never mind how long precisely -- having little
or no money in my purse, and nothing particular
to interest me on shore, I thought I would sail
about a little and see the watery part of the
world..."
Line breaks are converted into a single space. Indentation is ignored.
Use block strings to hold formatted or indentation-sensitive text:
html = """
<strong>
cup of coffeescript
</strong>
"""
The indentation level that begins the first block is maintained through-out
###
Line 1
Line 2
###
//becomes...
/*
Line 1
Line 2
*/
Parameters in parentheses, an arrow, then the function body
square = (x) -> x * x
weatherReport = (location) ->
[location, 72, "Mostly Sunny"]
//JS:
square = function(x) {
return x * x;
};
Note: There is no need to write "return'
fill=(container, liquid ="coffee")
Many styles:
ong = ["do", "re", "mi", "fa", "so"]
address: [
"Via Roma 42R"
"Bellagio, Italy 22021"
]
singers = {Jagger: "Rock�, Elvis: "Roll"}
kids =
brother:
name: "Max"
age: 11
sister:
name: "Ida"
age: 9
yearsOld = max: 10, ida: 9, tim: 11
if happy and knowsIt
clapsHands()
else
showIt()
mood = greatlyImproved if singing
date = if friday then sue else jill
- Use
in
to loop over values - Use
of
to loop over keys
for element in array
# code here
# Loops through food array, assigning "food" to each, and calling eat(food);
eat food for food in ["toast", "cheese", "chocolate", "wine"] when food isnt "chocolate"
shortNames = (name for name in list when name.length < 5)
events = (x for x in [0..10] by 2)
buy() while supply > demand
sell() until supply > demand
healthy = 200 > cholesterol > 60
try
nonexistent / undefined
catch error
The error is: #{error}"
finally
cleanUp()
variable?
returns true unless it is null or undefined
solipsism = true if mind? and not world?
footprints = yeti ? "bear" // If "yeti" exists, footprints = yeti, otherwise footprints = "bear"
speed ?= 15 //if "speed" exists, speed = speed, otherwise speed = 15
zip = lottery.drawWinner?().address?.zipcode
class Animal
constructor: (name) ->
@name = name
move: (meters) ->
alert @name + " moved #{meters}m."
class Snake extends Animal
move: ->
alert "Slithering..."
super 5
class Horse extends Animal
move: ->
alert "Galloping..."
super 45
sam = new Snake "Sammy the Python"
tom = new Horse "Tommy the Palomino"
sam.move()
tom.move()
This:
constructor: (name) ->
@name = name
Can be shortened to:
constructor: (@name) ->
Returns multiple values:
weatherReport = (location) ->
[location, 72, "Mostly Sunny"]
[city, temp, forecast] = weatherReport "Berkeley, CA"
Go deeper:
futurists =
sculptor: "Umberto Boccioni"
painter: "Vladimir Burliuk"
poet:
name: "F.T. Marinetti"
address: [
"Via Roma 42R"
"Bellagio, Italy 22021"
]
{poet: {name, address: [street, city]}} = futurists
Useful for parameters:
class Person
constructor: (options) ->
{@name, @age, @height} = options
tim = new Person age: 4
My fave:
create: (type, weight, {title, subtitle} = {}) ->
title ?= 'Default title'
subtitle ?= 'Default subtitle'
switch day
when "Mon" then go work
when "Tue" then go relax
when "Thu" then go iceFishing
when "Fri", "Sat"
if day is bingoDay
go bingo
go dancing
when "Sun" then go church
else go work
Can be cleaner than if statements:
score = 76
grade = switch
when score < 60 then 'F'
when score < 70 then 'D'
when score < 80 then 'C'
when score < 90 then 'B'
else 'A'