codefest 2013. rauschmayer a. — an overview of ecmascript 6, the next version of javascript

70
ECMAScript 6 Dr. Axel Rauschmayer rauschma.de 2013-03-30 CodeFest 2013, Novosibirsk

Upload: codefest

Post on 08-May-2015

1.274 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: CodeFest 2013. Rauschmayer A. — An overview of ECMAScript 6, the next version of JavaScript

ECMAScript 6

Dr. Axel Rauschmayer

rauschma.de

2013-03-30

CodeFest 2013, Novosibirsk

Page 2: CodeFest 2013. Rauschmayer A. — An overview of ECMAScript 6, the next version of JavaScript

About me

Axel Rauschmayer:Editor of JavaScript WeeklyAuthor of 2ality.comCo-organizer of MunichJS

I have written about ECMAScript.next/ECMAScript 6 since early 2011.

Dr. Axel Rauschmayer (rauschma.de) ECMAScript 6 2013-03-30 2 / 70

Page 3: CodeFest 2013. Rauschmayer A. — An overview of ECMAScript 6, the next version of JavaScript

JavaScript: it has become big

Photographer: Via Tsuji

Used for much more than it wasoriginally created for.

Dr. Axel Rauschmayer (rauschma.de) ECMAScript 6 2013-03-30 3 / 70

Page 4: CodeFest 2013. Rauschmayer A. — An overview of ECMAScript 6, the next version of JavaScript

ECMAScript 6

ECMAScript 6:Next version of JavaScript(current engines: ECMAScript 5).Be better at what JavaScript is used for now.

This talk:Specific goals for ECMAScript 6?How is ECMAScript 6 created?Features?

WarningAll information in this talk is preliminary, features can and will changebefore ECMAScript 6 is final.

Dr. Axel Rauschmayer (rauschma.de) ECMAScript 6 2013-03-30 4 / 70

Page 5: CodeFest 2013. Rauschmayer A. — An overview of ECMAScript 6, the next version of JavaScript

Background

Page 6: CodeFest 2013. Rauschmayer A. — An overview of ECMAScript 6, the next version of JavaScript

Glossary

TC39 (Ecma Technical Committee 39): the committee evolvingJavaScript.

Members: companies (all major browser vendors etc.).Meetings attended by employees and invited experts.

JavaScript: colloquially: the language; formally: one implementationECMAScript: the language standard

ECMAScript Harmony: improvements after ECMAScript 5 (severalversions)

ECMAScript.next: code name for upcoming version, subset ofHarmonyECMAScript 6: the final name of ECMAScript.next (probably)

Dr. Axel Rauschmayer (rauschma.de) ECMAScript 6 2013-03-30 6 / 70

Page 7: CodeFest 2013. Rauschmayer A. — An overview of ECMAScript 6, the next version of JavaScript

Goals for ECMAScript 6

One of several goals [1]: make JavaScript better

for complex applications

for libraries (possibly including the DOM)

as a target of code generators

Dr. Axel Rauschmayer (rauschma.de) ECMAScript 6 2013-03-30 7 / 70

Page 8: CodeFest 2013. Rauschmayer A. — An overview of ECMAScript 6, the next version of JavaScript

Challenges of evolving a web language

Little control over ECMAScript versions:1 Browsers: encounter code in many different versions.

Even within a single app (third party code!).2 Apps: encounter engines supporting a variety of versions.

Constraints for ECMAScript 6:#1 ⇒ Must not break existing code.#2 ⇒ Can’t be used right away (not everywhere).

Dr. Axel Rauschmayer (rauschma.de) ECMAScript 6 2013-03-30 8 / 70

Page 9: CodeFest 2013. Rauschmayer A. — An overview of ECMAScript 6, the next version of JavaScript

Challenges of evolving a web language

don’t break existing

code

add new features

fix pitfalls

preserve nature of

JavaScript

Goals Requirements

