147
ES6 ES6 Next Generation Javascript Next Generation Javascript By and Ramesh Nair Grégoire Charvet https://github.com/hiddentao/es6-slides

ES6 - Next Generation Javascript

Embed Size (px)

DESCRIPTION

A brief look at the new features coming in Javascript ES6: - Scope and control - Iterators and Generators - Collections - Typed objects - Direct proxies - Template strings - API improvements - Modularity

Citation preview

Page 1: ES6 - Next Generation Javascript

ES6ES6Next Generation JavascriptNext Generation Javascript

By and Ramesh Nair Grégoire Charvet

https://github.com/hiddentao/es6-slides

Page 2: ES6 - Next Generation Javascript

SpeakersSpeakersGrégoire Charvet (geekingfrog.com)

Full time node.js developper

Passionate about the web

Working on the web for almost 2 years now

Ramesh Nair (hiddentao.com)

Full time Javascript/Node developper

Also worked with PHP, Python, Java, C++

Loves optimizing for performance

Page 3: ES6 - Next Generation Javascript

Rapid history of javascriptRapid history of javascript

Page 4: ES6 - Next Generation Javascript

The birthThe birthCreated in 10 days in 1995 (by Brendan Eich) at NetscapeBrought to ECMA a year later to standardize itjavaScript has nothing to do with java

Page 5: ES6 - Next Generation Javascript

Early historyEarly historyECMAScript 2 in 98, 3 in 99War with Microsoft -> ES4 has never been adoptedIn 2005, Microsoft introduced ajaxIn 2009, all parties agreed to move forward with ES5 +harmony process

Page 6: ES6 - Next Generation Javascript

NowNowJavascript most well known implementation of ECMAScript(with ActionScript)Javascript is the assembly of the webConfusing version number, JS 1.8 correspond to ES6

Page 7: ES6 - Next Generation Javascript

