SlideShare a Scribd company logo
ECMAScript 6
Drama in 5 acts
Piotr Lewandowski
20.03.2015
All temporary solutions
become final
Act 1 - Preludium
ES 4
ES 4
ES 6
ES 4
ES 6
ES 2015
Make JavaScript better
for complex application
for libraries
as target of code generators
Pave the Cowpaths
and don't break the web
ECMAScript 6
Modules
Act 2
Design goals
Compact syntax
Support cyclic dependencies
Asynchronous loading
Configurable
Design goals
Compact syntax
Support cyclic dependencies
Asynchronous loading
Configurable
// lib/math.js
export function sum(x, y) {
return x + y;
}
export var pi = 3.141593;
// app.js
import * as math from "lib/math";
math.sum(22, 20); // 42
// Default exports and named exports
import myLib from 'src/mylib';
import { function1, function2 } from 'src/mylib';
Importing
// Default exports and named exports
import myLib from 'src/mylib';
import { function1, function2 } from 'src/mylib';
Importing
// Renaming: import named1 as myNamed1
import { function1 as fn, function2 } from 'src/mylib';
// Default exports and named exports
import myLib from 'src/mylib';
import { function1, function2 } from 'src/mylib';
Importing
// Renaming: import named1 as myNamed1
import { function1 as fn, function2 } from 'src/mylib';
// Importing the module as an object
// (with one property per named export)
import * as mylib from 'src/mylib';
// Default exports and named exports
import myLib from 'src/mylib';
import { function1, function2 } from 'src/mylib';
Importing
// Renaming: import named1 as myNamed1
import { function1 as fn, function2 } from 'src/mylib';
// Importing the module as an object
// (with one property per named export)
import * as mylib from 'src/mylib';
// Only load the module, don’t import anything
import 'src/mylib';
Syntax
Act 3
Object {} enhancement
let firstName = 'Joe';
let lastName = 'Doe';
let person = {
firstName,
['last' + 'Name']: lastName,
getAge() {
return 42;
}
};
person.firstName // Joe
person.lastName // Doe
person.getAge() // 42
var firstName = 'Joe';
var lastName = 'Doe';
var person = {
firstName: firstName,
getAge: function() {
return 42;
}
};
person['last' + 'Name'] = lastName;
person.firstName // Joe
person.lastName // Doe
person.getAge() // 42
ES6 ES5
Destructuring
let [a, , b] = [1, 2, 3];
Destructuring
let [a, , b] = [1, 2, 3];
[a, b] = [b, a]
let person = {
firstName: 'Joe',
lastName: 'Doe'.
age: 42
};
let { firstName, lastName } = person;
firstName // Joe
lastName // Doe
Destructuring
let [a, , b] = [1, 2, 3];
[a, b] = [b, a]
Spread operator
Math.max(...[1, 2, 3]);
// the same as
Math.max(1, 2, 3);
Spread operator
Math.max(...[1, 2, 3]);
// the same as
Math.max(1, 2, 3);
let arr1 = [0, 1, 2];
let arr2 = [3, 4, 5];
arr1.push(...arr2);
Spread operator
Math.max(...[1, 2, 3]);
// the same as
Math.max(1, 2, 3);
let arr1 = [0, 1, 2];
let arr2 = [3, 4, 5];
arr1.push(...arr2);
[head, ...tail] = [1, 2, 3, 4, 5];
Scope - let & const
function foo() {
a = 1;
if (a) {
var a;
let b = a + 2;
Scope - let & const
function foo() {
a = 1;
if (a) {
var a;
let b = a + 2;
console.log( b );
}
console.log( a );
console.log( b );
}
Scope - let & const
function foo() {
a = 1;
if (a) {
var a;
let b = a + 2;
console.log( b );
}
console.log( a );
console.log( b );
}
console.log( b ); // 3
}
console.log( a ); // 1
console.log( b ); // ReferenceError: `b` is not defined
}
function foo() {
a = 1; // careful, `a` has been hoisted!
if (a) {
var a; // hoisted to function scope!
let b = a + 2; // `b` block-scoped to `if` block!
Scope - let & const
function foo() {
a = 1;
if (a) {
var a;
let b = a + 2;
console.log( b );
}
console.log( a );
console.log( b );
}
console.log( b ); // 3
}
console.log( a ); // 1
console.log( b ); // ReferenceError: `b` is not defined
}
function foo() {
a = 1; // careful, `a` has been hoisted!
if (a) {
var a; // hoisted to function scope!
let b = a + 2; // `b` block-scoped to `if` block!
if (true) {
const foo = "foo";
// error
foo = "bar";
}
Functions
Act 4
Classes
class Point {
constructor(x, y) {
this.x = x;
this.y = y;
}
toString() {
return '(' + this.x + ', ' + this.y + ')';
}
}
Classes
class Point {
constructor(x, y) {
this.x = x;
this.y = y;
}
toString() {
return '(' + this.x + ', ' + this.y + ')';
}
}
class ColorPoint extends Point {
constructor(x, y, color) {
super(x, y);
this.color = color;
}
toString() {
return super.toString() + ' in ' + this.color;
}
}
Classes
class Point {
constructor(x, y) {
this.x = x;
this.y = y;
}
toString() {
return '(' + this.x + ', ' + this.y + ')';
}
}
class ColorPoint extends Point {
constructor(x, y, color) {
super(x, y);
this.color = color;
}
toString() {
return super.toString() + ' in ' + this.color;
}
}
let cp = new ColorPoint(25, 8, 'green');
cp.toString(); // '(25, 8) in green'
console.log(cp instanceof ColorPoint); // true
console.log(cp instanceof Point); // true
console.log(typeof Point); // function
Arrow functions
(x, y) => x + y
Lexical scope
class Person {
constructor() {
this.name = 'Joe';
}
// ...
load() {
xhrGet(data => {
this.name = data.name;
});
}
}
function Person {
this.name = 'Joe';
// ...
this.load = function() {
var that = this;
xhrGet(function(data) {
that.name = data.name;
});
}
}
Lexical scope
vs
document.body.addEventListener('click', function(event) {
console.log(event, this) // EventObject, BodyElement
});
document.body.addEventListener('click', event =>
console.log(event, this) // EventObject, WindowElement
);
Default parameters
function add(x = 0, y = 0) {
return x + y;
}
function add(x, y) {
x = x || 0;
y = y || 0;
return x + y;
}
ES6
ES5
Rest parameter
function friends(...friends) {
friends.forEach(friend => {
// ...
}
}
function friends() {
var friends = [].slice.call(arguments, 1);
friends.forEach(function(friend) {
// ...
}
}
ES6
ES5
Can I use it?
Act 5 - Final
Compilators
TypeScript
Traceur
Babel
Compilators
TypeScript
Traceur
Babel
for back-end
Babel-node for node.js
io.js - fork of node.js
Stay tuned
and check
compatibility table
http://kangax.github.io/compat-table/es6/
ECMAScript 6
Further reading
2ality.com
kangax ES6 compatibility table
babeljs.io/docs/learn-es6

More Related Content

ODP
EcmaScript 6
PDF
EcmaScript 6 - The future is here
PDF
An Intro To ES6
PDF
Explaining ES6: JavaScript History and What is to Come
PDF
ES6 - Next Generation Javascript
PDF
ES2015 (ES6) Overview
PPTX
ES6 in Real Life
PDF
JavaScript - new features in ECMAScript 6
EcmaScript 6
EcmaScript 6 - The future is here
An Intro To ES6
Explaining ES6: JavaScript History and What is to Come
ES6 - Next Generation Javascript
ES2015 (ES6) Overview
ES6 in Real Life
JavaScript - new features in ECMAScript 6

What's hot (20)

PDF
Introduction into ES6 JavaScript.
ODP
ES6 PPT FOR 2016
PPTX
Introduction to Ecmascript - ES6
PDF
JavaScript ES6
PPTX
Type Driven Development with TypeScript
PDF
HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6
PDF
Javascript ES6 generators
PDF
"Немного о функциональном программирование в JavaScript" Алексей Коваленко
PPTX
Academy PRO: ES2015
PDF
How to Clone Flappy Bird in Swift
PDF
Proxies are Awesome!
PDF
FalsyValues. Dmitry Soshnikov - ECMAScript 6
PPTX
Category theory, Monads, and Duality in the world of (BIG) Data
PDF
A swift introduction to Swift
PDF
Planet-HTML5-Game-Engine Javascript Performance Enhancement
PDF
Being functional in PHP (DPC 2016)
PPT
Developing iOS apps with Swift
PPTX
EcmaScript unchained
PDF
Swiftの関数型っぽい部分
PDF
[Let'Swift 2019] 실용적인 함수형 프로그래밍 워크샵
Introduction into ES6 JavaScript.
ES6 PPT FOR 2016
Introduction to Ecmascript - ES6
JavaScript ES6
Type Driven Development with TypeScript
HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6
Javascript ES6 generators
"Немного о функциональном программирование в JavaScript" Алексей Коваленко
Academy PRO: ES2015
How to Clone Flappy Bird in Swift
Proxies are Awesome!
FalsyValues. Dmitry Soshnikov - ECMAScript 6
Category theory, Monads, and Duality in the world of (BIG) Data
A swift introduction to Swift
Planet-HTML5-Game-Engine Javascript Performance Enhancement
Being functional in PHP (DPC 2016)
Developing iOS apps with Swift
EcmaScript unchained
Swiftの関数型っぽい部分
[Let'Swift 2019] 실용적인 함수형 프로그래밍 워크샵
Ad

Similar to ECMAScript 6 (20)

PDF
Workshop 10: ECMAScript 6
PDF
JavaScript in 2016
PPTX
JavaScript in 2016 (Codemotion Rome)
PDF
Introduction to ECMAScript 2015
PPTX
Javascript Basics
PDF
Javascript
PDF
ES6: The future is now
PDF
java script functions, classes
PPTX
ECMA5 and ES6 Promises
PDF
Fitc whats new in es6 for web devs
PDF
ECMAScript2015
PDF
React, Redux, ES2015 by Max Petruck
PDF
React, Redux, ES2015 by Max Petruck
PDF
Value Objects, Full Throttle (to be updated for spring TC39 meetings)
PDF
JavaScript Primer
PDF
Es.next
PPT
Intermediate JavaScript
PDF
ESCMAScript 6: Get Ready For The Future. Now
PDF
Es6 to es5
PDF
ECMAScript 6
Workshop 10: ECMAScript 6
JavaScript in 2016
JavaScript in 2016 (Codemotion Rome)
Introduction to ECMAScript 2015
Javascript Basics
Javascript
ES6: The future is now
java script functions, classes
ECMA5 and ES6 Promises
Fitc whats new in es6 for web devs
ECMAScript2015
React, Redux, ES2015 by Max Petruck
React, Redux, ES2015 by Max Petruck
Value Objects, Full Throttle (to be updated for spring TC39 meetings)
JavaScript Primer
Es.next
Intermediate JavaScript
ESCMAScript 6: Get Ready For The Future. Now
Es6 to es5
ECMAScript 6
Ad

Recently uploaded (8)

PPTX
ADVANCE DIGITALS Product update list 2023
PPTX
Malware Removal Livonia by Zebras Computer – Protecting Your Devices from Dig...
PPTX
All-Season Storage_ The Value of 20ft Pop-Up Lockers in Nova Scotia’s Climate...
DOC
York毕业证学历认证,温哥华拉萨尔学院毕业证留学学历
PDF
Supermarket Floral Ad Roundup- Week 32 2025.pdf
PDF
Supermarket Floral Ad Roundup- Week 31 2025.pdf
PDF
Custom Round Plaques at Trophy-World Malaysia | Custom Trophies & Plaques Sup...
PPT
group influence related to consumer behaviour
ADVANCE DIGITALS Product update list 2023
Malware Removal Livonia by Zebras Computer – Protecting Your Devices from Dig...
All-Season Storage_ The Value of 20ft Pop-Up Lockers in Nova Scotia’s Climate...
York毕业证学历认证,温哥华拉萨尔学院毕业证留学学历
Supermarket Floral Ad Roundup- Week 32 2025.pdf
Supermarket Floral Ad Roundup- Week 31 2025.pdf
Custom Round Plaques at Trophy-World Malaysia | Custom Trophies & Plaques Sup...
group influence related to consumer behaviour

ECMAScript 6

  • 1. ECMAScript 6 Drama in 5 acts Piotr Lewandowski 20.03.2015
  • 2. All temporary solutions become final Act 1 - Preludium
  • 6. Make JavaScript better for complex application for libraries as target of code generators
  • 7. Pave the Cowpaths and don't break the web
  • 10. Design goals Compact syntax Support cyclic dependencies Asynchronous loading Configurable
  • 11. Design goals Compact syntax Support cyclic dependencies Asynchronous loading Configurable // lib/math.js export function sum(x, y) { return x + y; } export var pi = 3.141593; // app.js import * as math from "lib/math"; math.sum(22, 20); // 42
  • 12. // Default exports and named exports import myLib from 'src/mylib'; import { function1, function2 } from 'src/mylib'; Importing
  • 13. // Default exports and named exports import myLib from 'src/mylib'; import { function1, function2 } from 'src/mylib'; Importing // Renaming: import named1 as myNamed1 import { function1 as fn, function2 } from 'src/mylib';
  • 14. // Default exports and named exports import myLib from 'src/mylib'; import { function1, function2 } from 'src/mylib'; Importing // Renaming: import named1 as myNamed1 import { function1 as fn, function2 } from 'src/mylib'; // Importing the module as an object // (with one property per named export) import * as mylib from 'src/mylib';
  • 15. // Default exports and named exports import myLib from 'src/mylib'; import { function1, function2 } from 'src/mylib'; Importing // Renaming: import named1 as myNamed1 import { function1 as fn, function2 } from 'src/mylib'; // Importing the module as an object // (with one property per named export) import * as mylib from 'src/mylib'; // Only load the module, don’t import anything import 'src/mylib';
  • 17. Object {} enhancement let firstName = 'Joe'; let lastName = 'Doe'; let person = { firstName, ['last' + 'Name']: lastName, getAge() { return 42; } }; person.firstName // Joe person.lastName // Doe person.getAge() // 42 var firstName = 'Joe'; var lastName = 'Doe'; var person = { firstName: firstName, getAge: function() { return 42; } }; person['last' + 'Name'] = lastName; person.firstName // Joe person.lastName // Doe person.getAge() // 42 ES6 ES5
  • 18. Destructuring let [a, , b] = [1, 2, 3];
  • 19. Destructuring let [a, , b] = [1, 2, 3]; [a, b] = [b, a]
  • 20. let person = { firstName: 'Joe', lastName: 'Doe'. age: 42 }; let { firstName, lastName } = person; firstName // Joe lastName // Doe Destructuring let [a, , b] = [1, 2, 3]; [a, b] = [b, a]
  • 21. Spread operator Math.max(...[1, 2, 3]); // the same as Math.max(1, 2, 3);
  • 22. Spread operator Math.max(...[1, 2, 3]); // the same as Math.max(1, 2, 3); let arr1 = [0, 1, 2]; let arr2 = [3, 4, 5]; arr1.push(...arr2);
  • 23. Spread operator Math.max(...[1, 2, 3]); // the same as Math.max(1, 2, 3); let arr1 = [0, 1, 2]; let arr2 = [3, 4, 5]; arr1.push(...arr2); [head, ...tail] = [1, 2, 3, 4, 5];
  • 24. Scope - let & const function foo() { a = 1; if (a) { var a; let b = a + 2;
  • 25. Scope - let & const function foo() { a = 1; if (a) { var a; let b = a + 2; console.log( b ); } console.log( a ); console.log( b ); }
  • 26. Scope - let & const function foo() { a = 1; if (a) { var a; let b = a + 2; console.log( b ); } console.log( a ); console.log( b ); } console.log( b ); // 3 } console.log( a ); // 1 console.log( b ); // ReferenceError: `b` is not defined } function foo() { a = 1; // careful, `a` has been hoisted! if (a) { var a; // hoisted to function scope! let b = a + 2; // `b` block-scoped to `if` block!
  • 27. Scope - let & const function foo() { a = 1; if (a) { var a; let b = a + 2; console.log( b ); } console.log( a ); console.log( b ); } console.log( b ); // 3 } console.log( a ); // 1 console.log( b ); // ReferenceError: `b` is not defined } function foo() { a = 1; // careful, `a` has been hoisted! if (a) { var a; // hoisted to function scope! let b = a + 2; // `b` block-scoped to `if` block! if (true) { const foo = "foo"; // error foo = "bar"; }
  • 29. Classes class Point { constructor(x, y) { this.x = x; this.y = y; } toString() { return '(' + this.x + ', ' + this.y + ')'; } }
  • 30. Classes class Point { constructor(x, y) { this.x = x; this.y = y; } toString() { return '(' + this.x + ', ' + this.y + ')'; } } class ColorPoint extends Point { constructor(x, y, color) { super(x, y); this.color = color; } toString() { return super.toString() + ' in ' + this.color; } }
  • 31. Classes class Point { constructor(x, y) { this.x = x; this.y = y; } toString() { return '(' + this.x + ', ' + this.y + ')'; } } class ColorPoint extends Point { constructor(x, y, color) { super(x, y); this.color = color; } toString() { return super.toString() + ' in ' + this.color; } } let cp = new ColorPoint(25, 8, 'green'); cp.toString(); // '(25, 8) in green' console.log(cp instanceof ColorPoint); // true console.log(cp instanceof Point); // true console.log(typeof Point); // function
  • 33. Lexical scope class Person { constructor() { this.name = 'Joe'; } // ... load() { xhrGet(data => { this.name = data.name; }); } } function Person { this.name = 'Joe'; // ... this.load = function() { var that = this; xhrGet(function(data) { that.name = data.name; }); } }
  • 34. Lexical scope vs document.body.addEventListener('click', function(event) { console.log(event, this) // EventObject, BodyElement }); document.body.addEventListener('click', event => console.log(event, this) // EventObject, WindowElement );
  • 35. Default parameters function add(x = 0, y = 0) { return x + y; } function add(x, y) { x = x || 0; y = y || 0; return x + y; } ES6 ES5
  • 36. Rest parameter function friends(...friends) { friends.forEach(friend => { // ... } } function friends() { var friends = [].slice.call(arguments, 1); friends.forEach(function(friend) { // ... } } ES6 ES5
  • 37. Can I use it? Act 5 - Final
  • 40. Stay tuned and check compatibility table http://kangax.github.io/compat-table/es6/
  • 42. Further reading 2ality.com kangax ES6 compatibility table babeljs.io/docs/learn-es6