Dr. Axel Rauschmayer (rauschma.de) ECMAScript 6 2013-03-30 9 / 70

Page 10: CodeFest 2013. Rauschmayer A. — An overview of ECMAScript 6, the next version of JavaScript

How features are designed

Avoid “design by committee”:Design by “champions” (groups of 1–2 experts)Feedback from TC39 and the web communityTC39 has final word on whether to include

Stages [2]:Strawman proposalTC39 is interested ⇒ proposalField-testing via one or more implementations, refinementsTC39 accepts feature ⇒ included in ECMAScript draftIncluded in final spec ⇒ Standard

Dr. Axel Rauschmayer (rauschma.de) ECMAScript 6 2013-03-30 10 / 70

Page 11: CodeFest 2013. Rauschmayer A. — An overview of ECMAScript 6, the next version of JavaScript

Variables and scoping

Page 12: CodeFest 2013. Rauschmayer A. — An overview of ECMAScript 6, the next version of JavaScript

Block-scoped variables

Function scope (var)

function order(x, y) {console.log(tmp);

// undefined

if (x > y) {var tmp = x;x = y;y = tmp;

}return [x, y];

}

Block scope (let, const)

function order(x, y) {console.log(tmp);

// ReferenceError:// tmp is not defined

if (x > y) {let tmp = x;x = y;y = tmp;

}return [x, y];

}

Dr. Axel Rauschmayer (rauschma.de) ECMAScript 6 2013-03-30 12 / 70

Page 13: CodeFest 2013. Rauschmayer A. — An overview of ECMAScript 6, the next version of JavaScript

Destructuring: objects

Extract data (more than one value!) via patterns:

let obj = { first: 'Jane', last: 'Doe' };

let { first: f , last: l } = obj;console.log(f + ' ' + l); // Jane Doe

let { first, last } = obj;// same as { first: first , last: last }

console.log(first + ' ' + last); // Jane Doe

Usage: variable declarations, assignments, parameter definitions.

Dr. Axel Rauschmayer (rauschma.de) ECMAScript 6 2013-03-30 13 / 70

Page 14: CodeFest 2013. Rauschmayer A. — An overview of ECMAScript 6, the next version of JavaScript

Destructuring: arrays

let [x, y] = [ 'a', 'b' ]; // x='a', y='b'let [x, y] = [ 'a', 'b', 'c', 'd' ]; // x='a', y='b'let [x, y, ...rest] = [ 'a', 'b', 'c', 'd' ];

// x='a', y='b', rest = [ 'c', 'd' ]

[x,y] = [y,x];

Dr. Axel Rauschmayer (rauschma.de) ECMAScript 6 2013-03-30 14 / 70

Page 15: CodeFest 2013. Rauschmayer A. — An overview of ECMAScript 6, the next version of JavaScript

Destructuring: refutable by default

Refutable (default): exception if match isn’t exact.{ a: x, b: y } = { a: 3 }; // fails

Irrefutable: always match.{ a: x, ?b: y } = { a: 3 }; // x=3, y=undefined

Default value: use if no match or value is undefined{ a: x, b: y=5 } = { a: 3 }; // x=3, y=5

Dr. Axel Rauschmayer (rauschma.de) ECMAScript 6 2013-03-30 15 / 70

Page 16: CodeFest 2013. Rauschmayer A. — An overview of ECMAScript 6, the next version of JavaScript

Functions and parameters

Page 17: CodeFest 2013. Rauschmayer A. — An overview of ECMAScript 6, the next version of JavaScript

Arrow functions: less to type

Compare:

let squares = [1, 2, 3].map(x => x * x);

let squares = [1, 2, 3].map(function (x) {return x * x});

Dr. Axel Rauschmayer (rauschma.de) ECMAScript 6 2013-03-30 17 / 70

Page 18: CodeFest 2013. Rauschmayer A. — An overview of ECMAScript 6, the next version of JavaScript

