ES 2015

ES 2015

Co nového u JavaScriptu

Ondřej Žára, @0ndras

Obsah

  1. Jak si stojí ES 2015
  2. Novinky ES 2015: syntaxe
  3. Novinky ES 2015: rozšíření ES5
  4. Kde a jak si to lze vyzkoušet?

ES 2015

Ukázka

import * as net from "net.js"; function response(data) { data .filter( item => item.enabled ) .forEach( (item, index) => { console.log(item, index); }); } export function request(url) { return net.request("/data.txt").then(response, alert); }

let, const

const N = 8; N = 4; // exception let x = 1; if (true) { let x = 2; } alert(x); // 1

Arrow functions

var square = a => a*a; var add = (a, b) => a+b; // lexical this setTimeout( () => this.doStuff(), 1000 );

Enhanced object literals

var x = 42; var obj = { x, // "x":42 y() { return x; }, ["data_" + x]: x // "data_42":42 }

Template string literals

var x = "world"; var y = `hello ${x}`; var z = `this is a very long string`; // html je uživ. funkce, která dostane jednotlivé tokeny k naformátování html`<div> ${unsafe} </div>`;

Destructuring

var [a, b, c] = [1, 2, 3]; var f = function() { return {x:42}; } var { x } = f();

Default + Rest + Spread

var f = function(x, y = 12) { return x + y; } f(10); // 22 var f = function(x, ...y) { alert(y.length); } f(1, 2, 3); // 2 var f = function(a, b, c) { return c; } f(...[1, 2, 3]); // 3

Classes

class B extends A { constructor(x) { super(); // v konstruktoru dědící třídy povinné; před ním neexistuje this this.x = x; } get something() { /* .... */ } f1() { super.f1(); return this.x; } static f2() {} }

Modules

// a.js export var A = function() {}; export default function() {}; // b.js import { A } from "a"; // pozor na cestu! A(); import myLocalName from "a"; // default

{Weak,}{Map,Set}

var s = new Set(); s.add("hello").add("goodbye").add("hello"); s.size == 2; s.has("hello") == true; var m = new Map(); m.set("hello", 42); m.set(s, 34); m.get(s) == 34;

…a to ještě není všechno.

Symbols

(function() { var moneyKey = Symbol("money"); typeof(moneyKey) == "symbol"; var Person = function() { this[moneyKey] = 10000; } var person = new Person(); person.money == undefined; Object.getOwnPropertySymbols(person); // :-( })();

Iterators + for..of

let fibonacci = { [Symbol.iterator]() { let pre = 0, cur = 1; return { next() { [pre, cur] = [cur, pre + cur]; return { done: false, value: cur } } } } }

Iterators + for..of

for (var n of fibonacci) { if (n > 1000) break; console.log(n); }

Generators

var generator = function*() { var tmp = 1; while (true) { tmp *= 3; yield tmp; } } var iterator = generator(); iterator.next().value; // 3, next() vrací i done:true/false iterator.next().value; // 9 iterator.next().value; // 27

Proxies

var obj = {}; var interceptor = { get: function (receiver, name) { return `Hello, ${name}!`; } }; var p = new Proxy(obj, interceptor); p.world === "Hello, world!"

Reflect

Reflect.defineProperty(obj, name, descriptor); Reflect.construct(F, args); Reflect.get(obj, property, thisForGetter); /* ... */

Rozšíření ES5 #1

Number.EPSILON Number.MAX_SAFE_INTEGER Number.MIN_SAFE_INTEGER Number.isInteger(Infinity) // false Number.isNaN("NaN") // false Math.acosh(3) // 1.762747174039086 Math.hypot(3, 4) // 5 Math.imul(Math.pow(2, 32)-1, Math.pow(2, 32)-2) // 2 Math.sign(5) // 1 Math.trunc(3.1) // 3 /* ... */ "abc".repeat(3) // "abcabcabc"

Rozšíření ES5 #2

Array.from(document.querySelectorAll("*")) // real Array Array.of(1, 2, 3) // without special one-arg behavior [0, 0, 0].fill(7, 1) // [0,7,7] [1, 2, 3].find(x => x == 3) // 3 [1, 2, 3].findIndex(x => x == 2) // 1 [1, 2, 3, 4, 5].copyWithin(3, 0) // [1, 2, 3, 1, 2] ["a", "b", "c"].entries() // iterator [0, "a"], [1,"b"], [2,"c"] ["a", "b", "c"].keys() // iterator 0, 1, 2 ["a", "b", "c"].values() // iterator "a", "b", "c" Object.assign(target, { source: "data" });

Ostatní

ES 2015 už dnes?

Jak zkoušet ES 2015?

Transpilace ES 2015

Babel v praxi

Babel a moduly

ES 2016

Prostor pro otázky