+ All Categories
Home > Technology > ES6 patterns in the wild

ES6 patterns in the wild

Date post: 13-Apr-2017
Category:
Upload: joe-morgan
View: 370 times
Download: 1 times
Share this document with a friend
164
ES6 Patterns in the Wild
Transcript
Page 1: ES6 patterns in the wild

ES6 Patterns in the Wild

Page 2: ES6 patterns in the wild

function createThunkMiddleware(extraArgument) { return ({ dispatch, getState }) => next => action => { if (typeof action === 'function') { return action(dispatch, getState, extraArgument); } return next(action); }; }

const thunk = createThunkMiddleware(); thunk.withExtraArgument = createThunkMiddleware;

export default thunk;

Page 3: ES6 patterns in the wild

Code to Run (wild code)

Page 4: ES6 patterns in the wild

Code to Run (wild code)

Code we write Code we import

Page 5: ES6 patterns in the wild

Code to Run (wild code)

Code we write Code we import$$

Page 6: ES6 patterns in the wild

Code to Teach

Page 7: ES6 patterns in the wild

Code to Teach

Tutorials

Page 8: ES6 patterns in the wild

Code to Teach

Tutorials

Documentation

Style Guides

Stack Overflow

BooksConferences

Page 9: ES6 patterns in the wild

Code to Teach

Tutorials

Documentation

Style Guides

Stack Overflow

BooksConferences

Good Stuff

Page 10: ES6 patterns in the wild

Syntax

SELECT ID, NAME, AGE, AMOUNT FROM CUSTOMERS, ORDERS WHERE CUSTOMERS.ID = ORDERS.CUSTOMER_ID;

Page 11: ES6 patterns in the wild

Unreadable

Page 12: ES6 patterns in the wild

Pattern

Page 13: ES6 patterns in the wild

Complexity

Page 14: ES6 patterns in the wild

What we expect

Page 15: ES6 patterns in the wild

What we get

Page 16: ES6 patterns in the wild

Code to Teach Code to Run

Page 17: ES6 patterns in the wild

Code to Teach Code to Run

Page 18: ES6 patterns in the wild

Simple

Complex

Page 19: ES6 patterns in the wild

Complex

Simple

Page 20: ES6 patterns in the wild

Joe Morgan

Page 21: ES6 patterns in the wild

Lawrence, KS

Page 22: ES6 patterns in the wild
Page 23: ES6 patterns in the wild

I Write Code

Page 24: ES6 patterns in the wild

Joe Morgan

Page 25: ES6 patterns in the wild

Joe Morgan

Read

Page 26: ES6 patterns in the wild

Joe Morgan

ReadRead

Critically

Page 27: ES6 patterns in the wild

Reading Code

Page 28: ES6 patterns in the wild

Redux

Page 29: ES6 patterns in the wild

{ donuts: [ “chocolate”, ], filter: null }

state.donuts.map(donut => { return ( <h2> {{donut}} </h2> ) })

<h2> Chocolate </h2>

State Component

Page 30: ES6 patterns in the wild

{ donuts: [ “chocolate”, ], filter: null }

state.donuts.map(donut => { return ( <h2> {{donut}} </h2> ) })

<h2> Chocolate </h2>

State Component

dispatch(addDonut(‘maple’))

Page 31: ES6 patterns in the wild

{ donuts: [ “chocolate”, “maple”, ], filter: null }

state.donuts.map(donut => { return ( <h2> {{donut}} </h2> ) })

<h2> Chocolate </h2>

State Component

dispatch(addDonut(‘maple’))

Page 32: ES6 patterns in the wild

updated state

{ donuts: [ “chocolate”, “maple”, ], filter: null }

state.donuts.map(donut => { return ( <h2> {{donut}} </h2> ) })

<h2> Chocolate </h2>

State Component

dispatch(addDonut(‘maple’))

Page 33: ES6 patterns in the wild

updated state

{ donuts: [ “chocolate”, “maple”, ], filter: null }

state.donuts.map(donut => { return ( <h2> {{donut}} </h2> ) })