Arrow functions: lexical this, no more that=this

function UiComponent {var that = this;var button = document.getElementById('#myButton');button.addEventListener('click', function () {

console.log('CLICK');that.handleClick();

});}UiComponent.prototype.handleClick = function () { ... };

function UiComponent {let button = document.getElementById('#myButton');button.addEventListener('click', () => {

console.log('CLICK');this.handleClick();

});}

Dr. Axel Rauschmayer (rauschma.de) ECMAScript 6 2013-03-30 18 / 70

Page 19: CodeFest 2013. Rauschmayer A. — An overview of ECMAScript 6, the next version of JavaScript

Arrow functions: variants

Zero parameters:

() => expr() => { stmt0; stmt1; ... }

One parameter:

arg => exprarg => { stmt0; stmt1; ... }

More than one parameter:

(arg0, arg1, ...) => expr(arg0, arg1, ...) => { stmt0; stmt1; ... }

Dr. Axel Rauschmayer (rauschma.de) ECMAScript 6 2013-03-30 19 / 70

Page 20: CodeFest 2013. Rauschmayer A. — An overview of ECMAScript 6, the next version of JavaScript

Parameter handling 1: parameter default values

Give missing parameters a default value.

function func1(x, y=3) {return [x,y];

}

Interaction:

> func1(1, 2)[1, 2]> func1(1)[1, 3]> func1()[undefined, 3]

Dr. Axel Rauschmayer (rauschma.de) ECMAScript 6 2013-03-30 20 / 70

Page 21: CodeFest 2013. Rauschmayer A. — An overview of ECMAScript 6, the next version of JavaScript

Parameter handling 2: rest parameters

Put trailing parameters in an array.

function func2(arg0, ...others) {return others;

}

Interaction:

> func2(0, 1, 2, 3)[1, 2, 3]> func2(0)[]> func2()[]

Eliminate the need for the special variable arguments.

Dr. Axel Rauschmayer (rauschma.de) ECMAScript 6 2013-03-30 21 / 70

Page 22: CodeFest 2013. Rauschmayer A. — An overview of ECMAScript 6, the next version of JavaScript

Parameter handling 3: named parameters

Idea: name parameters via an object literal.More descriptive:

moveTo({ x: 50, y: 50, speed: 0.5 })

Easy to omit:foo({ opt1: 'a', opt2: 'b', opt3: 'c' })foo({ opt3: 'c' })foo({ opt1: 'a', opt3: 'c' })

Dr. Axel Rauschmayer (rauschma.de) ECMAScript 6 2013-03-30 22 / 70

Page 23: CodeFest 2013. Rauschmayer A. — An overview of ECMAScript 6, the next version of JavaScript

Parameter handling 3: named parameters

Use destructuring for named parameters opt1 and opt2:

function func3(arg0, { opt1, opt2 }) {return return [opt1, opt2];

}

Interaction:

> func3(0, { opt1: 'a', opt2: 'b' })['a', 'b']

Dr. Axel Rauschmayer (rauschma.de) ECMAScript 6 2013-03-30 23 / 70

Page 24: CodeFest 2013. Rauschmayer A. — An overview of ECMAScript 6, the next version of JavaScript

Spread operator (...)

Turn an array into function/method arguments:

> Math.max(7, 4, 11)11> Math.max(...[7, 4, 11])11

The inverse of a rest parameterMostly replaces Function.prototype.apply()Also works in constructors

Dr. Axel Rauschmayer (rauschma.de) ECMAScript 6 2013-03-30 24 / 70

Page 25: CodeFest 2013. Rauschmayer A. — An overview of ECMAScript 6, the next version of JavaScript

Modularity

Page 26: CodeFest 2013. Rauschmayer A. — An overview of ECMAScript 6, the next version of JavaScript

Object literals

// ECMAScript 6let obj = {

__proto__: someObject, // special propertymyMethod(arg1, arg2) { // method definition

...}

};

