Defaults
function trim(str, char = ' ')
{
…
}
trim(' lala land ')
Slide 32
Slide 32 text
Magic Parts
Slide 33
Slide 33 text
Named Parameters
Slide 34
Slide 34 text
Named Parameters
function complex(name, priority, user, title) {
…
}
complex(
„Hannes Moser",
undefined,
null,
„Mag.(FH)„
)
// I like this more
complex({
name: "Hannes Moser",
title: "Mag.(FH)"
})
Slide 35
Slide 35 text
Named Parameters
import assign from 'lodash/assign'
function (config) {
config = assign({baseurl: '/'}, config)
}
Slide 36
Slide 36 text
Named Parameters
function url({baseurl: '/'}) {
…
}
url({})
Slide 37
Slide 37 text
Named Parameters
function url({baseurl: '/'} = {}){
…
}
url() // "/"
Slide 38
Slide 38 text
Class-free Objects
Slide 39
Slide 39 text
Class-free Objects: Prototypes
function Pokemon(name) {
this.name = name
// handle ...
}
Pokemon.prototype.collect = function() {
…
}
const pikachu = new Pokemon("Pikachu")
Slide 40
Slide 40 text
Class-free Objects: ES2015 Classes
class Pokemon {
constructor(name) {
this.name = name
// handle ...
}
collect() {
…
}
}
const pikachu = new Pokemon("Pikachu")
Slide 41
Slide 41 text
Class-free Objects: Constructor Function
function pokemon({name} = {}) {
function collect() {
…
}
return {
collect
}
}
const pikachu = pokemon({name: 'Pokemon'})
Slide 42
Slide 42 text
Class-free Objects: Parasitic Inheritance
import parasite from 'parasite'
function pokemon({name} = {}) {
const super = parasite({name})
function collect() {
…
}
super.collect = collect
return
}
const pikachu = pokemon({name: ‚Pokemon'})
pikachu.eat() && pikachu.collect()
Slide 43
Slide 43 text
Clean Interfaces
Slide 44
Slide 44 text
Clean Interfaces
export default function pokemon() {
function collect() {
…
}
return {collect}
}
const pikachu = pokemon()
pikachu.bad = function() {
…
}
// no problem at all
for-of
const a = [1, 2]
for([idx, val] of a.entries()) {
console.log(idx, val)
}
Slide 53
Slide 53 text
mandatory
parameters
Slide 54
Slide 54 text
mandatory parameters
function mandatory() {
throw new Error("Missing Parameter")
}
function foo(a = mandatory()) {
…
}
foo() // Error: Missing Parameter
foo(1)
Slide 55
Slide 55 text
mixins
Slide 56
Slide 56 text
mixins
const Validation = Sup => class extends Sup {
validate(schema) {
…
}
}
class Person {
…
}
class Employee extends Validation(Person) {
…
}
Slide 57
Slide 57 text
that’s not async
Slide 58
Slide 58 text
that’s not async
const file = open('hannes.json')
const result = process(file)
const html = render(result)
html
.then(str => console.log(str))
.catch(err => console.error(err))
Slide 59
Slide 59 text
that’s not async
function open(file) {
return new Promise((resolve, reject) => {
fs.readFile(file, (err, data) => {
if (err) {
return reject(err)
}
return resolve(data)
})
})
}
Slide 60
Slide 60 text
async/await
const file = await open('hannes.json')
const result = process(file)
const html = render(result)
console.log(html)
Slide 61
Slide 61 text
Thank You
Slide 62
Slide 62 text
Sources
• ECMAScript® 2016 Language Specification
• Douglas Crockford: The Better Parts
• Axel Rauschmayr: 2ality.com, Exploring ES6
• Rebecca Murphy: Ain't No Party Like a Third-Party
JavaScript Party
• Snippets: https://github.com/eliias/the-magic-parts