+ - 0:00:00

es6 goodies in node 4

 
 
 

Patrick Mueller @pmuellr, muellerware.org
senior node engineer at NodeSource

  
1 / 25
es6 goodies in node.js 4.0

node 4.0 announced!

Have You Heard Of It?

Node.js v4.0.0 has just been released. This is a huge milestone for Node under the new Node.js Foundation. All thanks to the development process inherited from the io.js fork.

All the io.js work has now been integrated back into the core node.js stream!

But wait, there's more ...

  
2 / 25
es6 goodies in node.js 4.0

new in node 4.0

  
3 / 25
es6 goodies in node.js 4.0

wat EcmaScript 6 features?

my favorites:

  • template strings - `new kind of strings!`

  • classes - class X { foo() { log("in foo") } }

  • arrow functions - cb => cb("shorter functions")

  
4 / 25
es6 goodies in node.js 4.0

template strings

  
5 / 25
es6 goodies in node.js 4.0

template strings - simple

x = "World"
y = "Hello"

console.log(`${y}, ${x}`)
// prints "Hello, World"

console.log(
`multi
line
strings`
)
// prints:
// multi
// line
// strings
  
6 / 25
es6 goodies in node.js 4.0

template strings - tagged use case

"use strict"


// old school push-lines-to-array, join-when-done

const lines = []

lines.push("Hello")
lines.push("line number: " + (1+1))
lines.push("Later")

console.log(lines.join('\n'))

// prints:
//   Hello
//   line number: 2
//   Later
  
7 / 25
es6 goodies in node.js 4.0

template strings - tagged use case

"use strict"
const LinePoster = require("./Line-Poster")

// new school push-lines-to-array, join-when-done

const p = LinePoster()

p`Hello`
p`line number: ${1+1}`
p`Later`

console.log(p())

// prints:
//   Hello
//   line number: 2
//   Later
  
8 / 25
es6 goodies in node.js 4.0

template strings - tagged use case

"use strict"

const _ = require("underscore")

const interpolate = require("./interpolate")

module.exports = function LinePoster(lines) {
  lines = lines || []

  return function p(strings /*, value, value */) {
    if (!strings) return lines.join('\n')

    const values = _.toArray(arguments).slice(1)

    lines.push( interpolate(strings, values))
  }
}
  
9 / 25
es6 goodies in node.js 4.0

template strings - tagged use case

p`Hello`

becomes

p( ["Hello"] , [ ] )

p`line number: ${1+1}`

becomes

p(["line number: ",""],[2])
  
10 / 25
es6 goodies in node.js 4.0

template strings - tagged use case

const _ = require("underscore")

//-----------------------------------------------------------------
// f([a1,a2,..], [b1,b2,..]) -> "" + a1 + b1 + a2 + b2 ...
//-----------------------------------------------------------------

module.exports = function interpolate(strings, values) {

  // zip([a1,a2,..], [b1,b2,..])) -> [[a1,b1], [a2,b2], ...]
  strings = _.zip(strings, values)

  // flatten([[a1,b1], [a2,b2], ...]) -> [a1, b1, a2, b2, ...]
  strings = _.flatten(strings)

  return strings.join('')
}
  
11 / 25
es6 goodies in node.js 4.0

template strings - moar info

  
12 / 25
es6 goodies in node.js 4.0

classes

  
13 / 25
es6 goodies in node.js 4.0

classes - simple old school

"use strict"

function Animal(name) {
  this.name = name
}

Animal.prototype.speak = function speak() {
  console.log("hi, my name is " + this.name)
}

new Animal("Bob").speak()

// prints: hi, my name is Bob
  
14 / 25
es6 goodies in node.js 4.0

classes - simple new school

"use strict"

class Animal {

  constructor(name) {
    this.name = name
  }

  speak() {
    console.log(`hi, my name is ${this.name}`)
  }
}

new Animal("Bob").speak()

// prints: hi, my name is Bob
  
15 / 25
es6 goodies in node.js 4.0

classes - subclasses old school

  
16 / 25
es6 goodies in node.js 4.0

classes - subclasses new school

"use strict"

class Animal {
  species() {                 // <----------------
    throw new Error("subclass responsibiity") // h/t Smalltalk
  }
}

class Frog extends Animal {   // <----------------
  species() {                 // <----------------
    return "frog"
  }
}

console.log(new Frog().species())   // prints: frog
console.log(new Animal().species()) // throws error
  
17 / 25
es6 goodies in node.js 4.0

classes - super calls

"use strict"

class Animal {
  constructor(name) {
    this.name = name
  }
  speak() {
    console.log("hi, I'm " + this.name)
  }
}

class Frog extends Animal {
  constructor(name) {
    super(name)     // <--------------
  }
}

new Frog("Bob").speak()   // prints: hi, I'm Bob
  
18 / 25
es6 goodies in node.js 4.0

class performance note

from Trevor Norris, one of the resident performance gurus at NodeSource:

It's worth mentioning that super() isn't optimized yet. So should not be used in hot code.

  
19 / 25
es6 goodies in node.js 4.0

classes - moar info

  
20 / 25
es6 goodies in node.js 4.0

arrow functions

  
21 / 25
es6 goodies in node.js 4.0

arrow function

const foo = ()    => console.log("in foo")
            // ~like function foo() { console.log(...) }
const bar = x     => console.log("in bar with", x)
            // ~like function bar(x) { console.log(...) }
const gru = (x,y) => console.log("in gru with", x, y)
            // ~like function gru(x,y) { console.log(...) }
const pup = () => {
  console.log("in pup with")
  console.log("...nothing")
}

foo(); bar(42); gru(1,99); pup()

// prints:
// in foo
// in bar with 42
// in gru with 1 99
// in pup with
// ...nothing
  
22 / 25
es6 goodies in node.js 4.0

arrow function w/this

"use strict"

class FakeTransaction {
  expensiveThing(cb) {
    setTimeout( () => this.expensiveThingDone(cb), 500)
    //                ^^^^ look ma, no bind() or self/that
  }

  expensiveThingDone(cb) {
    cb()
  }
}

new FakeTransaction().expensiveThing(
  () => console.log("expensive thing done!")
)

// prints: "expensive thing done"
  
23 / 25
es6 goodies in node.js 4.0

arrow functions - moar info

  
24 / 25
es6 goodies in node.js 4.0

fin

  
25 / 25
es6 goodies in node.js 4.0

node 4.0 announced!

Have You Heard Of It?

Node.js v4.0.0 has just been released. This is a huge milestone for Node under the new Node.js Foundation. All thanks to the development process inherited from the io.js fork.

All the io.js work has now been integrated back into the core node.js stream!

But wait, there's more ...

  
2 / 25
Paused

Help

Keyboard shortcuts

, , Pg Up, k Go to previous slide
, , Pg Dn, Space, j Go to next slide
Home Go to first slide
End Go to last slide
f Toggle fullscreen mode
c Clone slideshow
p Toggle presenter mode
w Pause/Resume the presentation
t Restart the presentation timer
?, h Toggle this help
Esc Back to slideshow