Installation
Install NodeJs (htttp://nodejs.org/) |
Install npm (nodejs included) |
npm install coffee-script -g |
Usage
~$ coffee src/file.coffee |
Run CoffeeScript file |
~$ coffee -c -o output/ src/ |
Compile files from src to output |
~$ coffee -e "console.log 'Hello World'" |
Evaluate CoffeeScript code |
$ coffee --join output/all.js --compile src/*.coffee |
Concatenate the source CoffeeScript before compiling |
~$ coffee |
Start Read-eval-print loop |
~$ coffee --output output/ -cw src/ |
Watch directory src for changes |
Variables
language = 'CoffeeScript' |
? - Existential Operator
coffee = 'espresso' if morning? |
cups = 0; cups ?= 1 |
sugar = count ? 1 |
coffee = one?.coffee?().please?.no?.sugar? |
|
|
Functions
hello = -> console.log "Hello" |
define function |
hello() |
call function |
sum = (a, b) -> a + b |
define function with args |
sum 1, 2 |
call function with args |
sum_multiply = (a, b) -> [ a + b, a * b] |
define function |
[ sum, multiply ] = sum_multiply 1, 2 |
call function |
myFunc = (param = 'default') -> console.log param |
define function with default param |
otherFunc = (first, other...) -> conole.log other |
define function |
otherFunc myArray... |
call function with array as arguments |
Scope
Notice how all of the variable declarations have been pushed up to the top of the closest scope, the first time they appear. outer is not redeclared within the inner function, because it's already in scope; inner within the function, on the other hand, should not be able to change the value of the external variable of the same name, and therefore has a declaration of its own. |
|
|
Arrays
array = [1, 2, 3, 4] |
Array with numbers from 1 to 4 |
array = [1...4] |
Array with numbers from 1 to 3 |
array = [1..3] |
Array with numbers from 1 to 3 |
array1 = array[1..3] |
Create new array from three items from array |
array[1..3] = [3, 2, 1] |
Change three items of array |
ground_coffee.reverse() |
Reverse elements in array |
Math.max [12, 32, 11, 67, 1, 3]... |
Max element of array |
[1 ,2, 3].concat [ 4, 5, 6] |
concatenating arrays |
Operators and aliases
CoffeeScript |
JavaScript |
this, @ |
this |
is |
=== |
isnt |
!== |
not |
! |
and |
&& |
or |
|| |
true, yes, on |
true |
false, no, off |
false |
of |
in |
in |
find element in array (No JS equivalent) |
|
Created By
kucherenko.org
Metadata
Favourited By
Comments
RSrini 11:11 15 Jul 15
array = [1....3]
Array with numbers from 1 to 4
There is no found dots. only 2 dots and 3 dots available.
When i try this the generated array become empty. Please update.
apk, 09:24 21 Nov 15
updated
Add a Comment
Related Cheat Sheets
More Cheat Sheets by apk