// ECMAScript 5var obj = Object.create(someObject);obj.myMethod = function (arg1, arg2) {

...};

Dr. Axel Rauschmayer (rauschma.de) ECMAScript 6 2013-03-30 26 / 70

Page 27: CodeFest 2013. Rauschmayer A. — An overview of ECMAScript 6, the next version of JavaScript

Object literals: property value shorthand

Shorthand: {x,y} is the same as { x: x, y: y }.

function computePoint() {let x = computeX();let y = computeY();return { x, y }; // shorthand

}

let {x,y} = computePoint(); // shorthand

Dr. Axel Rauschmayer (rauschma.de) ECMAScript 6 2013-03-30 27 / 70

Page 28: CodeFest 2013. Rauschmayer A. — An overview of ECMAScript 6, the next version of JavaScript

Symbols

Each symbol is a unique value. Use asenum-style valuesproperty keys:let sym = Symbol();console.log(typeof sym); // symbollet obj = {

[sym](arg) { // computed property key...

}};obj[sym](123);

Advantages of symbols as property keys:No name clashes!Configure various aspects of the language⇒ import public symbols from a system module

Dr. Axel Rauschmayer (rauschma.de) ECMAScript 6 2013-03-30 28 / 70

Page 29: CodeFest 2013. Rauschmayer A. — An overview of ECMAScript 6, the next version of JavaScript

Classes

class Point {constructor(x, y) {

this.x = x;this.y = y;

}toString() {

return '('+this.x+', '+this.y+')';}

}

function Point(x, y) {this.x = x;this.y = y;

}Point.prototype.toString = function () {

return '('+this.x+', '+this.y+')';};

Dr. Axel Rauschmayer (rauschma.de) ECMAScript 6 2013-03-30 29 / 70

Page 30: CodeFest 2013. Rauschmayer A. — An overview of ECMAScript 6, the next version of JavaScript

Classes: sub-type

class ColorPoint extends Point {constructor(x, y, color) {

super(x, y); // same as super.constructor(x, y)this.color = color;

}toString() {

return this.color+' '+super();}

}

function ColorPoint(x, y, color) {Point.call(this, x, y);this.color = color;

}ColorPoint.prototype = Object.create(Point.prototype);ColorPoint.prototype.constructor = ColorPoint;ColorPoint.prototype.toString = function () {

return this.color+' '+Point.prototype.toString.call(this);};

Dr. Axel Rauschmayer (rauschma.de) ECMAScript 6 2013-03-30 30 / 70

Page 31: CodeFest 2013. Rauschmayer A. — An overview of ECMAScript 6, the next version of JavaScript

Static methods

class Point {static zero() {

return new Point(0, 0);}constructor(x, y) {

this.x = x;this.y = y;

}}let p = Point.zero();

Dr. Axel Rauschmayer (rauschma.de) ECMAScript 6 2013-03-30 31 / 70

Page 32: CodeFest 2013. Rauschmayer A. — An overview of ECMAScript 6, the next version of JavaScript

Private properties

Hiding some properties from external access (object literals, classes):Still under discussion.But there will be some way of doing it.Possibly: a special kind of symbol.

Dr. Axel Rauschmayer (rauschma.de) ECMAScript 6 2013-03-30 32 / 70

Page 33: CodeFest 2013. Rauschmayer A. — An overview of ECMAScript 6, the next version of JavaScript

Modules: overview

// lib/math.jslet notExported = 'abc';export function square(x) {

return x * x;}export const MY_CONSTANT = 123;

// main.jsimport {square} from 'lib/math';console.log(square(3));

Alternatively:

import 'lib/math' as math;console.log(math.square(3));

Dr. Axel Rauschmayer (rauschma.de) ECMAScript 6 2013-03-30 33 / 70

Page 34: CodeFest 2013. Rauschmayer A. — An overview of ECMAScript 6, the next version of JavaScript

Modules: features

