Slide 1

Slide 1 text

JAVASCRIPT ES6 « The future » Kevin Soltysiak - @ksol - http://www.ksol.fr
 StrasbourgJS ~ 2015-19-01

Slide 2

Slide 2 text

JAVASCRIPT ES 6 • New features • Improved syntax • Soon to be standardized

Slide 3

Slide 3 text

ARROW FUNCTIONS • Shorthand for function definitions • Shares the lexical context (unlike regular functions)

Slide 4

Slide 4 text

ARROW FUNCTIONS // Expression bodies var odds = evens.map(v => v + 1); var nums = evens.map((v, i) => v + i); // Lexical this var bob = { _name: "Bob", _friends: [], printFriends: () => { this._friends.forEach(f => console.log(this._name + " knows " + f)); } }

Slide 5

Slide 5 text

ENHANCED LITERALS • Shorthands for writing object literals

Slide 6

Slide 6 text

ENHANCED LITERALS var obj = { // __proto__ __proto__: theProtoObj, // Shorthand for ‘handler: handler’ handler, // Methods toString() { // Super calls return "d " + super.toString(); }, // Computed (dynamic) property names [ "prop_" + (() => 42)() ]: 42 };

Slide 7

Slide 7 text

CLASSES • You can declare classes • Still prototypal inheritance at work, this is only syntactic

Slide 8

Slide 8 text

CLASSES class SkinnedMesh extends THREE.Mesh { constructor(geometry, materials) { super(geometry, materials); this.idMatrix = SkinnedMesh.defaultMatrix(); this.bones = []; this.boneMatrices = []; //... } update(camera) { //... super.update(); } static defaultMatrix() { return new THREE.Matrix4(); } }

Slide 9

Slide 9 text

TEMPLATE STRINGS • Allows interpolations and multiline strings

Slide 10

Slide 10 text

TEMPLATE STRINGS // Basic literal string creation `In JavaScript "\n" is a line-feed.` // Multiline strings `In JavaScript this is not legal.` // Interpolate variable bindings var name = "Bob", time = "today"; `Hello ${name}, how are you ${time}?`

Slide 11

Slide 11 text

DEFAULT ARGUMENTS • Functions can have default arguments • Sugar to convert arguments from and to arrays

Slide 12

Slide 12 text

DEFAULT ARGUMENTS function f(x, y=12) { // y is 12 if not passed (or passed as undefined) return x + y; } f(3) == 15

Slide 13

Slide 13 text

DEFAULT ARGUMENTS function f(x, ...y) { // y is an Array return x * y.length; } f(3, "hello", true) == 6 function f(x, y, z) { return x + y + z; } // Pass each elem of array as argument f(...[1,2,3]) == 6

Slide 14

Slide 14 text

DESTRUCTURING • Allows you to use pattern matching in many contexts

Slide 15

Slide 15 text

DESTRUCTURING // list matching var [a, , b] = [1,2,3]; // object matching var { op: a, lhs: { op: b }, rhs: c } = getASTNode() // object matching shorthand // binds `op`, `lhs` and `rhs` in scope var {op, lhs, rhs} = getASTNode()

Slide 16

Slide 16 text

DESTRUCTURING / Can be used in parameter position function g({name: x}) { console.log(x); } g({name: 5}) // Fail-soft destructuring with defaults var [a = 1] = []; a === 1;

Slide 17

Slide 17 text

LET + CONST • « var » binds to the function • « let » binds to the block • « const » is single-assignment

Slide 18

Slide 18 text

LET + CONST function f() { { let x; { let y = "out"; // okay, block scoped name const x = « sneaky"; // error, const x = "foo"; } // error, already declared in block let x = « inner »; // y is not defined here } }

Slide 19

Slide 19 text

ITERATORS • Allows you to create custom iterators • Used with « for … of »

Slide 20

Slide 20 text

ITERATORS let fibonacci = { [Symbol.iterator]() { let pre = 0, cur = 1; return { next() { [pre, cur] = [cur, pre + cur]; return { done: false, value: cur } } } } }

Slide 21

Slide 21 text

ITERATORS for (var n of fibonacci) { // truncate the sequence at 1000 if (n > 1000) break; print(n); }

Slide 22

Slide 22 text

MODULES • Language-level support for component definition • Implicitly async model – no code executes until requested modules are available and processed.

Slide 23

Slide 23 text

MODULES // lib/math.js export function sum(x, y) { return x + y; } export var pi = 3.141593; // app.js import * as math from "lib/math"; alert("2π = " + math.sum(math.pi, math.pi)); // otherApp.js import {sum, pi} from "lib/math"; alert("2π = " + sum(pi, pi));

Slide 24

Slide 24 text

MAP & SET • Efficient data structures for common algorithms • Also available « leak-free » via WeakMap & WeakSet

Slide 25

Slide 25 text

SUBCLASSABLE BUILT-INS • built-ins like Array, Date and DOM Elements can be subclassed.

Slide 26

Slide 26 text

PROMISE • API for asynchronous programming • First class representation of a value that may be made available in the future • Already used in many libraries (jQuery, Ember.js, etc) • Many implementations for today (Q, RSVP, etc)

Slide 27

Slide 27 text

NEW APIS • Many new library additions, including core Math libraries, Array conversion helpers, and Object.assign for copying.

Slide 28

Slide 28 text

NEW APIS "abcde".contains("cd") // true "abc".repeat(3) // "abcabcabc" Array.from(document.querySelectorAll('*')) // Returns a real Array Array.of(1, 2, 3) // Similar to new Array(...), but without special one-arg behavior [0, 0, 0].fill(7, 1) // [0,7,7] [1,2,3].findIndex(x => x == 2) // 1 ["a", "b", "c"].entries() // iterator [0, "a"], [1,"b"], [2,"c"] ["a", "b", "c"].keys() // iterator 0, 1, 2 ["a", "b", "c"].values() // iterator "a", "b", "c"

Slide 29

Slide 29 text

ALSO • Generators • Binary/Octal literals • Unicode • Symbols • Proxy objects • Reflection API • Tail recursive calls

Slide 30

Slide 30 text

SUPPORT ? • In the browser: it’s getting there ! • In node.js: runtime flags can enable some features • io.js: some features enabled by default, other via runtime flags • Spec should be final mid-2015

Slide 31

Slide 31 text

SUPPORT ? • Projects like 6to5 (github/6to5/6to5) compiles ES6 to ES5 • You can develop in ES6 today !

Slide 32

Slide 32 text

QUESTIONS ?

Slide 33

Slide 33 text

THANKS ! Kevin Soltysiak - @ksol - http://www.ksol.fr
 StrasbourgJS ~ 2015-19-01 Slides strongly inspired from github.com/lukehoban/es6features