An Overview of ECMAScript 6 Presentation
An Overview of ECMAScript 6 Presentation
2ality.com
2013-05-30
Axel Rauschmayer:
Blogger at 2ality.com
JavaScript: Used for much more than it was originally created for.
Let’s make it better for those tasks. . .
Dr. Axel Rauschmayer (2ality.com) ECMAScript 6 2013-05-30 3 / 71
ECMAScript 6
This talk:
Why (goals)?
How (design process)?
What (features)?
Warning
All information is preliminary, features can and will change.
Important terms:
TC39 (Ecma Technical Committee 39): the committee evolving
JavaScript.
Members: companies (all major browser vendors etc.).
Meetings attended by employees and invited experts.
JavaScript: colloquially: the language; formally: one implementation
ECMAScript: the language standard
ECMAScript Harmony: improvements after ECMAScript 5
(ECMAScript 6 and 7)
ECMAScript.next: code name for upcoming version, subset of
Harmony
ECMAScript 6: the final name of ECMAScript.next (probably)
Stages [2]:
Strawman proposal
TC39 is interested ∆ proposal
Field-testing via one or more implementations, refinements
TC39 accepts feature ∆ included in ECMAScript draft
Included in final spec ∆ Standard
Usage:
variable declarations
assignments
parameter definitions
let [x, y] = [ a , b ];
// x= a , y= b
Compare:
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 (2ality.com) ECMAScript 6 2013-05-30 16 / 71
Arrow functions: versions
General form:
Interaction:
> func1(1, 2)
[1, 2]
> func1(1)
[1, 3]
> func1()
[undefined, 3]
Interaction:
> func2(0, 1, 2, 3)
[1, 2, 3]
> func2(0)
[]
> func2()
[]
Interaction:
// ECMAScript 6
let obj = {
__proto__: someObject, // special property
myMethod(arg1, arg2) { // method definition
...
}
};
// ECMAScript 5
var obj = Object.create(someObject);
obj.myMethod = function (arg1, arg2) {
...
};
function computePoint() {
let x = computeX();
let y = computeY();
return { x, y }; // shorthand
}
function handleColor(color) {
switch(color) {
case red:
...
case green:
...
case blue:
...
}
}
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 (2ality.com) ECMAScript 6 2013-05-30 30 / 71
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();
}
}
class Point {
static zero() {
return new Point(0, 0);
}
constructor(x, y) {
this.x = x;
this.y = y;
}
}
let p = Point.zero();
// lib/math.js
let notExported = abc ;
export function square(x) {
return x * x;
}
export const MY_CONSTANT = 123;
// main.js
import {square} from lib/math ;
console.log(square(3));
Alternatively:
import lib/math as math;
console.log(math.square(3));
Dr. Axel Rauschmayer (2ality.com) ECMAScript 6 2013-05-30 34 / 71
Modules: features
Invocation:
map.set(obj, 123);
console.log(map.get(obj)); // 123
console.log(map.has(obj)); // true
map.delete(obj);
console.log(map.has(obj)); // false
class Point {
constructor(x, y) {
Object.assign(this, { x, y });
}
}
And more!
Iterable: Iterator:
traversable data structure pointer for traversing iterable
Examples of iterables:
Arrays
Results produced by tool functions and methods
(keys(), values(), entries()).
for-in:
Basically useless for arrays
Quirky for objects
Array.prototype.forEach():
doesn’t work with iterables
hello
world
next()
yield
next()
yield
function* generatorFunction() {
yield 0;
yield 1;
yield 2;
}
function* iterTree(tree) {
if (Array.isArray(tree)) {
// inner node
for(let i=0; i < tree.length; i++) {
yield* iterTree(tree[i]); // recursion
}
} else {
// leaf
yield tree;
}
}
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);
}
});
ECMAScript specification:
let handler = {
get(target, name, receiver) {
return (...args) => {
console.log( Missing method +name
+ , arguments: +args);
}
}
};
let proxy = Proxy({}, handler);
console.log(parts.year); // 2012
ECMAScript 6:
Advantages:
Raw characters: no need to escape backslash and quote
Multi-line: no need to concatenate strings with newlines at the end