More features [3]:Rename importsConcatenation: put several modules in the same fileModule IDs are configurable (default: paths relative to importing file)Programmatic (e.g. conditional) loading of modules via an APIModule loading is customizable:

Automatic lintingAutomatically translate files (CoffeeScript, TypeScript)Use legacy modules (AMD, Node.js)

Dr. Axel Rauschmayer (rauschma.de) ECMAScript 6 2013-03-30 34 / 70

Page 35: CodeFest 2013. Rauschmayer A. — An overview of ECMAScript 6, the next version of JavaScript

Loops and iteration

Page 36: CodeFest 2013. Rauschmayer A. — An overview of ECMAScript 6, the next version of JavaScript

Iterables and iterators

[iterate]()...

Iterable:traversable data structure

next()

Iterator:pointer for traversing iterable

returns

Examples of iterables:ArraysResults produced by tool functions and methods(keys(), values(), entries()).

Dr. Axel Rauschmayer (rauschma.de) ECMAScript 6 2013-03-30 36 / 70

Page 37: CodeFest 2013. Rauschmayer A. — An overview of ECMAScript 6, the next version of JavaScript

Iterators

import {iterate} from '@iter'; // symbolfunction iterArray(arr) {

let i = 0;return { // both iterable and iterator

[iterate]() { // iterablereturn this; // iterator

},next() { // iterator

if (i < arr.length) {return { value: arr[i++] };

} else {return { done: true };

} } } }for (let elem of iterArray(['a', 'b'])) {

console.log(elem);}

Dr. Axel Rauschmayer (rauschma.de) ECMAScript 6 2013-03-30 37 / 70

Page 38: CodeFest 2013. Rauschmayer A. — An overview of ECMAScript 6, the next version of JavaScript

for-of: a better loop

for-in loop:Basically useless for arraysQuirky for objects

Array.prototype.forEach(): doesn’t work with iterables.

Dr. Axel Rauschmayer (rauschma.de) ECMAScript 6 2013-03-30 38 / 70

Page 39: CodeFest 2013. Rauschmayer A. — An overview of ECMAScript 6, the next version of JavaScript

for-of loop: arrays

Looping over iterables.

let arr = [ 'hello', 'world' ];for (let elem of arr) {

console.log(elem);}

Output:

helloworld

Dr. Axel Rauschmayer (rauschma.de) ECMAScript 6 2013-03-30 39 / 70

Page 40: CodeFest 2013. Rauschmayer A. — An overview of ECMAScript 6, the next version of JavaScript

for-of loop: objects

let obj = { first: 'Jane', last: 'Doe' };

Iterate over properties:

import {entries} from '@iter'; // returns an iterablefor (let [key, value] of entries(obj)) {

console.log(key + ' = ' + value);}

Iterate over property names:

import {keys} from '@iter'; // returns an iterablefor (let name of keys(obj)) {

console.log(name);}

Dr. Axel Rauschmayer (rauschma.de) ECMAScript 6 2013-03-30 40 / 70

Page 41: CodeFest 2013. Rauschmayer A. — An overview of ECMAScript 6, the next version of JavaScript

Template strings

Page 42: CodeFest 2013. Rauschmayer A. — An overview of ECMAScript 6, the next version of JavaScript

Template strings: string interpolation

Invocation:

templateHandler`Hello ${firstName} ${lastName}!`

Syntactic sugar for:

templateHandler(['Hello ', ' ', '!'], firstName, lastName)

Two kinds of tokens:Literal sections (static): 'Hello'Substitutions (dynamic): firstName

Dr. Axel Rauschmayer (rauschma.de) ECMAScript 6 2013-03-30 42 / 70

Page 43: CodeFest 2013. Rauschmayer A. — An overview of ECMAScript 6, the next version of JavaScript

Template strings: raw strings

No escaping, multiple lines:

var str = raw`This is a textwith multiple lines.

Escapes are not interpreted,\n is not a newline.`;