ES6ES6ES6 work started in 2011As of now (Feb 2014), ES6 is not yet adopted (but it's almostthere)Major milestoneNot 'production ready' yet

Page 8: ES6 - Next Generation Javascript

What we will cover todayWhat we will cover todaySupportScope and controlIterators and GeneratorsCollectionsTyped objectsDirect proxiesTemplate stringsAPI improvementsModularity

Page 9: ES6 - Next Generation Javascript

SupportSupport

24 30 ✗

For chrome, need to enable the experimental js features

full table

Page 10: ES6 - Next Generation Javascript

Node.js supportNode.js supportNode.js: get the latest 0.11 and add the flag --harmony

Support is the same as chrome (V8)

Page 11: ES6 - Next Generation Javascript

Safari supportSafari supportAlmost the same as IE (so quasi inexistant)

Page 12: ES6 - Next Generation Javascript

ScopingScoping

24 30 11

Page 13: ES6 - Next Generation Javascript

Block scopingBlock scopingFinally !

Page 14: ES6 - Next Generation Javascript

BeforeBeforefor(var i=10; i>0 ; i--) { // do stuff with i}console.log(i); // 0

Page 15: ES6 - Next Generation Javascript

letletfor(let i=10; i>10; i--) {}console.log(i); // `i is not defined`

Page 16: ES6 - Next Generation Javascript

Works with if tooWorks with if toovar log = function(msg) {};if(someCond) { let log = Math.log; // do some stuff}log("I'm done here");

Page 17: ES6 - Next Generation Javascript

Easier closuresEasier closuresbroken example

var a = ['rhum', 'banana', 'nutella'];for(var i = 0, n=a.length; i<n; i++) { var nomnom = a[i]; setTimeout(function() { console.log(nomnom); }, 500*(i+1))}

Page 18: ES6 - Next Generation Javascript

Easy fixEasy fixvar a = ['rhum', 'banana', 'nutella'];for(var i = 0, n=a.length; i<n; i++) { let nomnom = a[i]; setTimeout(function() { console.log(nomnom); }, 500*(i+1))}

Page 19: ES6 - Next Generation Javascript

let('s) recaplet('s) recapworks like varscope is defined by the current block ({ })

Page 20: ES6 - Next Generation Javascript

constconstLike let , but define read-only constant declarations.

'use strict';const i = 10;i = 5; // throw error

Page 21: ES6 - Next Generation Javascript

DestructurationDestructuration

24 ✗ ✗

Page 22: ES6 - Next Generation Javascript

With arrayWith array// Assignmentvar [day, month, year] = [19, 02, 2014];

// Swap two values.var x=3, y=4;[x, y] = [y, x];

Page 23: ES6 - Next Generation Javascript

Array with functionsArray with functionsvar now = function() { return [19, 02, 2014]; }var [day, month] = now();var [, , year] = now();

Page 24: ES6 - Next Generation Javascript

With objectsWith objectsvar now = function() { return { d: 19, m: 2, y: 2014}}

var {d: day, m: month} = now();// day: 19// month: 2

Page 25: ES6 - Next Generation Javascript

As argument of a functionAs argument of a functionrecipes = [ { name: 'burger', calorie: 215 }, { name: 'pizza', calorie: 266 } ];

recipes.forEach(function ({ name: name, calorie: calorie }) { console.log(name, calorie); });

Page 26: ES6 - Next Generation Javascript

Default function parametersDefault function parameters

24 30 ✗

Page 27: ES6 - Next Generation Javascript

Ability to define default value for functionsAbility to define default value for functionsparamaters.paramaters.

No more:

function(a) { if(!a) { a = 10; } // do stuff with a}

Page 28: ES6 - Next Generation Javascript

NowNowfunction(a=10) { // do stuff with a}

Page 29: ES6 - Next Generation Javascript

Undefined and nullUndefined and nullundefined will trigger the evaluation of the default value,

not null

function point (x, y=1, z=1) { return console.log(x, y, z);}

point(10, null); // 10, null, 1

Page 30: ES6 - Next Generation Javascript

ArityArityNumber of parameters without default value

(function(a){}).length // 1(function(a=10){}).length // 0(function(a=10, b){}).length // 1

Page 31: ES6 - Next Generation Javascript

Rest parametersRest parameters

24 ✗ ✗

Page 32: ES6 - Next Generation Javascript

Better than Better than argumentsargumentsfunction(name) { console.log(name); arguments[0] = 'ME !'; console.log(name); // ME ! Array.isArray(arguments); // false}

Page 33: ES6 - Next Generation Javascript

NowNowfunction(...args) { Array.isArray(args); // true // do some stuff}

function(name, ...more) {

}

Page 34: ES6 - Next Generation Javascript

ExampleExamplevar humblify = function(name, ...qualities) {

console.log('Hello %s', name);

console.log('You are '+qualities.join(' and '));

}

humblify('Greg', 'awesome', 'the master of the universe');

// Hello Greg

// You are awesome and the master of the universe

Page 35: ES6 - Next Generation Javascript

RestrictionsRestrictionsRest parameters can only be the last parameter

// incorrectfunction(...args, callback) {}

Page 36: ES6 - Next Generation Javascript

DetailsDetailsRest parameter is always an array

function f(name, ...more) { Array.isArray(more); // always true return more.length;}f(); // returns 0

Page 37: ES6 - Next Generation Javascript

ArityArityDoes not include the rest parameter

(function(a) {}).length // 1(function(a, ...rest) {}).length // 1

Page 38: ES6 - Next Generation Javascript

SpreadSpread

24 (with array) 27-28 (with functions)

✗ ✗

Page 39: ES6 - Next Generation Javascript

Expand expression where multipleExpand expression where multiplearguments or multiple element are neededarguments or multiple element are needed

Page 40: ES6 - Next Generation Javascript

More powerful arrayMore powerful arraymanipulationmanipulation

Usecase: create new array with an existing one inside it:

var from = [1, 2];// wants: [0, 1, 2, 3] ie [0, from, 3]

Page 41: ES6 - Next Generation Javascript

With es5With es5a.unshift(0);a.push(3);// and splice is here also

With es6With es6var total = [0, ...from, 3];

Page 42: ES6 - Next Generation Javascript

Converting any array-likeConverting any array-like

Page 43: ES6 - Next Generation Javascript

Array like ???Array like ???Object with a length property

Can access elements with []

var fake = { 0: 'I am', 1: 'not', 2: 'an aray', length: 3};

Page 44: ES6 - Next Generation Javascript

Array like in the wildArray like in the wildFunction's argumentsAll nodeList from the DOM

Page 45: ES6 - Next Generation Javascript

Before:

var nodes = document.querySelectorAll('p');var nodes = [].slice.call(nodes);

And now:

nodes = [...nodes];

Page 46: ES6 - Next Generation Javascript

Array conversionArray conversionBetter way:

Array.from(document.querySelectorAll('p'));

Out of the scope of the talk.

Page 47: ES6 - Next Generation Javascript

Spread with functionsSpread with functionsA better A better applyapply

var f = function(one, two, three) {}var a = [1, 2, 3];f.apply(null, a);

Page 48: ES6 - Next Generation Javascript

Apply ?Apply ?Function.prototype.applyfun.apply(thisArg, [argsArray])

Page 49: ES6 - Next Generation Javascript

Apply exampleApply examplefunction f() { for(let i=0; i<arguments.length; ++i) console.log(arguments[i]);}

f.apply(this, ['one', 2, 'foo']);// one// 2// foo

Page 50: ES6 - Next Generation Javascript

With es6's spreadWith es6's spreadvar f = function(one, two, three) {}var a = [1, 2, 3];f(...a);

Page 51: ES6 - Next Generation Javascript

Sweet syntaxSweet syntaxvar f = function(a, b, c, d, e, f) {};var a = [1, 2];f(-1, ...a, 3, ...[-3, -4]);

Page 52: ES6 - Next Generation Javascript

Apply for Apply for newnewWith es5, one cannot use apply with new .

var Constructor = function() { // do some stuff}var c = new Constructor.apply({}, []); //invalid

But now:

var dataFields = readDateFields(database);var d = new Date(...dateFields);

Page 53: ES6 - Next Generation Javascript

Better pushBetter pushTo push multiple elements:

var a = [];var toPush = [1, 2, 3];a.push.apply(a, toPush);

And now:

a.push(...toPush);

Page 54: ES6 - Next Generation Javascript

Next...Next...

IteratorsIterators

24 ✗ ✗

Page 55: ES6 - Next Generation Javascript

An iterator lets you iterate over the contents of an object.

In ES6, an iterator is an object with a next() method whichreturns {done, value} tuples.

An iterable is an object which can return an iterator.

Page 56: ES6 - Next Generation Javascript

Arrays are iterable:

var a = [1,2,3], i = a.iterator();

console.log(i.next()); // {done: false, value: 1}console.log(i.next()); // {done: false, value: 2}console.log(i.next()); // {done: false, value: 3}console.log(i.next()); // {done: true, value: undefined}

Page 57: ES6 - Next Generation Javascript

The for-of loop can be used to simplify iterations:

var a = [1,2,3];

for (var num of a) { console.log(num); // 1, 2, 3}

Page 58: ES6 - Next Generation Javascript

Array comprehensions:

var a = [ { color: 'red' }, { color: 'blue' }];

[ x.color for (x of a) if ('blue' === x.color) ]

// [ 'blue' ]

Page 59: ES6 - Next Generation Javascript

We can make any object iterable:

function ClassA() { this.elements = [1, 2, 3];}

Page 60: ES6 - Next Generation Javascript

By adding the @@iterator method:

ClassA.prototype['@@iterator'] = function() { return { elements: this.elements, index: 0, next: function() { if (this.index >= this.elements.length) return { done: true, value: undefined }; else return { done: false, value: this.elements[this.index++] };}}};

Page 61: ES6 - Next Generation Javascript

We can iterate over the Object:

var col = new ClassA();

for (var num of col) { console.log(num); // 1, 2, 3}

Page 62: ES6 - Next Generation Javascript

GeneratorsGenerators

27 30 ✗

Page 63: ES6 - Next Generation Javascript

A generator is a special type of iterator.

A generator provides a throw() method. Its next()method accepts a parameter.

A generator function acts as a constructor for a generator.

Generators offer a clean way of doing asynchronousprogramming!

Page 64: ES6 - Next Generation Javascript

Simple example:

var helloWorld = function*() { yield 'hello'; yield 'world';}

var hw = helloWorld();console.log( hw.next() ); // { value: 'hello', done: false }console.log( hw.next() ); // { value: 'world', done: false }console.log( hw.next() ); // { value: undefined, done: true }

Page 65: ES6 - Next Generation Javascript

Passing values back to generator:

var helloWorld = function*() { var nextWord = yield 'hello'; yield nextWord;}

var hw = helloWorld();

console.log( hw.next() ); // { value: 'hello', done: false }console.log( hw.next('world') ); // { value: 'world', done: false }console.log( hw.next() ); // { value: undefined, done: true }

Page 66: ES6 - Next Generation Javascript

Let's take it step-by-step to see how code gets suspended...

Page 67: ES6 - Next Generation Javascript

var helloWorld = function*() { console.log('Yield 1...'); var nextWord = yield 'hello'; console.log('Yield 2...'); yield nextWord; console.log('No more yields...');}

var hw = helloWorld();

console.log( hw.next() );console.log( hw.next('world') );console.log( hw.next() );

Page 68: ES6 - Next Generation Javascript

var helloWorld = function*() { console.log('Yield 1...'); var nextWord = yield 'hello'; console.log('Yield 2...'); yield nextWord; console.log('No more yields...');}

var hw = helloWorld();

console.log( hw.next() );console.log( hw.next('world') );console.log( hw.next() );

Page 69: ES6 - Next Generation Javascript

var helloWorld = function*() { console.log('Yield 1...'); var nextWord = yield 'hello'; console.log('Yield 2...'); yield nextWord; console.log('No more yields...');}

var hw = helloWorld();

console.log( hw.next() );console.log( hw.next('world') );console.log( hw.next() );

Page 70: ES6 - Next Generation Javascript

var helloWorld = function*() { console.log('Yield 1...'); var nextWord = yield 'hello'; console.log('Yield 2...'); yield nextWord; console.log('No more yields...');}

var hw = helloWorld();

console.log( hw.next() );console.log( hw.next('world') );console.log( hw.next() );

Page 71: ES6 - Next Generation Javascript

var helloWorld = function*() { console.log('Yield 1...'); var nextWord = yield 'hello'; console.log('Yield 2...'); yield nextWord; console.log('No more yields...');}

var hw = helloWorld();

console.log( hw.next() );console.log( hw.next('world') );console.log( hw.next() );

Page 72: ES6 - Next Generation Javascript

var helloWorld = function*() { console.log('Yield 1...'); var nextWord = yield 'hello'; console.log('Yield 2...'); yield nextWord; console.log('No more yields...');}

var hw = helloWorld();

console.log( hw.next() );console.log( hw.next('world') );console.log( hw.next() );

Yield 1...

Page 73: ES6 - Next Generation Javascript

var helloWorld = function*() { console.log('Yield 1...'); var nextWord = yield 'hello'; console.log('Yield 2...'); yield nextWord; console.log('No more yields...');}

var hw = helloWorld();

console.log( hw.next() );console.log( hw.next('world') );console.log( hw.next() );

Yield 1...

Page 74: ES6 - Next Generation Javascript

var helloWorld = function*() { console.log('Yield 1...'); var nextWord = yield 'hello'; console.log('Yield 2...'); yield nextWord; console.log('No more yields...');}

var hw = helloWorld();

console.log( hw.next() );console.log( hw.next('world') );console.log( hw.next() );

Yield 1...

{ done: false, value: 'hello'

}

Page 75: ES6 - Next Generation Javascript

var helloWorld = function*() { console.log('Yield 1...'); var nextWord = yield 'hello'; console.log('Yield 2...'); yield nextWord; console.log('No more yields...');}

var hw = helloWorld();

console.log( hw.next() );console.log( hw.next('world') );console.log( hw.next() );

Yield 1...

{ done: false, value: 'hello'

}

Page 76: ES6 - Next Generation Javascript

var helloWorld = function*() { console.log('Yield 1...'); var nextWord = yield 'hello'; console.log('Yield 2...'); yield nextWord; console.log('No more yields...');}

var hw = helloWorld();

console.log( hw.next() );console.log( hw.next('world') );console.log( hw.next() );

Yield 1...

{ done: false, value: 'hello'

}

Page 77: ES6 - Next Generation Javascript

var helloWorld = function*() { console.log('Yield 1...'); var nextWord = yield 'hello'; console.log('Yield 2...'); yield nextWord; console.log('No more yields...');}

var hw = helloWorld();

console.log( hw.next() );console.log( hw.next('world') );console.log( hw.next() );

Yield 1...

{ done: false, value: 'hello'

}

Yield 2...

Page 78: ES6 - Next Generation Javascript

var helloWorld = function*() { console.log('Yield 1...'); var nextWord = yield 'hello'; console.log('Yield 2...'); yield nextWord; console.log('No more yields...');}

var hw = helloWorld();

console.log( hw.next() );console.log( hw.next('world') );console.log( hw.next() );

Yield 1...

{ done: false, value: 'hello'

}

Yield 2...

Page 79: ES6 - Next Generation Javascript

var helloWorld = function*() { console.log('Yield 1...'); var nextWord = yield 'hello'; console.log('Yield 2...'); yield nextWord; console.log('No more yields...');}

var hw = helloWorld();

console.log( hw.next() );console.log( hw.next('world') );console.log( hw.next() );

Yield 1...

{ done: false, value: 'hello'

}

Yield 2...

{ done: false, value: 'world'

}

Page 80: ES6 - Next Generation Javascript

var helloWorld = function*() { console.log('Yield 1...'); var nextWord = yield 'hello'; console.log('Yield 2...'); yield nextWord; console.log('No more yields...');}

var hw = helloWorld();

console.log( hw.next() );console.log( hw.next('world') );console.log( hw.next() );

Yield 1...

{ done: false, value: 'hello'

}

Yield 2...

{ done: false, value: 'world'

}

Page 81: ES6 - Next Generation Javascript

var helloWorld = function*() { console.log('Yield 1...'); var nextWord = yield 'hello'; console.log('Yield 2...'); yield nextWord; console.log('No more yields...');}

var hw = helloWorld();

console.log( hw.next() );console.log( hw.next('world') );console.log( hw.next() );

Yield 1...

{ done: false, value: 'hello'

}

Yield 2...

{ done: false, value: 'world'

}

No more yields...

Page 82: ES6 - Next Generation Javascript

var helloWorld = function*() { console.log('Yield 1...'); var nextWord = yield 'hello'; console.log('Yield 2...'); yield nextWord; console.log('No more yields...');}

var hw = helloWorld();

console.log( hw.next() );console.log( hw.next('world') );console.log( hw.next() );

Yield 1...

{ done: false, value: 'hello'

}

Yield 2...

{ done: false, value: 'world'

}

No more yields...

{ done: true, value: undefined

}

Page 83: ES6 - Next Generation Javascript

The code in the generator doesn't start executing until you sayso.

When the yield statement is encountered it suspendsexecution until you tell it to resume.

What about throw() -ing errors?

Page 84: ES6 - Next Generation Javascript

var helloWorld = function*() { console.log('Yield 1...'); var nextWord = yield 'hello'; console.log('Yield 2...'); yield nextWord; console.log('No more yields...');}

var hw = helloWorld();

console.log( hw.next() );console.log( hw.throw('Voldemort') );console.log( hw.next() );

Page 85: ES6 - Next Generation Javascript

var helloWorld = function*() { console.log('Yield 1...'); var nextWord = yield 'hello'; console.log('Yield 2...'); yield nextWord; console.log('No more yields...');}

var hw = helloWorld();

console.log( hw.next() );console.log( hw.throw('Voldemort') );console.log( hw.next() );

Page 86: ES6 - Next Generation Javascript

var helloWorld = function*() { console.log('Yield 1...'); var nextWord = yield 'hello'; console.log('Yield 2...'); yield nextWord; console.log('No more yields...');}

var hw = helloWorld();

console.log( hw.next() );console.log( hw.throw('Voldemort') );console.log( hw.next() );

Page 87: ES6 - Next Generation Javascript

var helloWorld = function*() { console.log('Yield 1...'); var nextWord = yield 'hello'; console.log('Yield 2...'); yield nextWord; console.log('No more yields...');}

var hw = helloWorld();

console.log( hw.next() );console.log( hw.throw('Voldemort') );console.log( hw.next() );

Page 88: ES6 - Next Generation Javascript

var helloWorld = function*() { console.log('Yield 1...'); var nextWord = yield 'hello'; console.log('Yield 2...'); yield nextWord; console.log('No more yields...');}

var hw = helloWorld();

console.log( hw.next() );console.log( hw.throw('Voldemort') );console.log( hw.next() );

Yield 1...

Page 89: ES6 - Next Generation Javascript

var helloWorld = function*() { console.log('Yield 1...'); var nextWord = yield 'hello'; console.log('Yield 2...'); yield nextWord; console.log('No more yields...');}

var hw = helloWorld();

console.log( hw.next() );console.log( hw.throw('Voldemort') );console.log( hw.next() );

Yield 1...

Page 90: ES6 - Next Generation Javascript

var helloWorld = function*() { console.log('Yield 1...'); var nextWord = yield 'hello'; console.log('Yield 2...'); yield nextWord; console.log('No more yields...');}

var hw = helloWorld();

console.log( hw.next() );console.log( hw.throw('Voldemort') );console.log( hw.next() );

Yield 1...

{ done: false, value: 'hello'

}

Page 91: ES6 - Next Generation Javascript

var helloWorld = function*() { console.log('Yield 1...'); var nextWord = yield 'hello'; console.log('Yield 2...'); yield nextWord; console.log('No more yields...');}

var hw = helloWorld();

console.log( hw.next() );console.log( hw.throw('Voldemort') );console.log( hw.next() );

Yield 1...

{ done: false, value: 'hello'

}

Error: Voldemort

Page 92: ES6 - Next Generation Javascript

How do Generators make asynchronous programming easier?

Page 93: ES6 - Next Generation Javascript

The old-school way:

var readFile = function(fileName, cb) { ... };

var main = function(cb) { readFile('file1', function(err, contents1) { if (err) return cb(err); console.log(contents1);

readFile('file2', function(err, contents2) { if (err) return cb(err); console.log(contents2); cb(); }); }); }

main(console.error);

Page 94: ES6 - Next Generation Javascript

Improved using Promises:

var readFile = Promise.promisify(function(fileName, cb) { ... });

var main = function() { return readFile('file1') .then(function(contents) { console.log(contents); return readFile('file2'); }) .then(function(contents) { console.log(contents); }) .catch(console.error);}

main();

Page 95: ES6 - Next Generation Javascript

We can do better with generators.

But first we need a function which will automatically handlethe yield -ed values and call next() on the generator...

Page 96: ES6 - Next Generation Javascript

Automatically resolve Promises and call next() :

var runGenerator = function(generatorFunction) { var gen = generatorFunction();

var gNext = function(err, answer) { if (err) return gen.throw(err);

var res = gen.next(answer);

if (!res.done) { Promise.resolve(res.value) .then(function(newAnswer) { gNext(null, newAnswer); }) .catch(gNext); } }; gNext();}

Page 97: ES6 - Next Generation Javascript

Now we can rewrite main() as a generator function:

var readFile = Promise.promisify(function(fileName, cb) { ... });var main = function*() { try { console.log( yield readFile('file1') ); console.log( yield readFile('file2') ); } catch (err) { console.error(err); }}

runGenerator(main);

Page 98: ES6 - Next Generation Javascript

You don't need to write runGenerator() yourself.

- similar torunGenerator but more powerful.

- kick-assPromise library and provides runGenerator-like methods.

https://github.com/visionmedia/co

https://github.com/petkaantonov/bluebird

Page 99: ES6 - Next Generation Javascript

Generator delegation:

var inner = function*() { try { yield callServer1(); yield callServer2(); } catch (e) { console.error(e); }};

var outer = function*() { yield* inner();};

runGenerator(outer);

var outer = function*() { try { yield callServer1(); yield callServer2(); } catch (e) { console.error(e); }};

runGenerator(outer);

Page 100: ES6 - Next Generation Javascript

Generator comprehension:

(for (x of a) for (y of b) x * y)

(function* () { for (x of a) { for (y of b) { yield x * y; } }}())

Page 101: ES6 - Next Generation Javascript

Generators = future of JS asynchronous programming.

...and ES6 also has Promises!

http://spion.github.io/posts/why-i-am-switching-to-promises.html

Page 102: ES6 - Next Generation Javascript

Next...Next...

CollectionsCollections

24 30 11

Page 103: ES6 - Next Generation Javascript

SetSet - no duplicates allowed - no duplicates allowedvar items = new Set();items.add(5);items.add("5");items.add(5);console.log(items.size); // 2

var items = new Set([1,2,3,4,5,5,5]);console.log(items.size); // 5

Page 104: ES6 - Next Generation Javascript

Modifying a Set

var items = new Set([1,2,3,4,4]);console.log( items.has(4) ); // trueconsole.log( items.has(5) ); // false

items.delete(4);console.log( items.has(4) ); // falseconsole.log( items.size ); // 3

items.clear();console.log( items.size ); // 0

Page 105: ES6 - Next Generation Javascript

Iterate over a Set using for-of

var items = new Set([1,2,3,4,4]);

for (let num of items) { console.log( num ); // 1, 2, 3, 4}

Page 106: ES6 - Next Generation Javascript

Map - map from key to value

var map = new Map();map.set("name", "John");map.set(23, "age");

console.log( map.has(23); ) // trueconsole.log( map.get(23) ); // "age"console.log( map.size ); // 2

Page 107: ES6 - Next Generation Javascript

Map vs Object

Maps can have non-string keysMaps don't have prototype leakage issues, i.e. no need touse hasOwnProperty()But

Page 108: ES6 - Next Generation Javascript

Modifying a Map

var map = new Map([ ['name', 'John'], [23, 'age'] ]);console.log( map.size ); // 2

map.delete(23);console.log( map.get(23) ); // undefined

map.clear();console.log( map.size ); // 0

Page 109: ES6 - Next Generation Javascript

Iterating over a Mapvar map = new Map([ ['name', 'John'], [23, 'age'] ]);

for (var value of map.values()) { ... }

for (var key of map.keys()) { ... }

for (var item of map.items()) { console.log('key: ' + item[0] + ', value: ' + item[1]);}

for (var item of map) { // same as iterating map.items() }

map.forEach(function(value, key, map) { ... });

Page 110: ES6 - Next Generation Javascript

WeakMap - similar to Map , but...

Only allows Object keys

Only holds a reference to the Object used as a key, so it

doesn't prevent garbage collection

Do no provide a size

Cannot be iterated over

Page 111: ES6 - Next Generation Javascript

var weakMap = new WeakMap();var key = { stuff: true};

weakMap.set(key, 123); // weakMap contains 1 item

delete key; // weakMap is now empty

Page 112: ES6 - Next Generation Javascript

Next...Next...

Typed objectsTyped objects

✗ ✗ ✗

Page 113: ES6 - Next Generation Javascript

Typed objects are similar to struct objects in C

They provide memory-safe, structured access to contiguousdata

They can expose a binary representation, makingserialization/de-serialization easy

Page 114: ES6 - Next Generation Javascript

Example: represent a 2D point

const Point2D = new StructType({ x: uint32, y: uint32 });

Page 115: ES6 - Next Generation Javascript

Can access the underlying storage of the struct

let p = Point2D({x : 0, y : 0});

p.x = 5;

let arrayBuffer = Point.storage(p).buffer;

typeof arrayBuffer // ArrayBuffer

arrayBuffer.byteLength // 8

Page 116: ES6 - Next Generation Javascript

Hierarchy of typed objects

const Corner = new StructType({ point: Point2D });

const Triangle = Corner.dim(3);

let t = Triangle([{ point: { x: 0, y: 0 } }, { point: { x: 5, y: 5 } }, { point: { x: 10, y: 0 } }]);

t[0].point.x = 5;

Page 117: ES6 - Next Generation Javascript

A type object and all of its sub objects share the same memory

let t = Triangle([{ point: { x: 0, y: 0 } }, { point: { x: 5, y: 5 } }, { point: { x: 10, y: 0 } }]);

Triangle.storage(t).buffer.byteLength; // 24

Page 118: ES6 - Next Generation Javascript

Typed objects use copy-on-write

const Corner = new StructType({ point: Point2D });

let p = Point2D({ x: 1, y: 1 });let c = Corner({ point: {x: 2, y: 2} });

c.point = p; // p gets copiedc.point.x = 5;p.x; // 1

Page 119: ES6 - Next Generation Javascript

Built-in value types

uint8, uint8Clampeduint16uint32int8int16int32float32float64booleanstring

Page 120: ES6 - Next Generation Javascript

Next...Next...

Direct proxiesDirect proxies

24 ✗ ✗

Page 121: ES6 - Next Generation Javascript

Direct proxies allows you to intercept calls made to a regularobject

They can wrap any Object , including Date ,Function , etc.

Page 122: ES6 - Next Generation Javascript

Proxies are meant to work 'transparently'

var target = [];

var handler = { get: function() {...} };

var p = Proxy(target, handler);

Object.prototype.toString.call(p) // "[object Array]"

Object.getPrototypeOf(p) // Array.prototype

typeof p // "object"

Array.isArray(p) // true

p[0] // triggers handler.get(target, "0", p)

p === target // false

Page 123: ES6 - Next Generation Javascript

Proxy handler can choose what to intercept

getOwnPropertyDescriptor

getOwnPropertyNames

getPrototypeOf

defineProperty

deleteProperty

freeze

seal

preventExtensions

isFrozen

isExtensible

isSealed

has

hasOwn

get

set

enumerate

keys

apply

construct

Page 124: ES6 - Next Generation Javascript

Next...Next...

Template stringsTemplate strings

✗ ✗ ✗

Page 125: ES6 - Next Generation Javascript

What template strings allow

Multi-line strings

String formatting - think printf from C

HTML escaping

Localization/internationalization

Page 126: ES6 - Next Generation Javascript

Basic substitution

var name = "Tom",

msg = `Hello, ${name}!`;

console.log(msg); // "Hello, Tom!"

Page 127: ES6 - Next Generation Javascript

Expressions

var total = 30, msg = `The total is ${total * 2} units`;

console.log(msg); // "The total is 60 units"

Page 128: ES6 - Next Generation Javascript

Multi-line

var total = 30,

var msg = `The total is ${total * 2} units`;

Page 129: ES6 - Next Generation Javascript

Functions

// Gets called with: // ['Total is ', ' units']// 60var myFunc = function(literals) { var str = '', i = 0; while (i < literals.length) { str += literals[i++]; if (i < arguments.length) { str += '[' + arguments[i] + ']'; } } return str;};var total = 30;console.log( myFunc`Total is ${total * 2} units` );

// Total is [60] units

Page 130: ES6 - Next Generation Javascript

Next...Next...

API improvementsAPI improvements

24 ✗ ✗

with a few exceptions ( )full details

Page 131: ES6 - Next Generation Javascript

New Math functions

log10 , log2 , log1p , expm1 , cosh , sinh , tanh ,acosh , asinh , atanh , hypot , trunc , sign

Page 132: ES6 - Next Generation Javascript

Number

.isFinite()

.isNaN() - better than isNaN()

.isInteger()

.EPSILON - smallest values such that 1 +Number.EPSILON > 1

Page 133: ES6 - Next Generation Javascript

String

.repeat(n) - copy current string n times

.startsWith(str)

.endsWith(str)

.contains(str)

.toArray() - same as .split('')

Page 134: ES6 - Next Generation Javascript

Next...Next...

ModularityModularity

✗ ✗ ✗

Page 135: ES6 - Next Generation Javascript

ClassesClasses

Page 136: ES6 - Next Generation Javascript

In es5In es5Classes doesn't exist nativelyPrototype based inheritanceFramework and libraries implement their own class system

Page 137: ES6 - Next Generation Javascript

New keywordNew keywordclass Laptop { constructor() { this.brand = 'asus'; }

on() { ... } off() { ... }}

Page 138: ES6 - Next Generation Javascript

Call the parentCall the parentclass SmashedLaptop extend Laptop { constructor() { super(); this.pieces = []; }}

Page 139: ES6 - Next Generation Javascript

Key pointsKey pointsconstructor replace the function definition in es5No access to the prototype of the classMethods are defined the same way as objectsCan call the parent with super (and perform initializationwithin the constructor)

Page 140: ES6 - Next Generation Javascript

ModulesModules• import the default export of a moduleimport $ from "jquery";

• binding an external module to a variablemodule crypto from "crypto";

• binding a module's exports to variablesimport { encrypt, decrypt } from "crypto";

Page 141: ES6 - Next Generation Javascript

ModulesModules• binding & renaming one of a module's exportsimport { encrypt as enc } from "crypto";

• re-exporting another module's exportsexport * from "crypto";

• re-exporting specified exportsfrom another moduleexport { foo, bar } from "crypto";

Page 142: ES6 - Next Generation Javascript

Why ?Why ?No need for the global object anymoreWorks well with existing modules system (AMD,CommonJS and node)Simplicity and usabilityCompatibility with browser and non-browser environmentsEasy asynchronous external loading

Page 143: ES6 - Next Generation Javascript

Exporting and importingExporting and importingmodule "chocolate" { export let cocoa = 75;}

In another file:

import { cocoa } from "chocolate";// orimport { cocoa as c} from "chocolate";

Page 144: ES6 - Next Generation Javascript

Default exportDefault exportmodule "foo" {

export default function() {console.log("Hi")}

}

import foo from "foo"; // no brackets

foo(); // Hi

Page 145: ES6 - Next Generation Javascript

InternalsInternalsTop-level variables stay inside the moduleexport make variables visible to the other modules

Can be read (get)Cannot be changed (set)Cannot be dynamically changed at runtime

Modules are recursively instantiated before evaluationModules' body is run after all dependencies are instantiated

Page 146: ES6 - Next Generation Javascript

That's all forThat's all fortoday!today!

See for morehttp://kangax.github.io/es5-compat-table/es6/

Page 147: ES6 - Next Generation Javascript

Useful linksUseful linkshttp://www.ecmascript.org/http://www.esdiscuss.org/https://developer.mozilla.org/en/docs/Web/JavaScript/ECMAScript_6_support_in_Mozilla