SlideShare ist ein Scribd-Unternehmen logo
1 von 58
Downloaden Sie, um offline zu lesen
ES.next
EVEN MORE TO COME
Ignacio.Gil@akqa.com
Terminology
• TC39 - Ecma Technical Committee 39. Evolving JavaScript
!
• ECMAScript - Official name of the language. ECMA-262 spec
!
• JavaScript - Implementation of the language
!
• Harmony - Consensus specs from ECMAScript 5 onwards
!
• ES6 - ES.next - Code name for current version ECMAScript 6
Terminology
• ECMAScript 2015 - ES6
!
• ECMAScript 2016 - ES7
!
• etc.
ES5
Arrays
• .some
!
• .every
var employees = [{!
name: 'Bob', !
technical: true!
}, {!
name: 'Peter', !
technical: false!
}];!
!
if (hasTechDepartment(employees)) {!
// ...!
}!
// ES3!
function hasTechDepartment(team) {!
var i,!
length = team.length,!
found = false;!
!
for (i = 0; i < length; i++) {!
if (team[i].technical === true) {!
found = true;!
break;!
}!
}!
return found;!
}!
var employees = [{!
name: 'Bob', !
technical: true!
}, {!
name: 'Peter', !
technical: false!
}];!
!
if (hasTechDepartment(employees)) {!
// ...!
}!
var employees = [{!
name: 'Bob', !
technical: true!
}, {!
name: 'Peter', !
technical: false!
}];!
!
if (hasTechDepartment(employees)) {!
// ...!
}!
// ES5 .forEach!
function hasTechDepartment(team) {!
var found = false;!
!
team.forEach(function (employee) {!
if (employee.technical === true) {!
found = true;!
}!
});!
return found;!
}!
var employees = [{!
name: 'Bob', !
technical: true!
}, {!
name: 'Peter', !
technical: false!
}];!
!
if (hasTechDepartment(employees)) {!
// ...!
}!
// ES5 .some!
function hasTechDepartment(team) {!
return team.some(function (employee) {!
// Exits iteration and returns true if found, otherwise false!
return employee.technical === true;!
});!
}!
var employees = [{!
name: 'Bob', !
technical: true!
}, {!
name: 'Peter', !
technical: false!
}];!
!
!
!
!
// ES5 .some functional style!
if (employees.some(isTech)) { // true!
// ...!
}!
!
// ES5 .every functional style!
if (employees.every(isTech)) { // false!
// ...!
}!
!
function isTech (employee) {!
return employee.technical === true;!
}!
ES6
ES6
• Object literal extensions
• Destructuring assignment
• Template strings
• Iterators, generators & for…of
• Modules
• proxies
• map, set, weakmap, weakset
Enhanced Object Literals
Computed properties (ES5)
// Computed property names (ES5)!
var myObject = {},!
i = 0;!
!
myObject['prop' + ++i] = 'hello';!
myObject['prop' + ++i] = 'world';!
myObject['prop' + ++i] = '!';!
!
console.log(myObject.prop1); // hello!
console.log(myObject.prop2); // world!
console.log(myObject.prop3); // !!
Computed properties (ES6)
// Computed property names (ES6)!
var i = 0,!
myObject = {!
['prop' + ++i]: 'hello',!
['prop' + ++i]: 'world',!
['prop' + ++i]: '!'!
};!
!
console.log(myObject.prop1); // hello!
console.log(myObject.prop2); // world!
console.log(myObject.prop3); // !!
Property definition (ES5)
var x = 1,!
y = 10,!
z = 3;!
!
// Property definition (ES5)!
var point = {!
x: x, !
y: y,!
z: z!
};!
Property definition shorthand (ES6)
var x = 1,!
y = 10,!
z = 3;!
!
// Property definition shorthand (ES6)!
var point = {x, y, z};!
!
console.log(point.x); // 1!
console.log(point.y); // 10!
console.log(point.z); // 3!
Method definition (ES5)
// Method definition (ES5)!
var object = {!
value: 42,!
toString: function() {!
return this.value;!
},!
// ES5 property getter!
get isLarge() {!
return (this.value >= 50) ? true : false;!
}!
};!
!
console.log(object.toString()); // 42!
console.log(object.isLarge); // false!
Method definition shorthand (ES6)
// Method definition shorthand (ES5)!
var object = {!
value: 42,!
toString() {!
return this.value;!
},!
// ES5 property getter!
get isLarge() {!
return (this.value >= 50) ? true : false;!
}!
};!
!
console.log(object.toString()); // 42!
console.log(object.isLarge); // false!
Computed method shorthand
// Computed method definition shorthand!
let index = 1,!
myObject = {!
['getListItem' + index] () { !
return this.list[index];!
},!
list: ['hello', 'world', '!']!
};!
!
console.log(myObject.getListItem1()); // "world"!
Destructuring assignment
Array destructuring
• Extract data from arrays or objects using a syntax that mirrors
the construction of its literals.
var list = ["one", "two", "three"];!
!
// without destructuring!
var one = list[0];!
var two = list[1];!
var three = list[2];!
!
// Array destructuring!
var [one, two, three] = list;!
!
Ignoring returned values
var list = ["one", "two", "three"];!
!
!
// Array destructuring!
var [one, , two] = list;!
!
console.log(one); // one!
console.log(two); // three!
Object destructuring
var object = {!
prop: 42, !
more: true!
};!
!
// Simple object destructuring!
var {prop, more} = object;!
!
console.log(prop); // 42!
console.log(more); // true !
!
!
// Assign new variable names!
var {prop: newProp, more: newMore} = object;!
!
console.log(newProp); // 42!
console.log(newMore); // true!
Nested object
// Nested object!
var metadata = {!
title: "Scratchpad",!
translations: [{!
locale: "de",!
localization_tags: [ ],!
last_edit: "2014-04-14T08:43:37",!
url: "/de/docs/Tools/Scratchpad",!
title: "JavaScript-Umgebung"!
}],!
url: "/en-US/docs/Tools/Scratchpad"!
};!
!
var { title: englishTitle, translations: [{ title: localeTitle }] } = metadata;!
!
console.log(englishTitle); // "Scratchpad"!
console.log(localeTitle); // "JavaScript-Umgebung"!
Template strings
Template strings
• Provides string interpolation sugar. Use back-ticks
// Basic string substitution usage!
var person = 'Nacho';!
!
console.log(`Yo! My name is ${person}!`);!
console.log('Yo! My name is ' + person + '!'); // ES5 equivalent!
Template strings
• Arbitrary JavaScript expressions
// Expression interpolation!
var a = 10,!
b = 10;!
!
console.log(`The number of libs is ${2 * (a + b)} and not ${10 * (a + b)}.`); !
console.log('The number of libs is ' + (2*(a + b)) + ' and not ' + (10*(a + b)) + ‘.');!
!
!
// Functions inside expressions!
function getContact() {!
return {name: 'Nacho', surname: 'Gil'};!
}!
!
console.log(`Hello, ${getContact().name.toUpperCase()}!`); // Hello, NACHO!!
console.log('Hello, ' + getContact().name.toUpperCase() + '!'); // ES5 equivalent!
Template strings
• Multi-lined strings
// Multi-line strings without needing n!
console.log(`string text line 1!
string text line 2`);!
!
console.log('string text line 1nstring text line 2'); // ES5 equivalent!
console.log('string text line 1n!
string text line 2'); // ES5 equivalent, with n!
Tagged template strings
• Transform a Template String by a function
var you = 'John',!
adjective = 'great',!
taggedFn = (literalSections, ...interpolations) => {!
// transform template!
};!
!
// Tagged Template!
taggedFn`Hello ${you}! You are looking ${adjective} today!`!
taggedFn(["Hello ", "! You are looking ", " today!"], you, adjective); // equivalent!
Use cases
• Contextual auto-escaping
!
!
!
• Localisation and formatting
safehtml`<a href="${url}?q=${query}" onclick="alert('${message}')">${message}</a>`;!
i18n`Hello ${name}! You have ${money}:c in your account!`!
Modules
ES6 Modules
• Compact syntax similar to CommonJS
!
• Preference for single/default exports
!
• Support for cyclic dependencies
!
• Support for synchronous & asynchronous loading
!
• Configurable module loading
Named exports (ES6)
// ---- math.js ----!
export function sum(x, y) {!
return x + y;!
}!
export const pi = 3.141593;!
!
!
// ---- app.js ----!
import * as math from "math"; // import all!
alert("2π = " + math.sum(math.pi, math.pi)); // 2π = 6.283186!
!
!
// ---- app.js ---- !
import {sum, pi} from "math"; // object destructuring!
alert("2π = " + sum(pi, pi)); // 2π = 6.283186!
CommonJS
// ---- math.js ----!
var pi = 3.141593;!
function sum(x, y) {!
return x + y;!
}!
module.exports = {!
sum: sum,!
pi: pi!
};!
!
// ---- app.js ----!
var math = require('math');!
alert('2π = ' + math.sum(math.pi, math.pi)); // 2π = 6.283186!
!
// ---- app.js ---- !
var sum = require('math').sum,!
pi = require('math').pi;!
!
alert("2π = " + sum(pi, pi)); // 2π = 6.283186!
Default exports (ES6)
• Modules that only export single values are very common
// ---- myFunc.js ----!
export default function () { /* ... */ };!
!
!
// ---- MyClass.js ----!
export default class { /* ... */ };!
!
!
// ---- main.js ----!
import myFunc from 'myFunc';!
import { default as MyClass } from 'MyClass';!
!
let instance = new MyClass();!
myFunc();!
Default + named exports (ES6)
// ---- underscore.js ----!
export default function (obj) {!
// ...!
}!
export function each(obj, iterator, context) {!
// ...!
}!
export { each as forEach }; // Export under different name!
!
!
// ---- main.js ----!
import _, { each } from 'underscore'; // Default and named exports!
CommonJS
// ---- underscore.js ----!
var _ = function (obj) {!
// ...!
};!
var each = _.each = _.forEach = function (obj, iterator, context) {!
// ...!
};!
!
module.exports = _;!
!
!
// ---- main.js ----!
var _ = require('underscore'),!
each = _.each;!
Why named exports?
•Static module structure
		 - ES6 imports and exports determined at compile time
		 - CommonJS design (flexibility) requires the code to be run
		 - CommonJS property look up is dynamic, hence slow
		 - ES6 ready for static type
		 - ES6 Enables asynchronous loading before running the code
Cyclic dependencies (ES6)
• To be possible, ES6 modules export bindings, not values.
// ---- lib.js ----!
export let counter = 0;!
export function inc() {!
counter++;!
}!
!
!
// ---- main.js ----!
import { inc, counter } from 'lib';!
!
console.log(counter); // 0!
!
inc();!
console.log(counter); // 1!
Module loader API (ES6)
• Programmatically work with modules and scripts
• Load modules conditionally
// Delivered asynchronously via ES6 promises!
System.import('some_module')!
.then( some_module => { ... } )!
.catch( error => { ... } );!
!
!
// Import several modules!
Promise.all( ['module1', 'module2', 'module3'].map(x => System.import(x)) )!
.then( ([module1, module2, module3]) => {!
// Use module1, module2, module3!
});!
ES6 Module standard benefits
• No more UMD (Universal Module Definition)
• End of system fragmentation
• Browser APIs will become modules instead of global variables
• No more objects as namespaces such as Math or JSON!
• Static module structure: performance & optimisations
Iterators & Generators
for..of statement
• Loop that iterates over property values
• Iterable objects like Array, arguments, NodeList, Map, Set,…
// if implemented NodeList.prototype[Symbol.iterator]!
let articleParagraphs = document.querySelectorAll("article > p");!
!
for (let paragraph of articleParagraphs) {!
paragraph.classList.add('read');!
}!
Symbol object
• May be used as a unique identifier for object properties to
describe particular behaviours
!
• Symbol.iterator returns the default iterator for an object
Expose iterator behaviour
• Symbol.iterator returns the iterator
var myArray = [1, 2],!
myArrayIterator = myArray[Symbol.iterator]();!
!
myArrayIterator.next(); // Result: Object {value: 1, done: false}!
myArrayIterator.next(); // Result: Object {value: 2, done: false}!
myArrayIterator.next(); // Result: Object {value: undefined, done: true}!
Iterators
// Object that iterates over words!
function Words(str) {!
this._str = str;!
}!
!
// The iterator factory!
Words.prototype[Symbol.iterator] = function() {!
// ... !
return {!
next: function() {!
// ... !
return {value: match, done: false};!
}!
};!
};!
!
// for..of iteration!
for ( let word of new Words('Hello world') ) {!
console.log(word); // => 'Hello', => 'world'!
}!
Generators
• function* with the ability to stop running and yield values
• Convenient way to create iterators
Words.prototype[Symbol.iterator] = function* () {!
var re = /S+/g;!
var str = this._str;!
var match;!
while (match = re.exec(str)) {!
yield match[0];!
}!
};!
!
// for..of iteration!
for (var word of new Words('Hello world')) {!
console.log(word); // => "Hello", => "world"!
}!
Infinite Generators
// generator function!
function* fibonacci() {!
let [prev, curr] = [0, 1];!
for (;;) {!
[prev, curr] = [curr, prev + curr];!
yield curr;!
}!
}!
!
for (let n of fibonacci()) {!
console.log(i);!
// Make sure you break at some point!!!
if (n > 1000) {!
break;!
}!
}!
Proxies
Proxy
• Define object custom behaviour for fundamental operations
• Define getters/setters for any arbitrary object property or method
// MongoDB save operation for arbitrary collections would throw error if not defined!
db.collection1.save({ a: 1, b: 2})!
db.collection2.save({ a: 1, b: 2})!
!
// Proxies can implement an API for this problem!
var db = new Proxy({}, {!
get: function(receiver, name) {!
return MongoDB.getOrCreateCollection(name);!
}!
});!
Use case: Validation with Proxy
let person = new Proxy({}, {!
set: function(obj, prop, value) {!
if (prop === 'age') {!
if (!Number.isInteger(value)) {!
throw new TypeError('The age is not an integer');!
}!
if (value > 200) {!
throw new RangeError('The age seems invalid');!
}!
}!
// The default behaviour to store the value!
obj[prop] = value;!
}!
});!
!
person.age = 100;!
console.log(person.age); // 100!
person.age = 'young'; // Throws => The age is not an integer!
person.age = 300; // Throws => The age seems invalid!
ES7…
ES7
• Object.observe
• async functions
…
Object.observe()
• Asynchronously observe the changes to an object
• Data binding revolutions
var obj = { foo: 0, bar: 1 };!
!
Object.observe(obj, function(changes) {!
// Array of objects each representing a change!
console.log(changes);!
});!
!
obj.baz = 2; // [{name: 'baz', object: <obj>, type: 'add'}]!
!
obj.foo = 'hello'; // [{name: 'foo', object: <obj>, type: 'update', oldValue: 0}]!
!
delete obj.baz; // [{name: 'baz', object: <obj>, type: 'delete', oldValue: 2}]!
ES7 async functions
• Function can await on a promise. Halts and resumes when ready
• catch can be used to deal with a rejected promise
async function loadStory() {!
try {!
let story = await getJSON('story.json');!
addHtmlToPage(story.html);!
addTextToPage("All done");!
} catch (err) {!
addTextToPage("Argh, broken: " + err.message);!
}!
document.querySelector('.spinner').style.display = 'none';!
}!
!
(async function() {!
await loadStory();!
console.log("Yey, story successfully loaded!");!
}());!
Thanks!
Ignacio.Gil@akqa.com
Resources
• ES6 Compatibility table
• ES6 support in Mozilla - MDN
• Modules: the final syntax - @ality
• ECMAScript 6 equivalents in ES5
• Template Strings - Addy Osmani
• Iterators gonna iterate - Jake Archibald
• The Basics Of ES6 Generators
• ES7 async functions - Jake Archibald

Weitere ähnliche Inhalte

Was ist angesagt?

Building Modern and Secure PHP Applications – Codementor Office Hours with Be...
Building Modern and Secure PHP Applications – Codementor Office Hours with Be...Building Modern and Secure PHP Applications – Codementor Office Hours with Be...
Building Modern and Secure PHP Applications – Codementor Office Hours with Be...Arc & Codementor
 
Ruby on Rails 中級者を目指して - 大場寧子
Ruby on Rails 中級者を目指して - 大場寧子Ruby on Rails 中級者を目指して - 大場寧子
Ruby on Rails 中級者を目指して - 大場寧子Yasuko Ohba
 
How DRY impacts JavaScript performance // Faster JavaScript execution for the...
How DRY impacts JavaScript performance // Faster JavaScript execution for the...How DRY impacts JavaScript performance // Faster JavaScript execution for the...
How DRY impacts JavaScript performance // Faster JavaScript execution for the...Mathias Bynens
 
Yii, frameworks and where PHP is heading to
Yii, frameworks and where PHP is heading toYii, frameworks and where PHP is heading to
Yii, frameworks and where PHP is heading toAlexander Makarov
 
Machine learning on source code
Machine learning on source codeMachine learning on source code
Machine learning on source codesource{d}
 
Esprima - What is that
Esprima - What is thatEsprima - What is that
Esprima - What is thatAbhijeet Pawar
 
Introducing Assetic (NYPHP)
Introducing Assetic (NYPHP)Introducing Assetic (NYPHP)
Introducing Assetic (NYPHP)Kris Wallsmith
 
AST Rewriting Using recast and esprima
AST Rewriting Using recast and esprimaAST Rewriting Using recast and esprima
AST Rewriting Using recast and esprimaStephen Vance
 
Doctrine MongoDB ODM (PDXPHP)
Doctrine MongoDB ODM (PDXPHP)Doctrine MongoDB ODM (PDXPHP)
Doctrine MongoDB ODM (PDXPHP)Kris Wallsmith
 
Getting Started with Microsoft Bot Framework
Getting Started with Microsoft Bot FrameworkGetting Started with Microsoft Bot Framework
Getting Started with Microsoft Bot FrameworkSarah Sexton
 
Learn You a Functional JavaScript for Great Good
Learn You a Functional JavaScript for Great GoodLearn You a Functional JavaScript for Great Good
Learn You a Functional JavaScript for Great GoodMike Harris
 
Alloy Tips & Tricks #TiLon
Alloy Tips & Tricks #TiLonAlloy Tips & Tricks #TiLon
Alloy Tips & Tricks #TiLonFokke Zandbergen
 
Feed Normalization with Ember Data 1.0
Feed Normalization with Ember Data 1.0Feed Normalization with Ember Data 1.0
Feed Normalization with Ember Data 1.0Jeremy Gillick
 
ISUCONアプリを Pythonで書いてみた
ISUCONアプリを Pythonで書いてみたISUCONアプリを Pythonで書いてみた
ISUCONアプリを Pythonで書いてみたmemememomo
 
Word Play in the Digital Age: Building Text Bots with Tracery
Word Play in the Digital Age: Building Text Bots with TraceryWord Play in the Digital Age: Building Text Bots with Tracery
Word Play in the Digital Age: Building Text Bots with TracerySarah Sexton
 
All I Need to Know I Learned by Writing My Own Web Framework
All I Need to Know I Learned by Writing My Own Web FrameworkAll I Need to Know I Learned by Writing My Own Web Framework
All I Need to Know I Learned by Writing My Own Web FrameworkBen Scofield
 
Interceptors: Into the Core of Pedestal
Interceptors: Into the Core of PedestalInterceptors: Into the Core of Pedestal
Interceptors: Into the Core of PedestalKent Ohashi
 

Was ist angesagt? (20)

Building Modern and Secure PHP Applications – Codementor Office Hours with Be...
Building Modern and Secure PHP Applications – Codementor Office Hours with Be...Building Modern and Secure PHP Applications – Codementor Office Hours with Be...
Building Modern and Secure PHP Applications – Codementor Office Hours with Be...
 
Ruby on Rails 中級者を目指して - 大場寧子
Ruby on Rails 中級者を目指して - 大場寧子Ruby on Rails 中級者を目指して - 大場寧子
Ruby on Rails 中級者を目指して - 大場寧子
 
AkJS Meetup - ES6++
AkJS Meetup -  ES6++AkJS Meetup -  ES6++
AkJS Meetup - ES6++
 
How DRY impacts JavaScript performance // Faster JavaScript execution for the...
How DRY impacts JavaScript performance // Faster JavaScript execution for the...How DRY impacts JavaScript performance // Faster JavaScript execution for the...
How DRY impacts JavaScript performance // Faster JavaScript execution for the...
 
Php introduction
Php introductionPhp introduction
Php introduction
 
Yii, frameworks and where PHP is heading to
Yii, frameworks and where PHP is heading toYii, frameworks and where PHP is heading to
Yii, frameworks and where PHP is heading to
 
Machine learning on source code
Machine learning on source codeMachine learning on source code
Machine learning on source code
 
Esprima - What is that
Esprima - What is thatEsprima - What is that
Esprima - What is that
 
Introducing Assetic (NYPHP)
Introducing Assetic (NYPHP)Introducing Assetic (NYPHP)
Introducing Assetic (NYPHP)
 
Modern JS with ES6
Modern JS with ES6Modern JS with ES6
Modern JS with ES6
 
AST Rewriting Using recast and esprima
AST Rewriting Using recast and esprimaAST Rewriting Using recast and esprima
AST Rewriting Using recast and esprima
 
Doctrine MongoDB ODM (PDXPHP)
Doctrine MongoDB ODM (PDXPHP)Doctrine MongoDB ODM (PDXPHP)
Doctrine MongoDB ODM (PDXPHP)
 
Getting Started with Microsoft Bot Framework
Getting Started with Microsoft Bot FrameworkGetting Started with Microsoft Bot Framework
Getting Started with Microsoft Bot Framework
 
Learn You a Functional JavaScript for Great Good
Learn You a Functional JavaScript for Great GoodLearn You a Functional JavaScript for Great Good
Learn You a Functional JavaScript for Great Good
 
Alloy Tips & Tricks #TiLon
Alloy Tips & Tricks #TiLonAlloy Tips & Tricks #TiLon
Alloy Tips & Tricks #TiLon
 
Feed Normalization with Ember Data 1.0
Feed Normalization with Ember Data 1.0Feed Normalization with Ember Data 1.0
Feed Normalization with Ember Data 1.0
 
ISUCONアプリを Pythonで書いてみた
ISUCONアプリを Pythonで書いてみたISUCONアプリを Pythonで書いてみた
ISUCONアプリを Pythonで書いてみた
 
Word Play in the Digital Age: Building Text Bots with Tracery
Word Play in the Digital Age: Building Text Bots with TraceryWord Play in the Digital Age: Building Text Bots with Tracery
Word Play in the Digital Age: Building Text Bots with Tracery
 
All I Need to Know I Learned by Writing My Own Web Framework
All I Need to Know I Learned by Writing My Own Web FrameworkAll I Need to Know I Learned by Writing My Own Web Framework
All I Need to Know I Learned by Writing My Own Web Framework
 
Interceptors: Into the Core of Pedestal
Interceptors: Into the Core of PedestalInterceptors: Into the Core of Pedestal
Interceptors: Into the Core of Pedestal
 

Andere mochten auch

Nous fàrmacs en diabetis: sendes velles i novelles (per Miquel Morera)
Nous fàrmacs en diabetis: sendes velles i novelles (per Miquel Morera)Nous fàrmacs en diabetis: sendes velles i novelles (per Miquel Morera)
Nous fàrmacs en diabetis: sendes velles i novelles (per Miquel Morera)aneronda
 
ESCRITO ESTA EN ACCION. MATEO 12:1-21 (MT. No. 12A) ALGUNOS CONSEJOS, PRODIGI...
ESCRITO ESTA EN ACCION. MATEO 12:1-21 (MT. No. 12A) ALGUNOS CONSEJOS, PRODIGI...ESCRITO ESTA EN ACCION. MATEO 12:1-21 (MT. No. 12A) ALGUNOS CONSEJOS, PRODIGI...
ESCRITO ESTA EN ACCION. MATEO 12:1-21 (MT. No. 12A) ALGUNOS CONSEJOS, PRODIGI...CPV
 
Preparing an effective resume (by henry pingol)
Preparing an effective resume (by henry pingol)Preparing an effective resume (by henry pingol)
Preparing an effective resume (by henry pingol)Henry Pingol
 
Becas ICO Asia-Pacífico
Becas ICO Asia-Pacífico Becas ICO Asia-Pacífico
Becas ICO Asia-Pacífico Cext
 
Didáctica Crítica Circulatorio-Respiratorio
Didáctica Crítica Circulatorio-RespiratorioDidáctica Crítica Circulatorio-Respiratorio
Didáctica Crítica Circulatorio-RespiratorioAsvalglez
 
Effective E-Mail Communication
Effective E-Mail CommunicationEffective E-Mail Communication
Effective E-Mail CommunicationHCNtrainingslides
 
Public Sector CIO Forum
Public Sector CIO ForumPublic Sector CIO Forum
Public Sector CIO ForumVarugis
 
Kokomo Twitter Seminar
Kokomo Twitter SeminarKokomo Twitter Seminar
Kokomo Twitter SeminarKyle Lacy
 
Student Information Center: WaveNet Central Training Guide
Student Information Center: WaveNet Central Training GuideStudent Information Center: WaveNet Central Training Guide
Student Information Center: WaveNet Central Training GuideWavenetcentral
 
Flemish public libraries on the web: a hybrid library mediation model
Flemish public libraries on the web: a hybrid library mediation modelFlemish public libraries on the web: a hybrid library mediation model
Flemish public libraries on the web: a hybrid library mediation modelBibnet vzw
 
Prueba gazapito quiere comer torta
Prueba gazapito quiere comer tortaPrueba gazapito quiere comer torta
Prueba gazapito quiere comer tortaLuis Tapia
 
eSport - Jugendkultur des 21. Jahrhunderts
eSport - Jugendkultur des 21. JahrhundertseSport - Jugendkultur des 21. Jahrhunderts
eSport - Jugendkultur des 21. JahrhundertsTurtle Entertainment
 
Gerencia de valor y valoración de empresas
Gerencia de valor y valoración de empresasGerencia de valor y valoración de empresas
Gerencia de valor y valoración de empresasemilcediaz29
 

Andere mochten auch (20)

Nous fàrmacs en diabetis: sendes velles i novelles (per Miquel Morera)
Nous fàrmacs en diabetis: sendes velles i novelles (per Miquel Morera)Nous fàrmacs en diabetis: sendes velles i novelles (per Miquel Morera)
Nous fàrmacs en diabetis: sendes velles i novelles (per Miquel Morera)
 
ESCRITO ESTA EN ACCION. MATEO 12:1-21 (MT. No. 12A) ALGUNOS CONSEJOS, PRODIGI...
ESCRITO ESTA EN ACCION. MATEO 12:1-21 (MT. No. 12A) ALGUNOS CONSEJOS, PRODIGI...ESCRITO ESTA EN ACCION. MATEO 12:1-21 (MT. No. 12A) ALGUNOS CONSEJOS, PRODIGI...
ESCRITO ESTA EN ACCION. MATEO 12:1-21 (MT. No. 12A) ALGUNOS CONSEJOS, PRODIGI...
 
Preparing an effective resume (by henry pingol)
Preparing an effective resume (by henry pingol)Preparing an effective resume (by henry pingol)
Preparing an effective resume (by henry pingol)
 
Becas ICO Asia-Pacífico
Becas ICO Asia-Pacífico Becas ICO Asia-Pacífico
Becas ICO Asia-Pacífico
 
Level 5 leadership, brain engineering and nlp workshop at govt 290415
Level 5 leadership, brain engineering and nlp workshop at govt 290415Level 5 leadership, brain engineering and nlp workshop at govt 290415
Level 5 leadership, brain engineering and nlp workshop at govt 290415
 
Consumo Social Media Argentina Junio/13
Consumo Social Media Argentina Junio/13Consumo Social Media Argentina Junio/13
Consumo Social Media Argentina Junio/13
 
Didáctica Crítica Circulatorio-Respiratorio
Didáctica Crítica Circulatorio-RespiratorioDidáctica Crítica Circulatorio-Respiratorio
Didáctica Crítica Circulatorio-Respiratorio
 
Effective E-Mail Communication
Effective E-Mail CommunicationEffective E-Mail Communication
Effective E-Mail Communication
 
Public Sector CIO Forum
Public Sector CIO ForumPublic Sector CIO Forum
Public Sector CIO Forum
 
Kokomo Twitter Seminar
Kokomo Twitter SeminarKokomo Twitter Seminar
Kokomo Twitter Seminar
 
BAPAS_OnlineRecruitment
BAPAS_OnlineRecruitmentBAPAS_OnlineRecruitment
BAPAS_OnlineRecruitment
 
Student Information Center: WaveNet Central Training Guide
Student Information Center: WaveNet Central Training GuideStudent Information Center: WaveNet Central Training Guide
Student Information Center: WaveNet Central Training Guide
 
Flemish public libraries on the web: a hybrid library mediation model
Flemish public libraries on the web: a hybrid library mediation modelFlemish public libraries on the web: a hybrid library mediation model
Flemish public libraries on the web: a hybrid library mediation model
 
El verbo
El verboEl verbo
El verbo
 
Prueba gazapito quiere comer torta
Prueba gazapito quiere comer tortaPrueba gazapito quiere comer torta
Prueba gazapito quiere comer torta
 
Energy system
Energy systemEnergy system
Energy system
 
eSport - Jugendkultur des 21. Jahrhunderts
eSport - Jugendkultur des 21. JahrhundertseSport - Jugendkultur des 21. Jahrhunderts
eSport - Jugendkultur des 21. Jahrhunderts
 
Gerencia de valor y valoración de empresas
Gerencia de valor y valoración de empresasGerencia de valor y valoración de empresas
Gerencia de valor y valoración de empresas
 
El dolor
El dolorEl dolor
El dolor
 
Cómo se hizo una novela. Fortunata y Jacinta
Cómo se hizo una novela. Fortunata y JacintaCómo se hizo una novela. Fortunata y Jacinta
Cómo se hizo una novela. Fortunata y Jacinta
 

Ähnlich wie Es.next

Turn your spaghetti code into ravioli with JavaScript modules
Turn your spaghetti code into ravioli with JavaScript modulesTurn your spaghetti code into ravioli with JavaScript modules
Turn your spaghetti code into ravioli with JavaScript modulesjerryorr
 
ECMAScript2015
ECMAScript2015ECMAScript2015
ECMAScript2015qmmr
 
Plugin jQuery, Design Patterns
Plugin jQuery, Design PatternsPlugin jQuery, Design Patterns
Plugin jQuery, Design PatternsRobert Casanova
 
Moving to modules
Moving to modulesMoving to modules
Moving to modulesSean Mize
 
Impress Your Friends with EcmaScript 2015
Impress Your Friends with EcmaScript 2015Impress Your Friends with EcmaScript 2015
Impress Your Friends with EcmaScript 2015Lukas Ruebbelke
 
Replacing Oracle with MongoDB for a templating application at the Bavarian go...
Replacing Oracle with MongoDB for a templating application at the Bavarian go...Replacing Oracle with MongoDB for a templating application at the Bavarian go...
Replacing Oracle with MongoDB for a templating application at the Bavarian go...Comsysto Reply GmbH
 
MongoDB Munich 2012: MongoDB for official documents in Bavaria
MongoDB Munich 2012: MongoDB for official documents in BavariaMongoDB Munich 2012: MongoDB for official documents in Bavaria
MongoDB Munich 2012: MongoDB for official documents in BavariaMongoDB
 
Serializing EMF models with Xtext
Serializing EMF models with XtextSerializing EMF models with Xtext
Serializing EMF models with Xtextmeysholdt
 
Beginning Scala with Skinny Framework #jjug_ccc
Beginning Scala with Skinny Framework #jjug_cccBeginning Scala with Skinny Framework #jjug_ccc
Beginning Scala with Skinny Framework #jjug_cccKazuhiro Sera
 
Awesomeness of JavaScript…almost
Awesomeness of JavaScript…almostAwesomeness of JavaScript…almost
Awesomeness of JavaScript…almostQuinton Sheppard
 
2013-06-15 - Software Craftsmanship mit JavaScript
2013-06-15 - Software Craftsmanship mit JavaScript2013-06-15 - Software Craftsmanship mit JavaScript
2013-06-15 - Software Craftsmanship mit JavaScriptJohannes Hoppe
 
2013-06-24 - Software Craftsmanship with JavaScript
2013-06-24 - Software Craftsmanship with JavaScript2013-06-24 - Software Craftsmanship with JavaScript
2013-06-24 - Software Craftsmanship with JavaScriptJohannes Hoppe
 
Building a real life application in node js
Building a real life application in node jsBuilding a real life application in node js
Building a real life application in node jsfakedarren
 
"Writing Maintainable JavaScript". Jon Bretman, Badoo
"Writing Maintainable JavaScript". Jon Bretman, Badoo"Writing Maintainable JavaScript". Jon Bretman, Badoo
"Writing Maintainable JavaScript". Jon Bretman, BadooYandex
 

Ähnlich wie Es.next (20)

Turn your spaghetti code into ravioli with JavaScript modules
Turn your spaghetti code into ravioli with JavaScript modulesTurn your spaghetti code into ravioli with JavaScript modules
Turn your spaghetti code into ravioli with JavaScript modules
 
ES6: The future is now
ES6: The future is nowES6: The future is now
ES6: The future is now
 
ECMAScript 6
ECMAScript 6ECMAScript 6
ECMAScript 6
 
ECMAScript2015
ECMAScript2015ECMAScript2015
ECMAScript2015
 
Plugin jQuery, Design Patterns
Plugin jQuery, Design PatternsPlugin jQuery, Design Patterns
Plugin jQuery, Design Patterns
 
Moving to modules
Moving to modulesMoving to modules
Moving to modules
 
Impress Your Friends with EcmaScript 2015
Impress Your Friends with EcmaScript 2015Impress Your Friends with EcmaScript 2015
Impress Your Friends with EcmaScript 2015
 
JavaScript ES6
JavaScript ES6JavaScript ES6
JavaScript ES6
 
Replacing Oracle with MongoDB for a templating application at the Bavarian go...
Replacing Oracle with MongoDB for a templating application at the Bavarian go...Replacing Oracle with MongoDB for a templating application at the Bavarian go...
Replacing Oracle with MongoDB for a templating application at the Bavarian go...
 
MongoDB Munich 2012: MongoDB for official documents in Bavaria
MongoDB Munich 2012: MongoDB for official documents in BavariaMongoDB Munich 2012: MongoDB for official documents in Bavaria
MongoDB Munich 2012: MongoDB for official documents in Bavaria
 
Serializing EMF models with Xtext
Serializing EMF models with XtextSerializing EMF models with Xtext
Serializing EMF models with Xtext
 
"Javascript" por Tiago Rodrigues
"Javascript" por Tiago Rodrigues"Javascript" por Tiago Rodrigues
"Javascript" por Tiago Rodrigues
 
ES6: Features + Rails
ES6: Features + RailsES6: Features + Rails
ES6: Features + Rails
 
Beginning Scala with Skinny Framework #jjug_ccc
Beginning Scala with Skinny Framework #jjug_cccBeginning Scala with Skinny Framework #jjug_ccc
Beginning Scala with Skinny Framework #jjug_ccc
 
Awesomeness of JavaScript…almost
Awesomeness of JavaScript…almostAwesomeness of JavaScript…almost
Awesomeness of JavaScript…almost
 
2013-06-15 - Software Craftsmanship mit JavaScript
2013-06-15 - Software Craftsmanship mit JavaScript2013-06-15 - Software Craftsmanship mit JavaScript
2013-06-15 - Software Craftsmanship mit JavaScript
 
2013-06-24 - Software Craftsmanship with JavaScript
2013-06-24 - Software Craftsmanship with JavaScript2013-06-24 - Software Craftsmanship with JavaScript
2013-06-24 - Software Craftsmanship with JavaScript
 
Building a real life application in node js
Building a real life application in node jsBuilding a real life application in node js
Building a real life application in node js
 
The Beauty of Java Script
The Beauty of Java ScriptThe Beauty of Java Script
The Beauty of Java Script
 
"Writing Maintainable JavaScript". Jon Bretman, Badoo
"Writing Maintainable JavaScript". Jon Bretman, Badoo"Writing Maintainable JavaScript". Jon Bretman, Badoo
"Writing Maintainable JavaScript". Jon Bretman, Badoo
 

Es.next

  • 1. ES.next EVEN MORE TO COME Ignacio.Gil@akqa.com
  • 2. Terminology • TC39 - Ecma Technical Committee 39. Evolving JavaScript ! • ECMAScript - Official name of the language. ECMA-262 spec ! • JavaScript - Implementation of the language ! • Harmony - Consensus specs from ECMAScript 5 onwards ! • ES6 - ES.next - Code name for current version ECMAScript 6
  • 3. Terminology • ECMAScript 2015 - ES6 ! • ECMAScript 2016 - ES7 ! • etc.
  • 4. ES5
  • 6. var employees = [{! name: 'Bob', ! technical: true! }, {! name: 'Peter', ! technical: false! }];! ! if (hasTechDepartment(employees)) {! // ...! }!
  • 7. // ES3! function hasTechDepartment(team) {! var i,! length = team.length,! found = false;! ! for (i = 0; i < length; i++) {! if (team[i].technical === true) {! found = true;! break;! }! }! return found;! }! var employees = [{! name: 'Bob', ! technical: true! }, {! name: 'Peter', ! technical: false! }];! ! if (hasTechDepartment(employees)) {! // ...! }!
  • 8. var employees = [{! name: 'Bob', ! technical: true! }, {! name: 'Peter', ! technical: false! }];! ! if (hasTechDepartment(employees)) {! // ...! }! // ES5 .forEach! function hasTechDepartment(team) {! var found = false;! ! team.forEach(function (employee) {! if (employee.technical === true) {! found = true;! }! });! return found;! }!
  • 9. var employees = [{! name: 'Bob', ! technical: true! }, {! name: 'Peter', ! technical: false! }];! ! if (hasTechDepartment(employees)) {! // ...! }! // ES5 .some! function hasTechDepartment(team) {! return team.some(function (employee) {! // Exits iteration and returns true if found, otherwise false! return employee.technical === true;! });! }!
  • 10. var employees = [{! name: 'Bob', ! technical: true! }, {! name: 'Peter', ! technical: false! }];! ! ! ! ! // ES5 .some functional style! if (employees.some(isTech)) { // true! // ...! }! ! // ES5 .every functional style! if (employees.every(isTech)) { // false! // ...! }! ! function isTech (employee) {! return employee.technical === true;! }!
  • 11. ES6
  • 12. ES6 • Object literal extensions • Destructuring assignment • Template strings • Iterators, generators & for…of • Modules • proxies • map, set, weakmap, weakset
  • 14. Computed properties (ES5) // Computed property names (ES5)! var myObject = {},! i = 0;! ! myObject['prop' + ++i] = 'hello';! myObject['prop' + ++i] = 'world';! myObject['prop' + ++i] = '!';! ! console.log(myObject.prop1); // hello! console.log(myObject.prop2); // world! console.log(myObject.prop3); // !!
  • 15. Computed properties (ES6) // Computed property names (ES6)! var i = 0,! myObject = {! ['prop' + ++i]: 'hello',! ['prop' + ++i]: 'world',! ['prop' + ++i]: '!'! };! ! console.log(myObject.prop1); // hello! console.log(myObject.prop2); // world! console.log(myObject.prop3); // !!
  • 16. Property definition (ES5) var x = 1,! y = 10,! z = 3;! ! // Property definition (ES5)! var point = {! x: x, ! y: y,! z: z! };!
  • 17. Property definition shorthand (ES6) var x = 1,! y = 10,! z = 3;! ! // Property definition shorthand (ES6)! var point = {x, y, z};! ! console.log(point.x); // 1! console.log(point.y); // 10! console.log(point.z); // 3!
  • 18. Method definition (ES5) // Method definition (ES5)! var object = {! value: 42,! toString: function() {! return this.value;! },! // ES5 property getter! get isLarge() {! return (this.value >= 50) ? true : false;! }! };! ! console.log(object.toString()); // 42! console.log(object.isLarge); // false!
  • 19. Method definition shorthand (ES6) // Method definition shorthand (ES5)! var object = {! value: 42,! toString() {! return this.value;! },! // ES5 property getter! get isLarge() {! return (this.value >= 50) ? true : false;! }! };! ! console.log(object.toString()); // 42! console.log(object.isLarge); // false!
  • 20. Computed method shorthand // Computed method definition shorthand! let index = 1,! myObject = {! ['getListItem' + index] () { ! return this.list[index];! },! list: ['hello', 'world', '!']! };! ! console.log(myObject.getListItem1()); // "world"!
  • 22. Array destructuring • Extract data from arrays or objects using a syntax that mirrors the construction of its literals. var list = ["one", "two", "three"];! ! // without destructuring! var one = list[0];! var two = list[1];! var three = list[2];! ! // Array destructuring! var [one, two, three] = list;! !
  • 23. Ignoring returned values var list = ["one", "two", "three"];! ! ! // Array destructuring! var [one, , two] = list;! ! console.log(one); // one! console.log(two); // three!
  • 24. Object destructuring var object = {! prop: 42, ! more: true! };! ! // Simple object destructuring! var {prop, more} = object;! ! console.log(prop); // 42! console.log(more); // true ! ! ! // Assign new variable names! var {prop: newProp, more: newMore} = object;! ! console.log(newProp); // 42! console.log(newMore); // true!
  • 25. Nested object // Nested object! var metadata = {! title: "Scratchpad",! translations: [{! locale: "de",! localization_tags: [ ],! last_edit: "2014-04-14T08:43:37",! url: "/de/docs/Tools/Scratchpad",! title: "JavaScript-Umgebung"! }],! url: "/en-US/docs/Tools/Scratchpad"! };! ! var { title: englishTitle, translations: [{ title: localeTitle }] } = metadata;! ! console.log(englishTitle); // "Scratchpad"! console.log(localeTitle); // "JavaScript-Umgebung"!
  • 27. Template strings • Provides string interpolation sugar. Use back-ticks // Basic string substitution usage! var person = 'Nacho';! ! console.log(`Yo! My name is ${person}!`);! console.log('Yo! My name is ' + person + '!'); // ES5 equivalent!
  • 28. Template strings • Arbitrary JavaScript expressions // Expression interpolation! var a = 10,! b = 10;! ! console.log(`The number of libs is ${2 * (a + b)} and not ${10 * (a + b)}.`); ! console.log('The number of libs is ' + (2*(a + b)) + ' and not ' + (10*(a + b)) + ‘.');! ! ! // Functions inside expressions! function getContact() {! return {name: 'Nacho', surname: 'Gil'};! }! ! console.log(`Hello, ${getContact().name.toUpperCase()}!`); // Hello, NACHO!! console.log('Hello, ' + getContact().name.toUpperCase() + '!'); // ES5 equivalent!
  • 29. Template strings • Multi-lined strings // Multi-line strings without needing n! console.log(`string text line 1! string text line 2`);! ! console.log('string text line 1nstring text line 2'); // ES5 equivalent! console.log('string text line 1n! string text line 2'); // ES5 equivalent, with n!
  • 30. Tagged template strings • Transform a Template String by a function var you = 'John',! adjective = 'great',! taggedFn = (literalSections, ...interpolations) => {! // transform template! };! ! // Tagged Template! taggedFn`Hello ${you}! You are looking ${adjective} today!`! taggedFn(["Hello ", "! You are looking ", " today!"], you, adjective); // equivalent!
  • 31. Use cases • Contextual auto-escaping ! ! ! • Localisation and formatting safehtml`<a href="${url}?q=${query}" onclick="alert('${message}')">${message}</a>`;! i18n`Hello ${name}! You have ${money}:c in your account!`!
  • 33. ES6 Modules • Compact syntax similar to CommonJS ! • Preference for single/default exports ! • Support for cyclic dependencies ! • Support for synchronous & asynchronous loading ! • Configurable module loading
  • 34. Named exports (ES6) // ---- math.js ----! export function sum(x, y) {! return x + y;! }! export const pi = 3.141593;! ! ! // ---- app.js ----! import * as math from "math"; // import all! alert("2π = " + math.sum(math.pi, math.pi)); // 2π = 6.283186! ! ! // ---- app.js ---- ! import {sum, pi} from "math"; // object destructuring! alert("2π = " + sum(pi, pi)); // 2π = 6.283186!
  • 35. CommonJS // ---- math.js ----! var pi = 3.141593;! function sum(x, y) {! return x + y;! }! module.exports = {! sum: sum,! pi: pi! };! ! // ---- app.js ----! var math = require('math');! alert('2π = ' + math.sum(math.pi, math.pi)); // 2π = 6.283186! ! // ---- app.js ---- ! var sum = require('math').sum,! pi = require('math').pi;! ! alert("2π = " + sum(pi, pi)); // 2π = 6.283186!
  • 36. Default exports (ES6) • Modules that only export single values are very common // ---- myFunc.js ----! export default function () { /* ... */ };! ! ! // ---- MyClass.js ----! export default class { /* ... */ };! ! ! // ---- main.js ----! import myFunc from 'myFunc';! import { default as MyClass } from 'MyClass';! ! let instance = new MyClass();! myFunc();!
  • 37. Default + named exports (ES6) // ---- underscore.js ----! export default function (obj) {! // ...! }! export function each(obj, iterator, context) {! // ...! }! export { each as forEach }; // Export under different name! ! ! // ---- main.js ----! import _, { each } from 'underscore'; // Default and named exports!
  • 38. CommonJS // ---- underscore.js ----! var _ = function (obj) {! // ...! };! var each = _.each = _.forEach = function (obj, iterator, context) {! // ...! };! ! module.exports = _;! ! ! // ---- main.js ----! var _ = require('underscore'),! each = _.each;!
  • 39. Why named exports? •Static module structure - ES6 imports and exports determined at compile time - CommonJS design (flexibility) requires the code to be run - CommonJS property look up is dynamic, hence slow - ES6 ready for static type - ES6 Enables asynchronous loading before running the code
  • 40. Cyclic dependencies (ES6) • To be possible, ES6 modules export bindings, not values. // ---- lib.js ----! export let counter = 0;! export function inc() {! counter++;! }! ! ! // ---- main.js ----! import { inc, counter } from 'lib';! ! console.log(counter); // 0! ! inc();! console.log(counter); // 1!
  • 41. Module loader API (ES6) • Programmatically work with modules and scripts • Load modules conditionally // Delivered asynchronously via ES6 promises! System.import('some_module')! .then( some_module => { ... } )! .catch( error => { ... } );! ! ! // Import several modules! Promise.all( ['module1', 'module2', 'module3'].map(x => System.import(x)) )! .then( ([module1, module2, module3]) => {! // Use module1, module2, module3! });!
  • 42. ES6 Module standard benefits • No more UMD (Universal Module Definition) • End of system fragmentation • Browser APIs will become modules instead of global variables • No more objects as namespaces such as Math or JSON! • Static module structure: performance & optimisations
  • 44. for..of statement • Loop that iterates over property values • Iterable objects like Array, arguments, NodeList, Map, Set,… // if implemented NodeList.prototype[Symbol.iterator]! let articleParagraphs = document.querySelectorAll("article > p");! ! for (let paragraph of articleParagraphs) {! paragraph.classList.add('read');! }!
  • 45. Symbol object • May be used as a unique identifier for object properties to describe particular behaviours ! • Symbol.iterator returns the default iterator for an object
  • 46. Expose iterator behaviour • Symbol.iterator returns the iterator var myArray = [1, 2],! myArrayIterator = myArray[Symbol.iterator]();! ! myArrayIterator.next(); // Result: Object {value: 1, done: false}! myArrayIterator.next(); // Result: Object {value: 2, done: false}! myArrayIterator.next(); // Result: Object {value: undefined, done: true}!
  • 47. Iterators // Object that iterates over words! function Words(str) {! this._str = str;! }! ! // The iterator factory! Words.prototype[Symbol.iterator] = function() {! // ... ! return {! next: function() {! // ... ! return {value: match, done: false};! }! };! };! ! // for..of iteration! for ( let word of new Words('Hello world') ) {! console.log(word); // => 'Hello', => 'world'! }!
  • 48. Generators • function* with the ability to stop running and yield values • Convenient way to create iterators Words.prototype[Symbol.iterator] = function* () {! var re = /S+/g;! var str = this._str;! var match;! while (match = re.exec(str)) {! yield match[0];! }! };! ! // for..of iteration! for (var word of new Words('Hello world')) {! console.log(word); // => "Hello", => "world"! }!
  • 49. Infinite Generators // generator function! function* fibonacci() {! let [prev, curr] = [0, 1];! for (;;) {! [prev, curr] = [curr, prev + curr];! yield curr;! }! }! ! for (let n of fibonacci()) {! console.log(i);! // Make sure you break at some point!!! if (n > 1000) {! break;! }! }!
  • 51. Proxy • Define object custom behaviour for fundamental operations • Define getters/setters for any arbitrary object property or method // MongoDB save operation for arbitrary collections would throw error if not defined! db.collection1.save({ a: 1, b: 2})! db.collection2.save({ a: 1, b: 2})! ! // Proxies can implement an API for this problem! var db = new Proxy({}, {! get: function(receiver, name) {! return MongoDB.getOrCreateCollection(name);! }! });!
  • 52. Use case: Validation with Proxy let person = new Proxy({}, {! set: function(obj, prop, value) {! if (prop === 'age') {! if (!Number.isInteger(value)) {! throw new TypeError('The age is not an integer');! }! if (value > 200) {! throw new RangeError('The age seems invalid');! }! }! // The default behaviour to store the value! obj[prop] = value;! }! });! ! person.age = 100;! console.log(person.age); // 100! person.age = 'young'; // Throws => The age is not an integer! person.age = 300; // Throws => The age seems invalid!
  • 55. Object.observe() • Asynchronously observe the changes to an object • Data binding revolutions var obj = { foo: 0, bar: 1 };! ! Object.observe(obj, function(changes) {! // Array of objects each representing a change! console.log(changes);! });! ! obj.baz = 2; // [{name: 'baz', object: <obj>, type: 'add'}]! ! obj.foo = 'hello'; // [{name: 'foo', object: <obj>, type: 'update', oldValue: 0}]! ! delete obj.baz; // [{name: 'baz', object: <obj>, type: 'delete', oldValue: 2}]!
  • 56. ES7 async functions • Function can await on a promise. Halts and resumes when ready • catch can be used to deal with a rejected promise async function loadStory() {! try {! let story = await getJSON('story.json');! addHtmlToPage(story.html);! addTextToPage("All done");! } catch (err) {! addTextToPage("Argh, broken: " + err.message);! }! document.querySelector('.spinner').style.display = 'none';! }! ! (async function() {! await loadStory();! console.log("Yey, story successfully loaded!");! }());!
  • 58. Resources • ES6 Compatibility table • ES6 support in Mozilla - MDN • Modules: the final syntax - @ality • ECMAScript 6 equivalents in ES5 • Template Strings - Addy Osmani • Iterators gonna iterate - Jake Archibald • The Basics Of ES6 Generators • ES7 async functions - Jake Archibald