Dr. Axel Rauschmayer (rauschma.de) ECMAScript 6 2013-03-30 43 / 70

Page 44: CodeFest 2013. Rauschmayer A. — An overview of ECMAScript 6, the next version of JavaScript

Template strings: regular expressions

ECMAScript 5 (XRegExp library):

var str = '/2012/10/Page.html';var parts = str.match(XRegExp(

'^ # match at start of string only \n' +'/ (?<year> [^/]+ ) # capture top dir as year \n' +'/ (?<month> [^/]+ ) # capture subdir as month \n' +'/ (?<title> [^/]+ ) # file name base \n' +'\\.html? # file name extension: .htm or .html \n' +'$ # end of string','x'

));

console.log(parts.year); // 2012

XRegExp features: named groups, ignored whitespace, comments.

Dr. Axel Rauschmayer (rauschma.de) ECMAScript 6 2013-03-30 44 / 70

Page 45: CodeFest 2013. Rauschmayer A. — An overview of ECMAScript 6, the next version of JavaScript

Template strings: regular expressions

ECMAScript 6:

let str = '/2012/10/Page.html';let parts = str.match(XRegExp.rx`

^ # match at start of string only/ (?<year> [^/]+ ) # capture top dir as year/ (?<month> [^/]+ ) # capture subdir as month/ (?<title> [^/]+ ) # file name base\.html? # file name extension: .htm or .html$ # end of string

`);

Advantages:Raw characters: no need to escape backslash and quoteMulti-line: no need to concatenate strings with newlines at the end

Dr. Axel Rauschmayer (rauschma.de) ECMAScript 6 2013-03-30 45 / 70

Page 46: CodeFest 2013. Rauschmayer A. — An overview of ECMAScript 6, the next version of JavaScript

Template strings: other use cases

Query languagesText localizationSecure templatesetc.

Dr. Axel Rauschmayer (rauschma.de) ECMAScript 6 2013-03-30 46 / 70

Page 47: CodeFest 2013. Rauschmayer A. — An overview of ECMAScript 6, the next version of JavaScript

Standard library

Page 48: CodeFest 2013. Rauschmayer A. — An overview of ECMAScript 6, the next version of JavaScript

Maps

Data structure mapping from arbitrary values to arbitrary values(objects: keys must be strings).

let map = new Map();let obj = {};

map.set(obj, 123);console.log(map.get(obj)); // 123console.log(map.has(obj)); // true

map.delete(obj);console.log(map.has(obj)); // false

Also: iteration (over keys, values, entries) and more.

Dr. Axel Rauschmayer (rauschma.de) ECMAScript 6 2013-03-30 48 / 70

Page 49: CodeFest 2013. Rauschmayer A. — An overview of ECMAScript 6, the next version of JavaScript

Weak maps

Idea – a map with weakly held keys:Map objects to data.Don’t prevent objects from being garbage-collected.Use cases: privately associating data with objects, private caches, . . .

Constraints:Can’t enumerate contentsKeys are objects, values are arbitrary values

Dr. Axel Rauschmayer (rauschma.de) ECMAScript 6 2013-03-30 49 / 70

Page 50: CodeFest 2013. Rauschmayer A. — An overview of ECMAScript 6, the next version of JavaScript

Sets

A collection of values without duplicates.

let set1 = new Set();set1.add('hello');console.log(set1.has('hello')); // trueconsole.log(set1.has('world')); // false

let set2 = new Set([3,2,1,3,2,3]);console.log(set2.values()); // 1,2,3

Dr. Axel Rauschmayer (rauschma.de) ECMAScript 6 2013-03-30 50 / 70

Page 51: CodeFest 2013. Rauschmayer A. — An overview of ECMAScript 6, the next version of JavaScript

Object.assign

Merge one object into another one.

class Point {constructor(x, y) {

Object.assign(this, { x, y });}

}

Similar to Underscore.js _.extend().

Dr. Axel Rauschmayer (rauschma.de) ECMAScript 6 2013-03-30 51 / 70

Page 52: CodeFest 2013. Rauschmayer A. — An overview of ECMAScript 6, the next version of JavaScript

Various other additions to the standard library

> 'abc'.repeat(3)'abcabcabc'> 'abc'.startsWith('ab')true> 'abc'.endsWith('bc')true

And more!

Dr. Axel Rauschmayer (rauschma.de) ECMAScript 6 2013-03-30 52 / 70

Page 53: CodeFest 2013. Rauschmayer A. — An overview of ECMAScript 6, the next version of JavaScript

Generators

Page 54: CodeFest 2013. Rauschmayer A. — An overview of ECMAScript 6, the next version of JavaScript

function* generatorFunction() { ... yield x; ...}

generatorObject

returns

next()

yield

next()

yield

Generators: suspend and resumea function

Shallow coroutines [4]: onlyfunction body is suspended.Uses: iterators, simplerasynchronous programming.

Dr. Axel Rauschmayer (rauschma.de) ECMAScript 6 2013-03-30 54 / 70

Page 55: CodeFest 2013. Rauschmayer A. — An overview of ECMAScript 6, the next version of JavaScript

Generators: example

Suspend via yield (“resumable return”):

function* generatorFunction() {yield 0;yield 1;yield 2;

}

Start and resume via next():

let genObj = generatorFunction();console.log(genObj.next()); // 0console.log(genObj.next()); // 1console.log(genObj.next()); // 2

Dr. Axel Rauschmayer (rauschma.de) ECMAScript 6 2013-03-30 55 / 70

Page 56: CodeFest 2013. Rauschmayer A. — An overview of ECMAScript 6, the next version of JavaScript

Generators: implementing an iterator

An iterator for nested arrays:

function* iterTree(tree) {if (Array.isArray(tree)) {

// inner nodefor(let i=0; i < tree.length; i++) {

yield* iterTree(tree[i]); // recursion}

} else {// leafyield tree;

}}

Difficult to write without recursion.

Dr. Axel Rauschmayer (rauschma.de) ECMAScript 6 2013-03-30 56 / 70

Page 57: CodeFest 2013. Rauschmayer A. — An overview of ECMAScript 6, the next version of JavaScript

Generators: asynchronous programming

Using the task.js library:

spawn(function* () {try {

var [foo, bar] = yield join(

read("foo.json") , read("bar.json")).timeout(1000);render(foo);render(bar);

} catch (e) {console.log("read failed: " + e);

}});

Wait for asynchronous calls via yield (internally based on promises).

Dr. Axel Rauschmayer (rauschma.de) ECMAScript 6 2013-03-30 57 / 70

Page 58: CodeFest 2013. Rauschmayer A. — An overview of ECMAScript 6, the next version of JavaScript

Proxies

Page 59: CodeFest 2013. Rauschmayer A. — An overview of ECMAScript 6, the next version of JavaScript

Proxies

Observe operations applied to object proxy, via handler h:

let target = {};let proxy = Proxy(target, h);

Each of the following operations triggers a method invocation on h:

proxy['foo'] → h.get(target, 'foo', p)proxy['foo'] = 123 → h.set(target, 'foo', 123, p)'foo' in proxy → h.has(target, 'foo')for (key in proxy) {...} → h.enumerate(target)

Dr. Axel Rauschmayer (rauschma.de) ECMAScript 6 2013-03-30 59 / 70

Page 60: CodeFest 2013. Rauschmayer A. — An overview of ECMAScript 6, the next version of JavaScript

Proxies in the prototype chain

let child = Object.create(proxy);

Operations on child can still trigger handler invocations(if the search for properties reaches proxy):

child['foo'] → h.get(target, 'foo', ch)child['foo'] = 123 → h.set(target, 'foo', 123, ch)'foo' in child → h.has(target, 'foo')for (key in child) {...} → h.enumerate(target)

