JavaScript (current: ECMAScript 5). This talk: Goals Design process Features When can I use it? Warning All information is preliminary, features can and will change. Dr. Axel Rauschmayer (2ality.com) ECMAScript 6 2013-12-04 3 / 69
committee evolving JavaScript. Members: companies (all major browser vendors etc.). Meetings attended by employees and invited experts. ECMAScript: the official name of the language Versions: ECMAScript 5 is short for “ECMAScript Language Specification, Edition 5” JavaScript: colloquially: the language formally: one implementation of ECMAScript ECMAScript Harmony: improvements after ECMAScript 5 (ECMAScript 6 and 7) Dr. Axel Rauschmayer (2ality.com) ECMAScript 6 2013-12-04 5 / 69
[1]: make JavaScript better for complex applications for libraries (possibly including the DOM) as a target of code generators Dr. Axel Rauschmayer (2ality.com) ECMAScript 6 2013-12-04 6 / 69
Design by “champions” (groups of 1–2 experts) Feedback from TC39 and the web community TC39 has final word on whether/when to include Stages of designing a feature [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 Dr. Axel Rauschmayer (2ality.com) ECMAScript 6 2013-12-04 7 / 69
1 JavaScript engines: Must run all existing code New versions = forced upgrades ⇒ ECMAScript 6 only adds features 2 JavaScript code: Must run on all engines that are in use ⇒ wait or compile ECMAScript 6 to ES5 (details later) Dr. Axel Rauschmayer (2ality.com) ECMAScript 6 2013-12-04 8 / 69
is the same as { x: x, y: y }. let obj = { first: 'Jane', last: 'Doe' }; let { first, last } = obj; console.log(first + ' ' + last); // Jane Doe Dr. Axel Rauschmayer (2ality.com) ECMAScript 6 2013-12-04 12 / 69
for (let index=0; index < arr.length; index++) { let element = arr[index]; if (predicate(element)) { return { element, index }; } } } let {element} = findElement(someArray, somePredicate); let {index} = findElement(someArray, somePredicate); // Order doesn't matter: let {index, element} = findElement(...); let {element, index} = findElement(...); Dr. Axel Rauschmayer (2ality.com) ECMAScript 6 2013-12-04 13 / 69
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 (2ality.com) ECMAScript 6 2013-12-04 18 / 69
arguments: # Math.max(7, 4, 11) 11 # Math.max(...[7, 4, 11]) 11 The inverse of a rest parameter Mostly replaces Function.prototype.apply() Also works in constructors Dr. Axel Rauschmayer (2ality.com) ECMAScript 6 2013-12-04 19 / 69
Module IDs are configurable (default: paths relative to importing file) Programmatic (e.g. conditional) loading of modules via an API Module loading is customizable: Automatic linting (think: JSLint, JSHint) Automatically translate files (CoffeeScript, TypeScript) Use legacy modules (AMD, Node.js) Dr. Axel Rauschmayer (2ality.com) ECMAScript 6 2013-12-04 31 / 69
string interpolation. if (x > MAX) { throw new Error(`At most ${MAX} allowed: ${x}`); } Multiple lines, no escaping: var str = raw`This is a text with multiple lines. Escapes are not interpreted, \n is not a newline.`; Dr. Axel Rauschmayer (2ality.com) ECMAScript 6 2013-12-04 34 / 69
– ignored whitespace, named groups, comments 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 `); console.log(parts.year); // 2012 Advantages: Raw characters: no need to escape backslash and quote Multi-line: no need to concatenate strings with newlines at the end Dr. Axel Rauschmayer (2ality.com) ECMAScript 6 2013-12-04 35 / 69
= '/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' )); Dr. Axel Rauschmayer (2ality.com) ECMAScript 6 2013-12-04 36 / 69
of primitive value: # let sym = Symbol(); # typeof sym 'symbol' Each symbol is unique. Dr. Axel Rauschmayer (2ality.com) ECMAScript 6 2013-12-04 39 / 69
= Symbol(); const blue = Symbol(); function handleColor(color) { switch(color) { case red: ... case green: ... case blue: ... } } Previously: var red = 'red'; var green = 'green'; var blue = 'blue'; Dr. Axel Rauschmayer (2ality.com) ECMAScript 6 2013-12-04 40 / 69
for ECMAScript and frameworks: Introduce publicly known symbols. Example: public symbol iterate makes an object iterable (details: later). Dr. Axel Rauschmayer (2ality.com) ECMAScript 6 2013-12-04 42 / 69
Point { constructor(x, y) { Object.assign(this, { x, y }); } } Similar to Underscore.js _.extend(). Dr. Axel Rauschmayer (2ality.com) ECMAScript 6 2013-12-04 46 / 69
data structure whose elements can be traversed Iterator: the pointer used for traversal Examples of iterables: Arrays Sets Results produced by tool functions Examples (built-in, for objects): keys(), values(), entries() All array-like DOM objects (eventually) Dr. Axel Rauschmayer (2ality.com) ECMAScript 6 2013-12-04 50 / 69
over: iterables let arr = [ 'hello', 'world' ]; for (let elem of arr) { console.log(elem); } Output – elements, not indices: hello world Dr. Axel Rauschmayer (2ality.com) ECMAScript 6 2013-12-04 52 / 69
= [1,2,3]; let squares = [for (x of numbers) x * x]; [for (i of document.querySelectorAll('.item')) i.textContent] Generator comprehensions produce generator objects (iterators): let twice = (for (x of iterable) x + x); iterable can be “infinite”! Dr. Axel Rauschmayer (2ality.com) ECMAScript 6 2013-12-04 54 / 69
6: Promises Better support for Unicode (strings, regular expressions) Standard modules Optimized tail calls Probably in ECMAScript 7: Handling binary data Object.observe() for data binding Integers (64 bit, 32 bit, etc.) Dr. Axel Rauschmayer (2ality.com) ECMAScript 6 2013-12-04 55 / 69
engines [5] Traceur by Google: compiles ECMAScript 6 to ECMAScript 5. dynamically (on the fly) statically (e.g. via tools) TypeScript by Microsoft: ECMAScript 6 + optional static typing (at development time) compiles to ECMAScript 5 es6-shim by Paul Miller: features of the ES6 standard library, backported to ES5. Dr. Axel Rauschmayer (2ality.com) ECMAScript 6 2013-12-04 58 / 69
of ES6 supported by late 2014. Can be used today (via TypeScript et al.) Biggest impact on community (currently: too much variety): Classes Modules More on ECMAScript 6 in my 2nd talk. . . Topics: callable entities, iterators, generators, etc. Dr. Axel Rauschmayer (2ality.com) ECMAScript 6 2013-12-04 60 / 69
by David Herman 3 “ES6 Modules” by Yehuda Katz 4 “Why coroutines won’t work on the web” by David Herman 5 “ECMAScript 6 compatibility table” by kangax [features already in JavaScript engines] Dr. Axel Rauschmayer (2ality.com) ECMAScript 6 2013-12-04 63 / 69
mailing list: es-discuss TC39 meeting notes by Rick Waldron “A guide to 2ality’s posts on ECMAScript 6” by Axel Rauschmayer Continuum, an ECMAScript 6 virtual machine written in ECMAScript 3. (Links are embedded in this slide.) Dr. Axel Rauschmayer (2ality.com) ECMAScript 6 2013-12-04 64 / 69
object proxy: let target = {}; let proxy = Proxy(target, h); Each of the following operations triggers a method invocation on h: proxy['foo'] → h.get(target, 'foo', proxy) proxy['foo'] = 123 → h.set(target, 'foo', 123, proxy) 'foo' in proxy → h.has(target, 'foo') for (key in proxy) {...} → h.enumerate(target) Dr. Axel Rauschmayer (2ality.com) ECMAScript 6 2013-12-04 66 / 69
all method invocations to a remote object Implementing data access objects for a database Data binding Logging Dr. Axel Rauschmayer (2ality.com) ECMAScript 6 2013-12-04 69 / 69