Slide 1

Slide 1 text

Sebastiano Armeli @sebarmeli http://html5hub.com/wp-content/uploads/2013/11/es6-hiway-sign.png

Slide 2

Slide 2 text

@sebarmeli Sebastiano Armeli

Slide 3

Slide 3 text

ES6

Slide 4

Slide 4 text

History 1995 1996 1997 1998 1999 2000 2003 (May) B. Eich invented Mocha (Dec) LiveScript renamed to JavaScript JSScript (June) ECMA-262 Ed.1! ! by TC39 committee ECMA-262 Ed.2 ECMA-262 Ed.3 ECMA-262 Ed.4 started ECMA-262 Ed.4 abandoned (Sep) Mocha renamed to LiveScript

Slide 5

Slide 5 text

History 2005 2007 2008 2009 2011 2014 2015 ES 4 again! (Adobe, Mozilla,! Google)! ES 3.1 ! (Microsoft, Yahoo)! beginning ES 5 spec finalized (June) ECMA-262 Ed.5 (Dec) ES 6 spec completion (Mar) Start publication! (Jun) ECMA-262 Ed.6 ! target release (July) Agreement:! ES3.1 & ES-Harmony! ! ES3.1 becomes ES5

Slide 6

Slide 6 text

ECMA-262 TC39 ES 4 ES-Harmony ES.Next ES 6 ECMA ES 7 es-discuss

Slide 7

Slide 7 text

Iteration & Generators Summary Arrow Functions Collections Modularity / Classes / Templates Scoping / Destructing / Parameters API improvements Proxies

Slide 8

Slide 8 text

Iteration & Generators Summary Collections Modularity / Classes / Templates Scoping / Destructing / Parameters API improvements Proxies Arrow Functions

Slide 9

Slide 9 text

(Fat) arrow function var y = (x) => x + 1 var y = function(x) { return x + 1; } ES6 ES5

Slide 10

Slide 10 text

(Fat) arrow function var y = function(x) { return x + 1; } ES6 ES5 Syntax sugar var y = (x) => x + 1

Slide 11

Slide 11 text

(Fat) arrow function var y = function(x) { return x + 1; } ES6 ES5 Syntax sugar Lexical `this` binding var y = (x) => x + 1

Slide 12

Slide 12 text

(Fat) arrow function var y = function(x) { return x + 1; } ES6 ES5 No constructor Syntax sugar Lexical `this` binding var y = (x) => x + 1

Slide 13

Slide 13 text

var y = (x) => {return x + 1} var y = function(x) { return x + 1; } ES6 ES5

Slide 14

Slide 14 text

var y = (x) => {return x + 1} var y = function(x) { return x + 1; } var z = (x, y) => ({ x: x, y: y }) var z = function(x, y) { return { x: x, y: y }; } ES6 ES5

Slide 15

Slide 15 text

