Skip to content

Instantly share code, notes, and snippets.

@nickfargo
Last active January 2, 2016 10:52
Show Gist options
  • Save nickfargo/12bbf306d5db70dc7df8 to your computer and use it in GitHub Desktop.
Save nickfargo/12bbf306d5db70dc7df8 to your computer and use it in GitHub Desktop.
The minimalist fast-track to fluency in CoffeeScript

Instant Coffee

CoffeeScript ⇒ JavaScript


  1. Arrows, not function:
  • e.g. ->, (a) ->, (a,b) ->, curried = (a) -> (b) -> ···
  • => binds this from outer scope
  • generator function if body contains yield
  1. Indentation, not {···}:
  • both for code blocks, and (optionally) for key: value object literals
  • inside multiline object/array literals, end-line comma delimiters are optional
  1. Expressions, not statements:
  • expression bodies evaluate to the value of their tail-position term
    • e.g. “implicit return” in function body: ask = -> 42
  • comprehensions evaluate to an array of each iteration’s evaluation
  1. Assignment, not declaration:
  • no var; identifier scope determined by first/outermost assignment
  • use do to scope arbitrarily with params of IIFE, e.g.:
    • v = do (a, b=x) -> ···v = (function(a, b) {···})(a, x)
  1. Operators:
  • All the unary and binary operators of JS, except void, plus:
    • [@, C::] ⇒ [this(.), C.prototype(.)]
    • not x!x
    • key of objectkey in object
    • value in arrayarray.indexOf(value) >= 0
    • [is, isnt, and, or] ⇒ [===, !==, &&, ||]
    • x or= 42x || (x = 42) (likewise: ||=, and=, &&=)
    • existential operator ? (see below)
  • Ternary conditional replaced by expression form of ifthenelse
  1. Conditionals:
  • ifthenelse ifelse (negate: unless)
  • Expression: e.g. v = if x then y else zv = x ? y : z
  • Postfix: e.g. x if yif (y) {x}
  1. Existence:
  • x?x != null (also catches undeclared x)
  • x?.k ⇒ if x? then x.k else undefined (“null soaking”)
  • x ? y ⇒ if x? then x else y (cf. x or y)
  • x ?= v ⇒ if x? then x else x = v (cf. x or= v)
  1. Loops:
  • loopwhile (true) {···}
  • while condwhile (cond) {···} (negate: until)
  • for value in array, for value, index in array
  • for key of object, for key, value of object, for own key ···
  • optional filter clause: for ··· when cfor ··· { if (c) {···} }
  • inline body with then, e.g.: for v in a then do (v) -> ···
  1. Comprehensions:
  • e.g. a = (x+x for x in y) ⇒ array mapping x+x to each x in y
  1. Constructors:
  • block expression class C ⇒ IIFE that returns a constructor C
  • at class-body scope level:
    • constructor: -> defines C (as function statement, hoisted)
    • @/this references C
    • name: method adds to prototype of C (equal to @::name = method)
  1. Invocation (optional):
  • Inline whitespace as implicit invocation paren (, e.g.:
    • f a, bf(a, b)
    • fs = require 'fs'
    • data = JSON.parse fs.readFileSync 'data.json', 'utf8'
  • right-associative; implicit matching ) runs to end of clause, e.g.:
    • f a + g bf(a + g(b))
    • a + f g ba + f(g(b))
    • f g b, c if aif (a) { f(g(b, c)) }
    • if f a then g b else cf(a) ? g(b) : c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment