JavaScript (current: ECMAScript 5). This talk: Goals Design process Features When can I use it? Dr. Axel Rauschmayer (rauschma.de) ECMAScript 6 2014-06-13 3 / 65
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 (rauschma.de) ECMAScript 6 2014-06-13 5 / 65
make JavaScript better for complex applications for libraries (including the DOM) as a target of code generators Dr. Axel Rauschmayer (rauschma.de) ECMAScript 6 2014-06-13 6 / 65
Design by “champions” (1–2 experts) Feedback from TC39 and the web development community Field-testing and refining via one or more implementations TC39 has final word on whether/when to include Dr. Axel Rauschmayer (rauschma.de) ECMAScript 6 2014-06-13 7 / 65
1 JavaScript engines: New versions = forced upgrades Must run all existing code ⇒ 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 (rauschma.de) ECMAScript 6 2014-06-13 8 / 65
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 (rauschma.de) ECMAScript 6 2014-06-13 12 / 65
for (let index=0; index < arr.length; index++) { let element = arr[index]; if (predicate(element)) { return { element, index }; } } return { element: undefined, index: -1 }; } 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 (rauschma.de) ECMAScript 6 2014-06-13 13 / 65
in an array. function func2(arg0, ...others) { return others; } Interaction: # func2(0, 1, 2, 3) [1, 2, 3] # func2(0) [] # func2() [] No need for arguments, anymore. Dr. Axel Rauschmayer (rauschma.de) ECMAScript 6 2014-06-13 18 / 65
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 (rauschma.de) ECMAScript 6 2014-06-13 19 / 65
Module IDs are configurable (default: paths relative to importing file) Programmatic (e.g. conditional) loading of modules via an API Dr. Axel Rauschmayer (rauschma.de) ECMAScript 6 2014-06-13 30 / 65
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 (rauschma.de) ECMAScript 6 2014-06-13 33 / 65
– 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 (rauschma.de) ECMAScript 6 2014-06-13 34 / 65
= '/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 (rauschma.de) ECMAScript 6 2014-06-13 35 / 65
Point { constructor(x, y) { Object.assign(this, { x, y }); } } Similar to _.extend() from Underscore.js. Dr. Axel Rauschmayer (rauschma.de) ECMAScript 6 2014-06-13 40 / 65
data structure whose elements can be traversed Iterator: the pointer used for traversal Examples of iterables: Arrays Sets All array-like DOM objects (eventually) Dr. Axel Rauschmayer (rauschma.de) ECMAScript 6 2014-06-13 44 / 65
'world']; for (let elem of arr) { console.log(elem); } Output – elements, not indices: hello world Dr. Axel Rauschmayer (rauschma.de) ECMAScript 6 2014-06-13 46 / 65
let keys = Object.keys(obj); for (let i=0; i < keys.length; i++) { let key = keys[i]; yield [key, obj[key]]; } } let myObj = { foo: 3, bar: 7 }; for (let [key, value] of iterEntries(myObj)) { console.log(key, value); } Output: foo 3 bar 7 Dr. Axel Rauschmayer (rauschma.de) ECMAScript 6 2014-06-13 49 / 65
of primitive value: # let sym = Symbol(); # typeof sym 'symbol' Each symbol is unique. Dr. Axel Rauschmayer (rauschma.de) ECMAScript 6 2014-06-13 52 / 65
= 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 (rauschma.de) ECMAScript 6 2014-06-13 53 / 65
for ECMAScript and frameworks: Introduce publicly known symbols. Example: property key Symbol.iterator makes an object iterable. Dr. Axel Rauschmayer (rauschma.de) ECMAScript 6 2014-06-13 55 / 65
Better support for Unicode (strings, regular expressions) Optimized tail calls Proxies (meta-programming) Candidates for ECMAScript 7: Handling binary data Object.observe() for data binding Integers (64 bit, 32 bit, etc.) Dr. Axel Rauschmayer (rauschma.de) ECMAScript 6 2014-06-13 57 / 65
set is frozen. It is mostly being refined now. Time table: End of 2014: specification is finished (except fixing last bugs) March 2015: publication process starts June 2015: formal publication Dr. Axel Rauschmayer (rauschma.de) ECMAScript 6 2014-06-13 58 / 65
engines [4] TypeScript: ECMAScript 6 plus (optional) type annotations Traceur: ES6-to-ES5 compiler that many solutions are based on: Plugins for Grunt, Gulp, Broccoli, etc. es6ify: transform for Browserify ES6 Module Transpiler: compile ES6 modules (subset of ES6) to AMD or CJS ES6 Fiddle: interactively try out ES6 (based on Traceur) Frameworks: Ember.js 1.6 is based on ECMAScript 6 modules (via ES6 Module Transpiler) AngularJS 2 is based on ECMAScript 6 (via Traceur) es6-shim by Paul Miller: features of the ES6 standard library, backported to ES5. More information: es6-tools by Addy Osmani. Dr. Axel Rauschmayer (rauschma.de) ECMAScript 6 2014-06-13 59 / 65
[4] Can be used today, by compiling to ECMAScript 5 Biggest impact on community (currently: too much variety): Classes Modules Dr. Axel Rauschmayer (rauschma.de) ECMAScript 6 2014-06-13 61 / 65
by David Herman 3 “ES6 Modules” by Yehuda Katz 4 “ECMAScript 6 compatibility table” by kangax [features already in JavaScript engines] Dr. Axel Rauschmayer (rauschma.de) ECMAScript 6 2014-06-13 64 / 65
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 (rauschma.de) ECMAScript 6 2014-06-13 65 / 65