var mapFn = words => words.map((w) => w.length); var mapFn = function(words) { return words.map(function(w) { return w.length; } } ES6 ES5 mapFn([‘sea’, ‘beach’, ‘do’]); // [3,5,2]

Slide 16

Slide 16 text

var obj = { doIt: function(){}, handle: function(){ var that = this; document.addEventListener(‘click’, function(e) { that.doIt(); }); } } ES3

Slide 17

Slide 17 text

var obj = { doIt: function(){}, handle: function(){ document.addEventListener(‘click’, function(e) { this.doIt(); }.bind(this)); } } ES5 var obj = { doIt: function(){}, handle: function(){ var that = this; document.addEventListener(‘click’, function(e) { that.doIt(); }); } } ES3

Slide 18

Slide 18 text

var obj = { doIt: function(){}, handle: function(){ document.addEventListener(‘click’, (e) => this.doIt()); } } ES6

Slide 19

Slide 19 text

Object.getPrototypeOf(() => {})

Slide 20

Slide 20 text

Object.getPrototypeOf(() => {}) Function.prototype

Slide 21

Slide 21 text

When to use ‘function’ ?

Slide 22

Slide 22 text

Constructors Generators (Methods in object literals)

Slide 23

Slide 23 text

Summary Arrow Functions Collections API improvements Proxies Scoping / Destructing / Parameters Iteration & Generators Modularity / Classes / Templates

Slide 24

Slide 24 text

Block Scoping Each BLOCK has got its lexical environment let/const bind variables to the lexical environment Variables declared with let/const are NOT hoisted

Slide 25

Slide 25 text

var vs let (function() { console.log(y) // “undefined” if (true) { var y = “value”; } console.log(y) // “value” }());

Slide 26

Slide 26 text

var vs let (function() { if (true) { let y = “value”; } console.log(y) // ERROR!! }()); (function() { console.log(y) // “undefined” if (true) { var y = “value”; } console.log(y) // “value” }());

Slide 27

Slide 27 text

const (function() { const X; X = “foo”; // ERROR: x unitialized }()); (function() { const X = “foo”; X = “foo2”; // ERROR: x is read-only }());

Slide 28

Slide 28 text

Block functions if (true) { function fn () {} } ! fn(); // ERROR!

Slide 29

Slide 29 text

Destructing array var [x,y] = [‘a’, ‘b’]; ! console.log(x); // ‘a’ ! console.log(y); // ‘b’ ! ! var [x,y] = [y, x]; ! console.log(x); // ‘b’

Slide 30

Slide 30 text

Destructing object var obj = {width: 50, height: 100}; ! ! var {width: w, height: h} = obj; var {width, height} = obj; ! ! console.log(width); // 50 console.log(w); // 50 console.log(height); // 100 console.log(h); // 100

Slide 31

Slide 31 text

Multiple return value var fn = function(){ return [“50”, “100”]; } ! var [width, height] = fn(); ! console.log(width); //50 console.log(height); //100

Slide 32

Slide 32 text

var fn = function(){ return { foo: “bar”, fizz: “buzz” } } ! var {foo, fizz} = fn(); ! console.log(foo); //“bar” console.log(fizz); //“buzz”

Slide 33

Slide 33 text

Parameter default values function(foo) { foo = foo || “a”; }

Slide 34

Slide 34 text

function(foo) { foo = foo || “a”; } function(foo = “a”) {} Parameter default values

Slide 35

Slide 35 text

function fn(…args) { console.log(args); //[“a”, “b”, “c”] args.forEach(function(arg) { console.log(arg); }); } ! fn(“a”, “b”, “c”); ! // a // b // c Rest parameters

Slide 36

Slide 36 text

Rest parameters function fn(a, …args) { console.log(args); //[“b”, “c”] args.forEach(function(arg) { console.log(arg); }); } ! fn(“a”, “b”, “c”); ! // b // c

Slide 37

Slide 37 text

Spread operator function fn(a, b, c) {} ! var array = [“A”, “B”, “C”]; fn.apply(null, array);

Slide 38

Slide 38 text

function fn(a, b, c) {} ! var array = [“A”, “B”, “C”]; fn.apply(null, array); fn(…array); Spread operator

Slide 39

Slide 39 text

function fn({id, name}) { console.log(name); } ! fn({name: “Seb”}); // “Seb Named parameters

Slide 40

Slide 40 text

Iteration & Generators Summary Collections API improvements Proxies Arrow Functions Scoping / Destructing / Parameters Modularity / Classes / Templates

Slide 41

Slide 41 text

for-of for-of loop on ‘iterables’ and iterators Arrays/Sets/Maps are ’iterables’ for-in limitations

Slide 42

Slide 42 text

for-of var array = [“a”, “b”, “c”]; ! for (let el of array) { console.log(el); } ! // “a” // “b” // “c”

Slide 43

Slide 43 text

Iterable { @@iterator: function() -> iterator } { next: function() -> any } Iterators

Slide 44

Slide 44 text

Iterator var array = [“a”, “b”, “c”]; ! array.entries() // Array Iterator array.keys() // Array Iterator Iterator from Array, Map, Set

Slide 45

Slide 45 text

function* g() { yield “a”; yield “b”; } Generator var generator = g(); generator ‘constructor’ generator.next(); //{ value: “a”, done: false} generator.next(); //{ value: “b”, done: false} generator.next(); //{ value: undefined, done: true}

Slide 46

Slide 46 text

! function* g() { yield “a”; var retVal = yield “b”; return retVal; } var generator = g(); generator.next().value; //“a” generator.next().value; //“b” generator.next(“c”).value; //“c”

Slide 47

Slide 47 text

! function* asyncFn() { var data = yield getUser(); doSomethingElse(data); } function run(genFunction) { var generator = genFunction(); generator.next().value.then(function(val){ generator.next(val); }, function(err) { generator.throw(err); }); } run(asyncFn); Promise

Slide 48

Slide 48 text

for (let el of generator) { console.log(el); } Generators are iterables

Slide 49

Slide 49 text

Iteration & Generators Summary Collections API improvements Proxies Arrow Functions Scoping / Destructing / Parameters Modularity / Classes / Templates

Slide 50

Slide 50 text

Set NO duplicates values

Slide 51

Slide 51 text

Set NO duplicates values Different types in a set

Slide 52

Slide 52 text

Set NO duplicates values Different types in a set add(key)/ has(key) / delete(key)

Slide 53

Slide 53 text

Set NO duplicates values Different types in a set add(key)/ has(key) / delete(key) values() -> Iterator

Slide 54

Slide 54 text

let countries = new Set(); countries.add(“US”); countries.add(“Italy”); countries.add(“US”); ! countries // Set [“US”, “Italy”]

Slide 55

Slide 55 text

let countries = new Set([“US”, “Italy”]); countries // Set [“US”, “Italy”] ! ! ! countries.delete(“Italy”); countries // Set [“US”]

Slide 56

Slide 56 text

! for(let country of countries.values()) { console.log(country); // “US” } ! for(let country of countries) { console.log(country); // “US” }

Slide 57

Slide 57 text

Map {“foo” : “bar” }

Slide 58

Slide 58 text

Map {“foo” : “bar” } Keys can be objects

Slide 59

Slide 59 text

Map {“foo” : “bar” } Keys can be objects get(key); has(key); set(key,val)

Slide 60

Slide 60 text

Map {“foo” : “bar” } get(key); has(key); set(key,val) delete(key); clear(); forEach(); Keys can be objects

Slide 61

Slide 61 text

let dict = new Map(); dict.set(“A”, 1); dict.set(“B”, 2); ! dict // Map {“A”: 1, “B”: 2}

Slide 62

Slide 62 text

let dict = new Map(); dict.set(“A”, 1); dict.set(“B”, 2); ! dict // Map {“A”: 1, “B”: 2} ! dict.get(“A”); // “1” dict.delete(“B”);

Slide 63

Slide 63 text

for(let w of dict.keys()) { // Map Iterator console.log(w); // “A” } ! for(let w of dict.values()) { // Map Iterator console.log(w); // 1 } ! dict.clear(); dict.size; // 0

Slide 64

Slide 64 text

let dict = new Map([[“x”, 1], [“y”, 2]]); ! ! dict; // Map {x: 1, y: 2} ! ! ! for(let w of dict) { ! console.log(w); // [“x”, 1] // [“y”, 2] ! });

Slide 65

Slide 65 text

dict.forEach(function(val, key, map) { ! console.log(val); // x // y ! console.log(key); // 1 // 2 ! console.log(map); // Map { x: 1, y: 2} ! });

Slide 66

Slide 66 text

WeakMap Avoid memory leaks

Slide 67

Slide 67 text

WeakMap Avoid memory leaks Reference to the key obj held weakly

Slide 68

Slide 68 text

WeakMap Avoid memory leaks Reference to the key obj held weakly Keys must be an objects

Slide 69

Slide 69 text

WeakMap Avoid memory leaks Reference to the key obj held weakly Keys must be an objects No iterators methods

Slide 70

Slide 70 text

Object properties ! with ! Map / WeakMap

Slide 71

Slide 71 text

Summary Collections API improvements Proxies Arrow Functions Scoping / Destructing / Parameters Iteration & Generators Modularity / Classes / Templates

Slide 72

Slide 72 text

Object Literal ! let obj = { ! __proto__: parentObj, meth1(a,b) { ! } ! };

Slide 73

Slide 73 text

Module ! export function register(ad) { return ad; } ! import {register} from “ads”; var app = { doIt: function() { register({}); } } export app; app.js lib/ads.js

Slide 74

Slide 74 text

export default class {}; // Ad.js import Ad from ‘ad'; // app.'s ! ! import { meth as method } from ‘a’;

Slide 75

Slide 75 text

Class ! class Animal { constructor(name) { this.name = name; } toString() { return “This is: ” + this.name; } }

Slide 76

Slide 76 text

Subclass - super ! class Cat extends Animal { constructor(name, ownerName) { super(name); this.ownerName = ownerName; } ! toString() { return super() + “ owned by ” + this.ownerName; } }

Slide 77

Slide 77 text

! class Animal { constructor(name) { this.name = name; } toString() { return “This is: ” + this.name; } } class Cat extends Animal { constructor(name, ownerName) { super.constructor(name); this.ownerName = ownerName; } ! toString() { return super.toString() + “ owned by ” + this.ownerName; } }

Slide 78

Slide 78 text

! function Animal(name) { this.name = name; } ! Animal.prototype.toString = function() { return “This is: ” + this.name; }; ! function Cat(name, ownerName) { Animal.call(this, name); this.ownerName = ownerName; } ! Cat.prototype = Object.create(Animal.prototype); Cat.prototype.constructor = Cat; Cat.prototype.parent = Animal; ! Cat.prototype.toString = function() { var super = Animal.prototype.toString.call(this); return super + “ owned by ” + this.ownerName; };

Slide 79

Slide 79 text

Template strings var a = “hello”; var b = “world”; ! `${a} ${b}!`

Slide 80

Slide 80 text

Template strings var a = “hello”; var b = “world”; ! `${a} ${b}!` var multiline = `Hello world !!!`;

Slide 81

Slide 81 text

Summary Collections API improvements Proxies Arrow Functions Scoping / Destructing / Parameters Iteration & Generators Modularity / Classes / Templates

Slide 82

Slide 82 text

String methods String.prototype.startsWith(str) => boolean String.prototype.endsWith(str) => boolean String.prototype.contains(str) => boolean String.prototype.repeat(num) => string

Slide 83

Slide 83 text

Number methods Number.isInteger(num) => boolean Number.isNaN(num) => boolean Number.isFinite(num) => boolean …

Slide 84

Slide 84 text

Array methods Array.from(obj) => Array Array.prototype.entries => Iterator Array.of(…args) => Array Array.prototype.keys => Iterator Array.prototype.values => Iterator

Slide 85

Slide 85 text

var divs = document.querySelectorAll("div"); ! Array.from(divs); ! // [
, ] ! Array.of(10, 11); ! // [10, 11] !

Slide 86

Slide 86 text

var array = [“a”, “b”, “c”]; ! for (let [index, el] of array.entries()) { console.log(index, el); // 0 “a” // 1 “b” // 2 “c” } ! for (let index of array.keys()) { console.log(index); } ! for (let el of array.values()) { console.log(el); } !

Slide 87

Slide 87 text

Object methods Object.setPrototypeOf(obj, proto) Object.assign(obj, mixin) Object.is(value1, value2)

Slide 88

Slide 88 text

Math methods Math.log2(num) => num Math.log10(num) => num Math.sinh(num) => num Math.cosh(num) => num …

Slide 89

Slide 89 text

Summary Collections API improvements Proxies Arrow Functions Scoping / Destructing / Parameters Iteration & Generators Modularity / Classes / Templates

Slide 90

Slide 90 text

Proxies Proxy(targetObject, interceptors) ! Different use cases (logging, mocking) Meta-programming

Slide 91

Slide 91 text

var obj = {num: 1}; ! obj = new Proxy(obj, { set: function (target, property, value) { target[property] = value + 1; } }); ! obj.num = 2 // [[Set]] console.log(obj.num); // 3 Proxies

Slide 92

Slide 92 text

function createDefensiveObject(target) { return new Proxy(target, { get: function(target, property) { if (property in target) { return target[property]; } else { throw new ReferenceError(); } } }); } ! var obj = createDefensiveObject({name: “Seb”}); console.log(obj.lastname); //ReferenceError http://www.nczonline.net/blog/2014/04/22/creating-defensive-objects-with-es6-proxies/ Proxies

Slide 93

Slide 93 text

Iteration & Generators Recap Arrow Functions Collections Scoping / Destructing / Parameters API improvements Proxies Modularity / Classes / Templates

Slide 94

Slide 94 text

Promises Symbols Better Unicode support Optimized tail calls Other Features..

Slide 95

Slide 95 text

ES6 today Traceur compiler (Google) es6-transpiler es6-module-transpiler (Square) es6-shim defs.js

Slide 96

Slide 96 text

http://wiki.ecmascript.org https://people.mozilla.org/~jorendorff/es6-draft.html http://kangax.github.io/compat-table/es6/ http://esdiscuss.org/ @sebarmeli Sebastiano Armeli