<h2> Chocolate </h2> <h2> Maple </h2>

State Component

dispatch(addDonut(‘maple’))

Page 34: ES6 patterns in the wild

Redux

Functional Array Methods High Order Functions Curry/Partially Applied Functions

Page 35: ES6 patterns in the wild

Redux

Page 36: ES6 patterns in the wild

updated state

{ donuts: [ “chocolate”, “maple”, ], filter: null }

state.donuts.map(donut => { return ( <h2> {{donut}} </h2> ) }

<h2> Chocolate </h2> <h2> Maple </h2>

State Component

dispatch(addDonut(‘maple’))

Middleware

Middleware

Page 37: ES6 patterns in the wild

import compose from './compose'

export default function applyMiddleware(...middlewares) { return (createStore) => (reducer, preloadedState, enhancer) => { var store = createStore(reducer, preloadedState, enhancer) var dispatch = store.dispatch var chain = []

var middlewareAPI = { getState: store.getState, dispatch: (action) => dispatch(action) } chain = middlewares.map(middleware => middleware(middlewareAPI)) dispatch = compose(...chain)(store.dispatch)

return { ...store, dispatch } } }

Page 38: ES6 patterns in the wild

import compose from './compose'

export default function applyMiddleware(...middlewares) { return (createStore) => (reducer, preloadedState, enhancer) => { var middlewareAPI = { getState: store.getState, dispatch: (action) => dispatch(action) } chain = middlewares.map(middleware => middleware(middlewareAPI))

} }

Arrow Functions

Page 39: ES6 patterns in the wild

import compose from './compose'

export default function applyMiddleware(...middlewares) { return (createStore) => (reducer, preloadedState, enhancer) => { var middlewareAPI = { getState: store.getState, dispatch: (action) => dispatch(action) } chain = middlewares.map(middleware => middleware(middlewareAPI))

} }

Page 40: ES6 patterns in the wild

import compose from './compose'

export default function applyMiddleware(...middlewares) { return function(createStore) { return function (reducer, preloadedState, enhancer) { var middlewareAPI = { getState: store.getState, dispatch: function (action) { return dispatch(action); } } chain = middlewares.map(function(middleware) { return middleware(middlewareAPI) }) } } }

Page 41: ES6 patterns in the wild

import compose from './compose'

export default function applyMiddleware(...middlewares) { return (createStore) => (reducer, preloadedState, enhancer) => { var middlewareAPI = { getState: store.getState, dispatch: (action) => dispatch(action) } chain = middlewares.map(middleware => middleware(middlewareAPI))

} }

Page 42: ES6 patterns in the wild

chain = middlewares.map(middleware => middleware(middlewareAPI))

Page 43: ES6 patterns in the wild

chain = middlewares.map(middleware => middleware(middlewareAPI))

chain = middlewares.map((middleware) => {return middleware(middlewareAPI)})

Page 44: ES6 patterns in the wild

var that = this;

Page 45: ES6 patterns in the wild

var namer = { name: 'bill', say: function() { var that = this console.log(this.name); setTimeout(function() { console.log(that.name); },200); } }

Page 46: ES6 patterns in the wild

var that = this;

Page 47: ES6 patterns in the wild

const namer = { name: 'bill', say: function() { console.log(this.name); setTimeout(() => { console.log(this.name); },200); } }

Page 48: ES6 patterns in the wild

Favor Arrow Functions

Page 49: ES6 patterns in the wild

Why Read Wild Code?

Page 50: ES6 patterns in the wild

i. Reading Code is a Skill

Page 51: ES6 patterns in the wild

function createThunkMiddleware(extraArgument) { return ({ dispatch, getState }) => next => action => { if (typeof action === 'function') { return action(dispatch, getState, extraArgument); } return next(action); }; }

const thunk = createThunkMiddleware(); thunk.withExtraArgument = createThunkMiddleware;

export default thunk;

Page 52: ES6 patterns in the wild

i. Reading Code is a Skill: Intuition

Page 53: ES6 patterns in the wild

i. Reading Code is a Skill: Intuition

Page 54: ES6 patterns in the wild

i. Reading Code is a Skill: Intuition

Page 55: ES6 patterns in the wild

Redux Rest/Spread

export default function applyMiddleware(...middlewares) { // Lots of stuff chain = middlewares.map(middleware => middleware(middlewareAPI)) dispatch = compose(...chain)(store.dispatch) } }

Page 56: ES6 patterns in the wild

export default function applyMiddleware(...middlewares) { // Lots of stuff chain = middlewares.map(middleware => middleware(middlewareAPI)) dispatch = compose(...chain)(store.dispatch) } }

(I actually didn’t realize they had different names)

Page 57: ES6 patterns in the wild

Redux Rest/Spread

Rest: list => array

Page 58: ES6 patterns in the wild

export default function applyMiddleware(...middlewares) { // Lots of stuff chain = middlewares.map(middleware => middleware(middlewareAPI)) dispatch = compose(...chain)(store.dispatch) } }

Page 59: ES6 patterns in the wild

export default function applyMiddleware(...middlewares)

examples/real-world/src/store/configureStore.dev.js

applyMiddleware(thunk, api, createLogger())

examples/real-world/src/store/configureStore.prod.js

applyMiddleware(thunk, api)

Page 60: ES6 patterns in the wild

Redux Rest/Spread

Spread: array => list

Page 61: ES6 patterns in the wild

export default function applyMiddleware(...middlewares) { dispatch = compose(...chain)(store.dispatch)

examples/real-world/src/store/configureStore.dev.js compose(thunk, api, createLogger())(store.dispatch)

examples/real-world/src/store/configureStore.prod.js compose(thunk, api)(store.dispatch)

Page 62: ES6 patterns in the wild

Redux Rest/Spread

Convert items to arrays

Page 63: ES6 patterns in the wild

export default function applyMiddleware(...middlewares) { // Lots of stuff

chain = middlewares.map(middleware => middleware(middlewareAPI)) dispatch = compose(...chain)(store.dispatch) } }

Page 64: ES6 patterns in the wild

export default function applyMiddleware(...middlewares) { // Lots of stuff var middlewareAPI = { getState: store.getState, dispatch: (action) => dispatch(action) } chain = middlewares.map(middleware => middleware(middlewareAPI)) dispatch = compose(...chain)(store.dispatch) } }

Page 65: ES6 patterns in the wild

Redux Rest/Spread

Converting array to a new array without mutations

Page 66: ES6 patterns in the wild

describe('combineReducers', () => { it('returns a composite reducer that maps the state keys to given reducers', () => { const reducer = combineReducers({ ... action.type === 'push' ? [ ...state, action.value ] : state })

Page 67: ES6 patterns in the wild

describe('combineReducers', () => { it('returns a composite reducer that maps the state keys to given reducers', () => { const reducer = combineReducers({ ... action.type === 'push' ? [ ...state, action.value ] : state })

Page 68: ES6 patterns in the wild

const family = [ { name: 'Joe', age: 34 }, { name: 'Theo', age: 1 }, { name: 'Dyan', age: 34 }, ]

const sortName = (a,b) => { return a.name > b.name ? 1 : -1 }

const sortAge = (a, b) => { if(a.age === b.age) { return 0; }

return a.age > b.age ? 1 : -1 }

Page 69: ES6 patterns in the wild

const family = [ { name: 'Joe', age: 34 }, { name: 'Theo', age: 1 }, { name: 'Dyan', age: 34 }, ]

const sortName = (a,b) => { return a.name > b.name ? 1 : -1 }

const sortAge = (a, b) => { if(a.age === b.age) { return 0; }

return a.age > b.age ? 1 : -1 }

> family.sort(sortAge);

Page 70: ES6 patterns in the wild

const family = [ { name: 'Theo', age: 1 }, { name: 'Joe', age: 34 }, { name: 'Dyan', age: 34 }, ]

const sortName = (a,b) => { return a.name > b.name ? 1 : -1 }

const sortAge = (a, b) => { if(a.age === b.age) { return 0; }

return a.age > b.age ? 1 : -1 }

[ { name: 'Theo', age: 1 }, { name: 'Joe', age: 34 }, { name: 'Dyan', age: 34 }, ]

Page 71: ES6 patterns in the wild

const family = [ { name: 'Theo', age: 1 }, { name: 'Joe', age: 34 }, { name: 'Dyan', age: 34 }, ]

const sortName = (a,b) => { return a.name > b.name ? 1 : -1 }

const sortAge = (a, b) => { if(a.age === b.age) { return 0; }

return a.age > b.age ? 1 : -1 }

Page 72: ES6 patterns in the wild

const family = [ { name: 'Theo', age: 1 }, { name: 'Joe', age: 34 }, { name: 'Dyan', age: 34 }, ]

const sortName = (a,b) => { return a.name > b.name ? 1 : -1 }

const sortAge = (a, b) => { if(a.age === b.age) { return 0; }

return a.age > b.age ? 1 : -1 }

> family.sort(sortName);

Page 73: ES6 patterns in the wild

const family = [ { name: 'Dyan', age: 34 }, { name: 'Joe', age: 34 }, { name: 'Theo', age: 1 }, ]

const sortName = (a,b) => { return a.name > b.name ? 1 : -1 }

const sortAge = (a, b) => { if(a.age === b.age) { return 0; }

return a.age > b.age ? 1 : -1 }

[ { name: 'Dyan', age: 34 }, { name: 'Joe', age: 34 }, { name: 'Theo', age: 1 }, ]

Page 74: ES6 patterns in the wild

const family = [ { name: 'Dyan', age: 34 }, { name: 'Joe', age: 34 }, { name: 'Theo', age: 1 }, ]

const sortName = (a,b) => { return a.name > b.name ? 1 : -1 }

const sortAge = (a, b) => { if(a.age === b.age) { return 0; }

return a.age > b.age ? 1 : -1 }

Page 75: ES6 patterns in the wild

const family = [ { name: 'Dyan', age: 34 }, { name: 'Joe', age: 34 }, { name: 'Theo', age: 1 }, ]

const sortName = (a,b) => { return a.name > b.name ? 1 : -1 }

const sortAge = (a, b) => { if(a.age === b.age) { return 0; }

return a.age > b.age ? 1 : -1 }

> family.sort(sortAge);

Page 76: ES6 patterns in the wild

const family = [ { name: 'Theo', age: 1 }, { name: 'Dyan', age: 34 }, { name: 'Joe', age: 34 }, ]

const sortName = (a,b) => { return a.name > b.name ? 1 : -1 }

const sortAge = (a, b) => { if(a.age === b.age) { return 0; }

return a.age > b.age ? 1 : -1 }

[ { name: 'Theo', age: 1 }, { name: 'Dyan', age: 34 }, { name: 'Joe', age: 34 }, ]

Page 77: ES6 patterns in the wild

const family = [ { name: 'Theo', age: 1 }, { name: 'Dyan', age: 34 }, { name: 'Joe', age: 34 }, ]

const sortName = (a,b) => { return a.name > b.name ? 1 : -1 }

const sortAge = (a, b) => { if(a.age === b.age) { return 0; }

return a.age > b.age ? 1 : -1 }

Page 78: ES6 patterns in the wild

const family = [ { name: 'Joe', age: 34 }, { name: 'Theo', age: 1 }, { name: 'Dyan', age: 34 }, ]

const sortName = (a,b) => { return a.name > b.name ? 1 : -1 }

const sortAge = (a, b) => { if(a.age === b.age) { return 0; }

return a.age > b.age ? 1 : -1 }

Page 79: ES6 patterns in the wild

const family = [ { name: 'Joe', age: 34 }, { name: 'Theo', age: 1 }, { name: 'Dyan', age: 34 }, ]

const sortName = (a,b) => { return a.name > b.name ? 1 : -1 }

const sortAge = (a, b) => { if(a.age === b.age) { return 0; }

return a.age > b.age ? 1 : -1 }

> [...family].sort(sortAge);

Page 80: ES6 patterns in the wild

const family = [ { name: 'Joe', age: 34 }, { name: 'Theo', age: 1 }, { name: 'Dyan', age: 34 }, ]

const sortName = (a,b) => { return a.name > b.name ? 1 : -1 }

const sortAge = (a, b) => { if(a.age === b.age) { return 0; }

return a.age > b.age ? 1 : -1 }

[ { name: 'Theo', age: 1 }, { name: 'Joe', age: 34 }, { name: 'Dyan', age: 34 }, ]

Page 81: ES6 patterns in the wild

const family = [ { name: 'Joe', age: 34 }, { name: 'Theo', age: 1 }, { name: 'Dyan', age: 34 }, ]

const sortName = (a,b) => { return a.name > b.name ? 1 : -1 }

const sortAge = (a, b) => { if(a.age === b.age) { return 0; }

return a.age > b.age ? 1 : -1 }

Page 82: ES6 patterns in the wild

ii. Your code will reflect your reading

Page 83: ES6 patterns in the wild

ii. Your code will reflect your reading

data structures architecture community standards

Page 84: ES6 patterns in the wild

ii. Your code will reflect your reading

It’s ok to take ideas from others

Page 85: ES6 patterns in the wild

ii. Your code will reflect your reading

Page 86: ES6 patterns in the wild

import compose from './compose'

export default function applyMiddleware(...middlewares) { return (createStore) => (reducer, preloadedState, enhancer) => { var store = createStore(reducer, preloadedState, enhancer) var dispatch = store.dispatch var chain = []

var middlewareAPI = { getState: store.getState, dispatch: (action) => dispatch(action) } chain = middlewares.map(middleware => middleware(middlewareAPI)) dispatch = compose(...chain)(store.dispatch)

return { ...store, dispatch } } }

Page 87: ES6 patterns in the wild

import compose from './compose'

export default function applyMiddleware(...middlewares) { return (createStore) => (reducer, preloadedState, enhancer) => { var store = createStore(reducer, preloadedState, enhancer) var dispatch = store.dispatch var chain = []

var middlewareAPI = { getState: store.getState, dispatch: (action) => dispatch(action) } chain = middlewares.map(middleware => middleware(middlewareAPI)) dispatch = compose(...chain)(store.dispatch)

return { ...store, dispatch } } }

Page 88: ES6 patterns in the wild

Redux Object Spread

Not part of ES6

Page 89: ES6 patterns in the wild

//.babelrc { "plugins": [ ... "transform-object-rest-spread", ], ... }

Page 90: ES6 patterns in the wild

Favor Arrow Functions

Page 91: ES6 patterns in the wild

Favor Array Methods (map, reduce, find, filter)

Page 92: ES6 patterns in the wild

Collecting items to arrays +

Arrow Functions =

Happiness

Page 93: ES6 patterns in the wild

Khan Academy

Page 94: ES6 patterns in the wild

Khan Academy

Consumer code. Not a library.

Page 95: ES6 patterns in the wild

Khan Academy

Page 96: ES6 patterns in the wild

Khan Academy

Object Oriented Complexity hidden behind interface Popular in typescript (angular 2) world

Page 97: ES6 patterns in the wild
Page 98: ES6 patterns in the wild

export default class Expression extends Node { constructor(...nodes) { super(); this.type = 'Expression'; this.children = new List(this, ...nodes); }

toString() { return `${this.type}:${this.children.toString()}`; }

toJSON() { return { ...super.toJSON(), children: [...f(this.children).map(child => child.toJSON())], }; }

clone(uniqueId = false) { ...

Page 99: ES6 patterns in the wild

export default class Expression extends Node { constructor(...nodes) { super(); this.type = 'Expression'; this.children = new List(this, ...nodes); }

toString() { return `${this.type}:${this.children.toString()}`; }

toJSON() { return { ...super.toJSON(), children: [...f(this.children).map(child => child.toJSON())], }; }

clone(uniqueId = false) { ...

Page 100: ES6 patterns in the wild

export default class Expression extends Node { constructor(...nodes) { super(); this.type = 'Expression'; this.children = new List(this, ...nodes); }

toString() { return `${this.type}:${this.children.toString()}`; }

toJSON() { return { ...super.toJSON(), children: [...f(this.children).map(child => child.toJSON())], }; }

clone(uniqueId = false) { ...

Page 101: ES6 patterns in the wild

iii. Feature Popularity

Page 102: ES6 patterns in the wild

iii. Feature Popularity: Frequency

The more useful the feature the more frequent it will pop up.

Page 103: ES6 patterns in the wild

iii. Feature Popularity: Frequency

In Shakespeare:

The top 10 most frequently occuring words make up 21.4% of all words. The top 100 most frequently occuring words make up 53.9% of all words.

Page 104: ES6 patterns in the wild

export default class Expression extends Node { constructor(...nodes) { super(); this.type = 'Expression'; this.children = new List(this, ...nodes); }

toString() { return `${this.type}:${this.children.toString()}`; }

toJSON() { return { ...super.toJSON(), children: [...f(this.children).map(child => child.toJSON())], }; }

clone(uniqueId = false) { ...

Page 105: ES6 patterns in the wild

export default class Expression extends Node { constructor(...nodes) { super(); this.children = new List(this, ...nodes); }

toString() { return `${this.type}:${this.children.toString()}`; }

get last() { return this.children.last; }

set last(value) { this.children.last = value; } }

Page 106: ES6 patterns in the wild

const e = new Expression({id:1}, {id:2}, {id:3}) e = { children: { first: { id: 1, next: { id: 2, next: { id: 3 } } }, last: { id: 3 } } }

Page 107: ES6 patterns in the wild

const e = new Expression({id:1}, {id:2}, {id:3}) e = { children: { first: { id: 1, next: { id: 2, next: { id: 3 } } }, last: { id: 3 } } }

Linked List

Page 108: ES6 patterns in the wild

Khan Academy

Page 109: ES6 patterns in the wild

export default class Expression extends Node {

get last() { return this.children.last; }

set last(value) { this.children.last = value; }

}

Page 110: ES6 patterns in the wild

Get/Set treats methods like properties (popular in

typescript)

Page 111: ES6 patterns in the wild

const e = new Expression({id:1}, {id:2}, {id:3}) e = { children: { first: { id: 1, next: { id: 2, next: { id: 3 } } }, last: { id: 3 } } }

Page 112: ES6 patterns in the wild

const e = new Expression({id:1}, {id:2}, {id:3}) e = { children: { first: { id: 1, next: { id: 2, next: { id: 3 } } }, last: { id: 3 } } }

e.last;

// { id: 3 }

Page 113: ES6 patterns in the wild

const e = new Expression({id:1}, {id:2}, {id:3}) e = { children: { first: { id: 1, next: { id: 2, next: { id: 3 } } }, last: { id: 3 } } }

e.last = { id.4 }

Page 114: ES6 patterns in the wild

const e = new Expression({id:1}, {id:2}, {id:3}) e = { children: { first: { id: 1, next: { id: 2, next: { id: 3 } } }, last: { id: 4 } } }

e.last = { id.4 }

Page 115: ES6 patterns in the wild

const e = new Expression({id:1}, {id:2}, {id:3}) e = { children: { first: { id: 1, next: { id: 2, next: { id: 3 } } }, last: { id: 4 } } }

e.last = { id.4 }

Page 116: ES6 patterns in the wild

export default class Expression extends Node {

get last() { return this.children.last; }

set last(value) { this.children.last = value; }

}

Page 117: ES6 patterns in the wild

export default class Expression extends Node {

get last() { return Object.assign( {}, { name:'last' }, this.children.last ); }

set last(value) { this.children.last = value; }

}

Page 118: ES6 patterns in the wild

const e = new Expression({id:1}, {id:2}, {id:3}) e = { children: { first: { id: 1, next: { id: 2, next: { id: 3 } } }, last: { id: 3 } } }

e.last

{ name: ‘last’, id: 3, }

Page 119: ES6 patterns in the wild

export default class Expression extends Node { constructor(...nodes) { super(); this.children = new List(this, ...nodes); }

toString() { return `${this.type}:${this.children.toString()}`; }

get first() { return this.children.first; }

set first(value) { this.children.first = value; } }

Page 120: ES6 patterns in the wild

export default class Expression extends Node {

toString() { return `${this.type}:${this.children.toString()}`; }

}

Page 121: ES6 patterns in the wild

export default class Expression extends Node {

toString() { return this.type + ':' + this.children.toString(); }

}

Page 122: ES6 patterns in the wild

export default class List extends Node { ... toString() { let first = true; for (let node of this) { if (!first) { result += ", "; } else { first = false; } result += node.id; } return result; } }

Page 123: ES6 patterns in the wild

export default class List extends Node { ... toString() { let first = true; for (let node of this) { // Do Stuff with node.id } } }

Page 124: ES6 patterns in the wild

export default class List extends Node { ... toString() { let first = true; for (let node of this) { // Do Stuff with node.id } } }

Page 125: ES6 patterns in the wild

const e = new Expression({id:1}, {id:2}, {id:3}) e = { children: { first: { id: 1, next: { id: 2, next: { id: 3 } } }, last: { id: 3 } } }

Page 126: ES6 patterns in the wild

const presentation = [ 'ES6 Patterns in the Wild', 'Joe Morgan', ]

for(let metadata of presentation) { console.log(metadata); }

Page 127: ES6 patterns in the wild

const presentation = [ 'ES6 Patterns in the Wild', 'Joe Morgan', ]

for(let metadata of presentation) { console.log(metadata); }

// ES 6 Patterns in the Wild // Joe Morgan

Page 128: ES6 patterns in the wild

const presentation = { title: 'ES6 Patterns in the Wild', author: 'Joe Morgan', }

for(let metadata of presentation) { console.log(metadata); }

Page 129: ES6 patterns in the wild

const presentation = { title: 'ES6 Patterns in the Wild', author: 'Joe Morgan', }

for(let metadata of presentation) { console.log(metadata); }

> TypeError: presentation[Symbol.iterator] is not a function

Page 130: ES6 patterns in the wild

export default class List { ... *[Symbol.iterator]() { let node = this.first; while (node != this.last) { let current = node; node = node.next; yield current; } if (this.last) { yield this.last; } } }

Page 131: ES6 patterns in the wild

export default class List { ... *[Symbol.iterator]() { let node = this.first; while (node != this.last) { let current = node; node = node.next; yield current; } if (this.last) { yield this.last; } } }

Page 132: ES6 patterns in the wild

iii. Feature Popularity: Not all features end up being popular

Page 133: ES6 patterns in the wild

iii. Feature Popularity: Not all features end up being popular

Page 134: ES6 patterns in the wild

export default class List { ... *[Symbol.iterator]() { let node = this.first; while (node != this.last) { let current = node; node = node.next; yield current; } if (this.last) { yield this.last; } } }

Page 135: ES6 patterns in the wild

const e = new Expression({id:1}, {id:2}, {id:3}) e = { children: { first: { id: 1, next: { id: 2, next: { id: 3 } } }, last: { id: 3 } } }

Page 136: ES6 patterns in the wild

const e = new Expression({id:1}, {id:2}, {id:3})

e = [{id:1}, {id:2}, {id:3}]

Page 137: ES6 patterns in the wild

Khan Academy

Create Simple Interfaces

Page 138: ES6 patterns in the wild

React

Page 139: ES6 patterns in the wild

React

Performance

Page 140: ES6 patterns in the wild

canUseCollections = ( ... typeof Map === 'function' && isNative(Map) && ... );

if (canUseCollections) { var itemMap = new Map(); var rootIDSet = new Set();

setItem = function(id, item) { itemMap.set(id, item); }; getItem = function(id) { return itemMap.get(id); }; ... }

else { var itemByKey = {}; var rootByKey = {}; var getKeyFromID = () => {} var getIDFromKey = () => {}

setItem = function(id, item) { var key = getKeyFromID(id); itemByKey[key] = item; }; getItem = function(id) { var key = getKeyFromID(id); return itemByKey[key]; }; ... }

Page 141: ES6 patterns in the wild

const presentation = { title: 'ES6 Patterns in the Wild', author: 'Joe Morgan', }

> presentation.title // 'ES6 Patterns in the Wild'

Page 142: ES6 patterns in the wild

const presentation = { title: 'ES6 Patterns in the Wild', author: 'Joe Morgan', }

> presentation.title // 'ES6 Patterns in the Wild'

Page 143: ES6 patterns in the wild

const presentation = new Map();

presentation.set('title', 'ES6 Patterns in the Wild');

presentation.set('author', 'Joe Morgan');

> presentation.get('title'); // 'ES6 Patterns in the Wild'

Page 144: ES6 patterns in the wild

const presentation = new Map() .set('title', 'ES6 Patterns in the Wild') .set('author', 'Joe Morgan');

> presentation.get('title'); // 'ES6 Patterns in the Wild'

Page 145: ES6 patterns in the wild

const presentation = new Map([ ['title', 'ES6 Patterns in the Wild'], ['author', 'Joe Morgan'] ]);

> presentation.get('title'); // 'ES6 Patterns in the Wild'

Page 146: ES6 patterns in the wild

canUseCollections = ( ... typeof Map === 'function' && isNative(Map) && ... );

if (canUseCollections) { var itemMap = new Map(); var rootIDSet = new Set();

setItem = function(id, item) { itemMap.set(id, item); }; getItem = function(id) { return itemMap.get(id); }; ... }

else { var itemByKey = {}; var rootByKey = {}; var getKeyFromID = () => {} var getIDFromKey = () => {}

setItem = function(id, item) { var key = getKeyFromID(id); itemByKey[key] = item; }; getItem = function(id) { var key = getKeyFromID(id); return itemByKey[key]; }; ... }

Page 147: ES6 patterns in the wild

React

Page 148: ES6 patterns in the wild

React

Page 149: ES6 patterns in the wild

React

As ES6 becomes native there are advantages beyond style

Page 150: ES6 patterns in the wild

Build a Library of Readable Code

Page 151: ES6 patterns in the wild

Lurk Around Github

Page 152: ES6 patterns in the wild

Lurk Around Github

Advanced Search

Page 153: ES6 patterns in the wild

Glance at your dependencies

Page 154: ES6 patterns in the wild

Have a few authors you like

Page 155: ES6 patterns in the wild

iv. Code can be beautiful

Page 156: ES6 patterns in the wild

function createThunkMiddleware(extraArgument) { return ({ dispatch, getState }) => next => action => { if (typeof action === 'function') { return action(dispatch, getState, extraArgument); } return next(action); }; }

const thunk = createThunkMiddleware(); thunk.withExtraArgument = createThunkMiddleware;

export default thunk;

Page 157: ES6 patterns in the wild

var data = [1,2,3,4]; var updated = []; for(var i = 0; i < data.length; i++) { updated.push(i*i); } return updated;

Page 158: ES6 patterns in the wild

const data = [1,2,3,4]; return data.map(n => n*n);

Page 159: ES6 patterns in the wild

Perfection is finally attained not when there is no longer anything to add, but when there is no longer anything to take away

Page 160: ES6 patterns in the wild

function createThunkMiddleware(extraArgument) { return ({ dispatch, getState }) => next => action => { if (typeof action === 'function') { return action(dispatch, getState, extraArgument); } return next(action); }; }

const thunk = createThunkMiddleware(); thunk.withExtraArgument = createThunkMiddleware;

export default thunk;

Page 161: ES6 patterns in the wild

Joe Morgan

Page 162: ES6 patterns in the wild

Joe Morgan

@joesmorgan

Page 163: ES6 patterns in the wild

Joe Morgan

@joesmorgan

thejoemorgan.com

Page 164: ES6 patterns in the wild

Joe Morgan

@joesmorgan

thejoemorgan.com

https://github.com/jsmapr1


Recommended