In loving memory of CoffeeScript

In the beginning..

A brief history of time

Fun facts



Better JS with CoffeeScript

"A better title might be CoffeeScript is beautiful
and I never want to write plain JavaScript again"

CoffeeScript is a little language that compiles into JavaScript.
Underneath that awkward Java-esque patina, JavaScript has always had a gorgeous heart.
CoffeeScript is an attempt to expose the good parts of JavaScript in a simple way.

a lot is in es6

Bound functions

var square = (x)=> x + x

Default params

var fill = (container, liquid="coffee")=> {
  console.log("Filling the #{container} with #{liquid}...")
}

Destructuring assignment

[a, b] = [1, 2]
{a, b} = {a:1, b:2}
{ AppStore } = App.Stores

Classes

class Animal {
  constructor(name) {
    this.name = name;
  }
  move(meters) {
    this.distance += meters;
  }
}
class Snake extends Animal {
  move() {
    alert("Slithering...");
    super(5);
  }
}

Classes

class Animal
  constructor: (@name)->
  
  
  move: (meters)->
    @distance += meters
    
    
class Snake extends Animal
  move: ->
    alert "Slithering..."
    super 5
    
    

Rest params

store.add = (category, ...items)=> {
  items.forEach((item)=> {
    store.aisle[category].push(item);
  });
};

Rest params

store.add = (category, items...)->
  items.forEach (item)->
    store.aisle[category].push item
    
    

Template strings

var hello = (name)=> `hello ${name}`
var multiline = ()=> {
 return `a
b
c`;
}

Template strings

hello = (name)=> "hello #{name}"
multiline = ->
  """
  a
  b
  c
  """
  

There's a lot I'll miss

No var

myVariable = 'myValue'

# never miss a var
# hoisting
# private by default
# JSLint compliant

Significant whitespace

if (url) {
  fetch(url, (data)=> {
    console.log(data);
  });
}
else {
  nope();
}

data = {
  url: '/api/data.json',
  properties: ['a','b'],
  params: {
    query: 'Ma'
  }
};

Significant whitespace

if url
  fetch url, (data)->
    console.log data
else
  nope()
  
  
  
  
data =
  url: '/api/data.json'
  properties: ['a','b']
  params:
    query: 'Ma'
    
    

Simple loops

words = 'Peter Piper picked a peck'.split ' '

for word, index in words
  console.log "#{index} #{word}"
  
  

Simple loops

var words = 'Peter Piper picked a peck'.split(' ');

for (var index = 0, len = words.length; index < len; index++) {
  var word = words[index];
  console.log("#{index} #{word}");
}

Simple loops

options =
  strict: true
  magic: 'string'
  
  
for key, value of options
  console.log "#{key} #{value}"
  
  

Simple loops

var options = {
  strict: true,
  magic: 'string'
};

for (key in options) {
  var value = options[key];
  console.log(key + " " + value);
}

Automatic returns

singHappy = ->
  if happy
    clapAlong()
  else
    dont()

Everything is an expression

# each
for word in words
  console.log "#{prefix} #{word}"
  
# map
prefixed = for word in words
  "#{prefix} #{word}"
  
# map + filter
fWords = for word in words when word[0] == 'f'
  "#{prefix} #{word}"
  

Everything is an expression

greeting = switch day
  when 'monday' then 'piss off'
  when 'sunday' then 'how do you do'
  else 'hello'
  
getGreeting = (day)->
  switch day
    when 'monday' then 'piss off'
    when 'sunday' then 'how do you do'
    else 'hello'

Conditionals

request() if active

return unless options?

Aliases / operators

== -> ===
&& -> and
|| -> or
? : ; -> if then else
if ! -> unless
(start < date && date < end) -> (start < date < end)
this. -> @
.prototype. -> ::

if myVariable? // not null or undefined
if params?.query? // soak operator

"We are stuck with JavaScript, we can’t change the language syntax in backwards incompatible ways if we want to write applications that run everywhere."

We'll be compiling forever

End of life?

Community

Syntax

Features

Thanks

@markbrown4