ecmascript 6 - the future is here

75
Sebastiano Armeli @sebarmeli http://courtreportingnyc.com/wp-content/uploads/2013/10/future-of-court-reporting.jpg

Upload: sebastiano-armeli-battana

Post on 20-May-2015

2.623 views

Category:

Technology


4 download

DESCRIPTION

Talk given at JsDay (Verona, Italy) - May 2014

TRANSCRIPT

Page 1: EcmaScript 6 - The future is here

Sebastiano Armeli @sebarmeli

http://courtreportingnyc.com/wp-content/uploads/2013/10/future-of-court-reporting.jpg

Page 2: EcmaScript 6 - The future is here

@sebarmeliSebastiano Armeli

Page 3: EcmaScript 6 - The future is here

ES6

Page 4: EcmaScript 6 - The future is here

History1995 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

Page 5: EcmaScript 6 - The future is here

History2005 2007 2008 2009 2011 2013 2014

ES 4 again!(Adobe, Mozilla,! Google)!

ES 3.1 !(Microsoft, Yahoo)! beginning

ES 5 spec finalized

(June) ECMA-262 Ed.5

(June) ES 6 proposals freeze

(Dec) ECMA-262 Ed.6 target release(27th April) latest spec draft

(July) Agreement: ES3.1 & ES-Harmony!!ES3.1 becomes ES5

Page 6: EcmaScript 6 - The future is here

ECMA-262TC39

ES 4

ES-HarmonyES.Next

ES 6

ECMA

ES 7es-discuss

Page 7: EcmaScript 6 - The future is here

ES-Harmony Proposal

ES 6 Candidate

ECMA-262 Ed.6 standardized

ES 6 Specification Draft

TC39 Process

Strawman Proposal

Page 8: EcmaScript 6 - The future is here

Generators & Iteration

SummaryFunctions

Collections Modularity

Scoping / Calling

API improvements Proxies

Page 9: EcmaScript 6 - The future is here

Generators & Iteration

SummaryFunctions

Collections Modularity

Scoping / Calling

API improvements Proxies

Page 10: EcmaScript 6 - The future is here

(Fat) arrow functionvar y = (x) => x + 1 var y = function(x) {

return x + 1; }

ES6 ES5

Page 11: EcmaScript 6 - The future is here

(Fat) arrow function

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

ES6 ES5

No constructor

Syntax sugar

Lexical `this` binding

Page 12: EcmaScript 6 - The future is here

(Fat) arrow function

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

ES6 ES5

No constructor

Syntax sugar

Lexical `this` binding

Page 13: EcmaScript 6 - The future is here

(Fat) arrow function

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

ES6 ES5

No constructor

Syntax sugar

Lexical `this` binding

Page 14: EcmaScript 6 - The future is here

let x = (x) => {return x + 1}

var x = function(x) { return x + 1; }

let x = (x, y) => ({ x: x, y: y })

var x = function(x, y) { return { x: x, y: y }; }

ES6 ES5

Page 15: EcmaScript 6 - The future is here

let map = words => words.map((w) => w.length);

var map = function(words) { return words.map(function(w) { return w.length; } }

ES6

ES5

map([‘sea’, ‘beach’, ‘do’]); // [3,5,2]

Page 16: EcmaScript 6 - The future is here

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

Page 17: EcmaScript 6 - The future is here

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

ES6

Page 18: EcmaScript 6 - The future is here

Object.getPrototypeOf(() => {})

Page 19: EcmaScript 6 - The future is here

Object.getPrototypeOf(() => {})

Function.prototype

Page 20: EcmaScript 6 - The future is here

When to use ‘function’ ?

Page 21: EcmaScript 6 - The future is here

Constructors

Generators

Methods in obj

Page 22: EcmaScript 6 - The future is here

Generators & Iteration

SummaryFunctions

Collections Modularity

Scoping & Calling

API improvements Proxies

Page 23: EcmaScript 6 - The future is here

Block ScopingEach BLOCK has got its lexical environment

let/const bind variables to the lexical environment

Variables declared with let/const are NOT hoisted

Page 24: EcmaScript 6 - The future is here

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

Page 25: EcmaScript 6 - The future is here

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” }());