Dr. Axel Rauschmayer (rauschma.de) ECMAScript 6 2013-03-30 60 / 70

Page 61: CodeFest 2013. Rauschmayer A. — An overview of ECMAScript 6, the next version of JavaScript

Proxy: example

let handler = {get(target, name, receiver) {

return (...args) => {console.log('Missing method '+name

+ ', arguments: '+args);}

}};let proxy = Proxy({}, handler);

Using the handler:

> let obj = Object.create(proxy);> obj.foo(1, 2)Missing method foo, arguments: 1, 2undefined

Dr. Axel Rauschmayer (rauschma.de) ECMAScript 6 2013-03-30 61 / 70

Page 62: CodeFest 2013. Rauschmayer A. — An overview of ECMAScript 6, the next version of JavaScript

Use cases for proxies

Typical meta-programming tasks:Sending all method invocations to a remote objectImplementing data access objects for a databaseData bindingLogging

Dr. Axel Rauschmayer (rauschma.de) ECMAScript 6 2013-03-30 62 / 70

Page 63: CodeFest 2013. Rauschmayer A. — An overview of ECMAScript 6, the next version of JavaScript

Conclusion

Page 64: CodeFest 2013. Rauschmayer A. — An overview of ECMAScript 6, the next version of JavaScript

Time table

ECMAScript specification:November 2013: final review of draftJuly 2014: editorially completeDecember 2014: Ecma approval

Features are already appearing in JavaScript engines [5]!

Dr. Axel Rauschmayer (rauschma.de) ECMAScript 6 2013-03-30 64 / 70

Page 65: CodeFest 2013. Rauschmayer A. — An overview of ECMAScript 6, the next version of JavaScript

Thank you!

Page 66: CodeFest 2013. Rauschmayer A. — An overview of ECMAScript 6, the next version of JavaScript

References

1 ECMAScript Harmony wiki2 “The Harmony Process” by David Herman3 “ES6 Modules” by Yehuda Katz4 “Why coroutines won’t work on the web” by David Herman5 “ECMAScript 6 compatibility table” by kangax [features already in

JavaScript engines]

Dr. Axel Rauschmayer (rauschma.de) ECMAScript 6 2013-03-30 66 / 70

Page 67: CodeFest 2013. Rauschmayer A. — An overview of ECMAScript 6, the next version of JavaScript

Resources

ECMAScript 6 specification drafts by Allen Wirfs-BrockECMAScript mailing list: es-discussTC39 meeting notes by Rick Waldron“A guide to 2ality’s posts on ECMAScript 6” by Axel RauschmayerContinuum, an ECMAScript 6 virtual machine written inECMAScript 3.

(Links are embedded in this slide.)

Dr. Axel Rauschmayer (rauschma.de) ECMAScript 6 2013-03-30 67 / 70

Page 68: CodeFest 2013. Rauschmayer A. — An overview of ECMAScript 6, the next version of JavaScript

Bonus slides

Page 69: CodeFest 2013. Rauschmayer A. — An overview of ECMAScript 6, the next version of JavaScript

Function-scoping: pitfall

function foo() {var x = 3;if (x >= 0) {

var tmp;console.log(tmp); // undefinedtmp = 'abc';

}if (x > 0) {

var tmp;console.log(tmp); // abc

}}

Dr. Axel Rauschmayer (rauschma.de) ECMAScript 6 2013-03-30 69 / 70

Page 70: CodeFest 2013. Rauschmayer A. — An overview of ECMAScript 6, the next version of JavaScript

Comprehensions

Array comprehensions produce an array:

let numbers = [1,2,3];let squares = [for (x of numbers) x*x];

Generator comprehensions produce a generator object (an iterator):

let squares = (for (x of numbers) x*x);

Dr. Axel Rauschmayer (rauschma.de) ECMAScript 6 2013-03-30 70 / 70