by Bruno Buccolo
1. Magnify your cursor on Accessibility -> Display.Browser Support (ECMAScript 6)
https://kangax.github.io/compat-table/es6/ It's not like updating Ruby on servers.Transpile(ECMAScript 6) => JavaScripthttps://babeljs.io https://github.com/google/traceur-compiler
Babel: ECMAScript 6 or Newer
let count = 1 count += 1 const number = 1 number += 1 // BOOM!
// Bad: `var` (Function scope) if (something) { var count = 1 } console.log(count) // 1
// Good: `let` (Block scope) if (something) { let value = 123 console.log(value) } console.log(value) // Uncaught ReferenceError: value is not defined
let set = new Set() set.add("hello").add("goodbye").add("hello") set.size === 2 set.has("hello") === true
let map = new Map() map.set("hello", 42) map.set("goodbye", 34) map.get("goodbye") === 34 map.size === 2 for (let [key, val] of map.entries()) { // hack, hack, hack }
// handleClick({ target: { x: 1, y: 2 } }) function handleClick(event) { let x = event.target.x let y = event.target.y console.log(x, y) } function handleClick2(event) { let {x, y} = event.target console.log(x, y) } function handleClick3({ target: { x, y } }) { console.log(x, y) }
let numbers = [1, 5, 10] let one = numbers[0] let ten = numbers[2] let [one, , ten] = numbers console.log(one, ten) // 1, 10 let [one, five] = numbers console.log(one, five) // 1, 5 let [one, ...rest] = numbers console.log(one, rest) // 1, [5, 10]
function* generateId(){ let index = 0 while(index < 3) { yield index++ } } let generator = generateId() console.log(generator.next().value); // 0 console.log(generator.next().value); // 1 console.log(generator.next().value); // 2 console.log(generator.next().value); // undefined
let fibonacci = { [Symbol.iterator]() { let pre = 0, cur = 1 return { next () { [ pre, cur ] = [ cur, pre + cur ] return { done: false, value: cur } } } } } for (let n of fibonacci) { if (n > 10) { console.log(n) // 13 break } }
let evens = [0, 2, 4, 6, 8] let odds = evens.map(even => even + 1) // [1, 3, 5, 7, 9] let numbers = evens.map((even, index) => index) // [0, 1, 2, 3, 4] let fives = numbers.map((number) => { return number * 5 }) // [0, 5, 10, 15, 20]
function after(duration = 0) { return new Promise((resolve, reject) => { setTimeout(resolve, duration) }) } // serial after(1000).then(() => { console.log("I'm done!") after(2000).then(() => { console.log("I'm done too!") }) }) // parallel Promise.all([after(1000), after(2000)]).then(() => { console.log("We're done!") })
function greeter(name) { return `How are you, ${ name }?` }
class Shape { constructor (id, x, y) { this.id = id this.move(x, y) } move (x, y) { this.x = x this.y = y } } const square = new Shape(1, 4, 5) square.move(0, 0)
class Counter extends React.Component { constructor(props) { super(props) this.state = { count: props.initialCount } } tick() { this.setState({ count: this.state.count + 1 }) } render() { return ( <div onclick="{this.tick.bind(this)}"> Clicks: {this.state.count } </div> ) } } Counter.propTypes = { initialCount: React.PropTypes.number } Counter.defaultProps = { initialCount: 0 }
import {sum, pi} from "lib/math" alert("2π = " + sum(pi, pi))
Potential interop problems with ES6