Page 26: EcmaScript 6 - The future is here

const(function() { const X; X = “foo”; // ERROR: x unitialized }());

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

Page 27: EcmaScript 6 - The future is here

Block functions

if (true) { function fn () {} } !

fn(); // ERROR!

Page 28: EcmaScript 6 - The future is here

Destructing arrayvar [x,y] = [‘a’, ‘b’]; !

console.log(x); // ‘a’ !

console.log(y); // ‘b’ !

!

var [x,y] = [y, x]; !

console.log(x); // ‘b’

Page 29: EcmaScript 6 - The future is here

Destructing object

var obj = {width: 50, height: 100}; !

var {width, height} = obj; !

console.log(width); // 50

Page 30: EcmaScript 6 - The future is here

Destructing multiple return value

var fn = function(){ return [“50”, “100”]; } !

var [width, height] = fn(); !

console.log(width); //50 console.log(height); //100

Page 31: EcmaScript 6 - The future is here

Default values

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

Page 32: EcmaScript 6 - The future is here

Default values

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

function(foo = “a”) {}

Page 33: EcmaScript 6 - The future is here

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

Page 34: EcmaScript 6 - The future is here

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

fn(“a”, “b”, “c”); !

// b // c

Page 35: EcmaScript 6 - The future is here

Spread operator

function fn(a, b, c) {} !

var array = [“A”, “B”, “C”];

fn.apply(null, array);

Page 36: EcmaScript 6 - The future is here

function fn(a, b, c) {} !

var array = [“A”, “B”, “C”];

fn.apply(null, array);

fn(…array);

Spread operator

Page 37: EcmaScript 6 - The future is here

function fn(a, b, c) { var array = Array.prototype. slice.apply(arguments); }

Spread operator

Page 38: EcmaScript 6 - The future is here

function fn(a, b, c) { var array = Array.prototype. slice.apply(arguments); var array = […arguments]; }

Spread operator

Page 39: EcmaScript 6 - The future is here

Generators & Iteration

SummaryFunctions

Collections Modularity

Scoping / Calling

API improvements Proxies

Page 40: EcmaScript 6 - The future is here

for-of

for-of loop on ‘iterables’

Arrays/Sets/Maps are ’iterables’

for-in limitations

Page 41: EcmaScript 6 - The future is here

Iterable{ iterator: function() -> iterator }

{ next: function() -> any }

Iterators

Page 42: EcmaScript 6 - The future is here

for-ofvar array = [“a”, “b”, “c”]; !

for (let el of array) { console.log(el); } !

// “a” // “b” // “c”

Page 43: EcmaScript 6 - The future is here

Array comprehension

var array = [1, 2, 11, 20]; !

var array_c = [x (for x of array) (if x > 10)]; !

// [11, 20]

Page 44: EcmaScript 6 - The future is here

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}

Page 45: EcmaScript 6 - The future is here

!

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”

Page 46: EcmaScript 6 - The future is here

!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

Page 47: EcmaScript 6 - The future is here

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

Generators are iterables

Page 48: EcmaScript 6 - The future is here

Generators & Iteration

SummaryFunctions

Collections Modularity

Scoping / Calling

API improvements Proxies

Page 49: EcmaScript 6 - The future is here

SetSet of values - NO duplicates

Different type of values

add(key)/ has(key) / delete(key)

entries() -> Iterator

Page 50: EcmaScript 6 - The future is here

let countries = new Set(); countries.add(“US”); countries.add(“Italy”); countries.add(“US”); !

countries.values().next().value; // “US” !

countries.values().next().value; // “Italy” !

for(let country of countries) { console.log(country); }

Page 51: EcmaScript 6 - The future is here

Mapkey-value

Different type of values

Object can be keys

get(key)/ has(key) / set(key,val)

Page 52: EcmaScript 6 - The future is here

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

dict.get(“A”); // “1” dict.delete(“B”); !

for(let w of dict.keys()) { console.log(w); // “A” } !

for(let w of dict.values()) { console.log(w); // “A” }

Page 53: EcmaScript 6 - The future is here

WeakMapAvoid memory leaks

Reference to the key obj held weakly

Key must be an object

No iterators methods

Page 54: EcmaScript 6 - The future is here

Generators & Iteration

SummaryFunctions

Collections Modularity

Scoping / Calling

API improvements Proxies

Page 55: EcmaScript 6 - The future is here

Module!module “ads” { export function register(ad) { return ad; } }

!

import {register} from “ads”; var app = { doIt: function() { register({}); } } export app;

app.js

lib/ads.js

Page 56: EcmaScript 6 - The future is here

!module “widget” {…} module “widget/button” {…} module “widget/text” {…} !!import ‘lib/ad’ as c; import { meth as method } from ‘a'; !!export default class {}; // Ad.js import Ad from ‘ad'; // app.js

Page 57: EcmaScript 6 - The future is here

\

Loader API

System.load('http://json.org/modules/json2.js', function(JSON) { alert(JSON.stringify([0, {a: true}])); });

System.import

Configure module loading

Page 58: EcmaScript 6 - The future is here

Class / Subclass!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; } }

Page 59: EcmaScript 6 - The future is here

!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() { return Animal.prototype.toString.call(this) + “ owned by ” + this.ownerName; }

Page 60: EcmaScript 6 - The future is here

Generators & Iteration

SummaryFunctions

Collections Modularity

Scoping / Calling

API improvements Proxies

Page 61: EcmaScript 6 - The future is here

String methods

startsWith()

endsWith()

contains()

Page 62: EcmaScript 6 - The future is here

Number methods

Number.isInteger(num)

Number.isNaN(num)

Number.isFinite(num)

Page 63: EcmaScript 6 - The future is here

Array methodsvar divs = document.querySelectorAll("div"); Array.from(divs); !// [<div></div>, </div></div>] !

Array.of(10, 11); !// [10, 11] !

!

Page 64: EcmaScript 6 - The future is here

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); } !

!

Page 65: EcmaScript 6 - The future is here

Object methods

Object.setPrototypeOf(obj, proto)

Object.assign(obj, mixin)

Object.is(value1, value2)

Page 66: EcmaScript 6 - The future is here

var object = { method() { return “a”; } } object.method(); // “a” !

Page 67: EcmaScript 6 - The future is here

var object = { method() { return “a”; } } object.method(); // “a” !

function f(x, y) { return {x: x, y: y};}

function f(x, y) { return {x, y}; } =

Page 68: EcmaScript 6 - The future is here

Math methods

Math.log2()

Math.log10()

Math.sinh()

Math.cosh()

Page 69: EcmaScript 6 - The future is here

Generators & Iteration

SummaryFunctions

Collections Modularity

Scoping

API improvements Proxies

Page 70: EcmaScript 6 - The future is here

Proxies

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

Page 71: EcmaScript 6 - The future is here

Proxiesfunction 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/

Page 72: EcmaScript 6 - The future is here

Generators & Iteration

RecapFunctions

Collections Modularity

Scoping / Calling

API improvements Proxies

Page 73: EcmaScript 6 - The future is here

Promises

Symbols

Generator Expressions

Quasi-literals (template-strings)

Page 74: EcmaScript 6 - The future is here

Tools

Traceur compiler (Google)

es6-transpiler

es6-module-transpiler (Square)

es6-shim

defs.js

Page 75: EcmaScript 6 - The future is here

http://wiki.ecmascript.org/doku.php

https://people.mozilla.org/~jorendorff/es6-draft.html

http://kangax.github.io/compat-table/es6/

http://esdiscuss.org/

@sebarmeliSebastiano Armeli