際際滷

際際滷Share a Scribd company logo
ES6EcmaScript 6
francjohny
1
Learn ES2015
let vs const
Template Literals
Arrow Functions
Default ParametersClasses
Spread Operator  Rest Operator 
Destructuring
Modules
for of loop
Promises
Symbols
String Interpolation
Enhanced Object Literals
Maps
Sets
2
ES5 ES6
3
Arrow Functions
Arrow Functions
// ES5 function expressions
const arr = [1, 2, 3];
const squares = arr.map(function (x)
{ return x * x });
// ES6 fat arrow => syntax
const arr = [1, 2, 3];
const squares = arr.map(x => x * x);
5
Arrow Function Syntax
() => { ... } // no parameter
x => { ... } // one parameter
(x, y) => { ... } // several parameters
x => { return x * x } // block
x => x * x // expression
6
let const
var vs. let vs. const
function order(x, y) {
if (x > y) {
let tmp = x;
x = y;
y = tmp;
}
console.log(tmp===x);
// ReferenceError: tmp is not defined
return [x, y];
}
let works similarly to var, but
the variable it declares is
block-scoped, it only exists
within the current block. var
is function-scoped.
8
var vs. let vs. const
const foo;
// SyntaxError: missing = in const declaration
const bar = 123;
bar = 456; // TypeError: `bar` is read-only
const obj = {};
obj.prop = 123;
console.log(obj.prop); // 123
const works like let, but the variable
you declare must be immediately
initialised, with a value that cant be
changed afterwards.
9
var vs. let vs. const
function logArgs(...args) {
for (const [index, elem] of
args.entries()) {
const message = index + '. ' +
elem;
console.log(message);
}
}
logArgs('Hello', 'everyone');
// Output:
// 0. Hello
// 1. everyone
10
var vs. let vs. const
const arr = [];
for (var i=0; i < 3; i++) {
arr.push(() => i);
}
arr.map(x => x()); // [3,3,3]
const arr = [];
for (let i=0; i < 3; i++) {
arr.push(() => i);
}
arr.map(x => x()); // [0,1,2]
11
Default Params
Default Parameters
// OK: `y` accesses `x` after it has been declared
function foo(x=1, y=x) {
return [x, y];
}
foo(); // [1,1]
// Exception: `x` tries to access `y` within TDZ
function bar(x=y, y=2) {
return [x, y];
}
bar(); // ReferenceError
13
Destructuring
14
Extracting values from data stored in objects and Arrays
Object Destructuring
15
const obj = { first: 'Jane', last:
'Doe' };
const {first: f, last: l} = obj;
// f = 'Jane'; l = 'Doe'
// {prop} is short for {prop: prop}
const {first, last} = obj;
// first = 'Jane'; last = 'Doe'
Array Destructuring
16
const iterable = ['a', 'b'];
const [x, y] = iterable;
// x = 'a'; y = b
const [all, year, month, day] =
/^(dddd)-(dd)-(dd)$/
.exec(2999-12-31');
[a, b] = [b, a]; //swap values
Array Destructuring
17
const [,, x, y] = ['a', 'b', 'c', d'];
// x = 'c'; y = d'
const [x, ...y] = ['a', 'b', 'c'];
// x='a'; y=['b', c']
const {length : len} = 'abc';
// len = 3
const {toString: s} = 123;
// s = Number.prototype.toString
Rest operator 
18
const [x, ...y] = ['a', 'b', 'c'];
// x='a'; y=['b', c']
const [x, y, ...z] = ['a'];
// x='a'; y=undefined; z=[]
const [x, ...[y, z]] = ['a', 'b', 'c'];
// x = 'a'; y = 'b'; z = 'c'
Spread operator 
19
In function and constructor calls, the spread
operator turns iterable values into arguments:
In Array literals, the spread operator turns
iterable values into Array elements:
Math.max(-1, 5, 11, 3) // 11
Math.max(...[-1, 5, 11, 3]) // 11
Math.max(-1, ...[-1, 5, 11], 3) // 11
[1, ...[2,3], 4] // [1, 2, 3, 4]
Template Literals
20
String literals that can stretch across multiple lines
and include interpolated expressions
Template Literals
21
const firstName = 'Jane';
console.log(`Hello ${firstName}!
How are you
today?`);
// Output:
// Hello Jane!
// How are you
// today?
Tagged template literals
22
tagFunction`Hello ${firstName} $
{lastName}!`
tagFunction(['Hello ', ' ', '!'],
firstName, lastName)
const str = String.raw`This is a text
with multiple lines.
Escapes are not interpreted,
n is not a newline.`;
Classes
23
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;
}
}
Modules
24
Multiple named exports
25
//------ lib.js ------
export const sqrt = Math.sqrt;
export function square(x) {
return x * x;
}
export function diag(x, y) {
return sqrt(square(x) + square(y));
}
//------ main.js ------
import { square, diag } from 'lib';
console.log(square(11)); // 121
console.log(diag(4, 3)); // 5
Single default exports
26
//------ myFunc.js ------
export default function () { 揃揃揃 }
//------ main1.js ------
import myFunc from 'myFunc';
myFunc();
//------ MyClass.js ------
export default class { 揃揃揃 }
//------ main2.js ------
import MyClass from 'MyClass';
const inst = new MyClass();
Symbols
27
A new primitive type in ECMAScript 6
Use case 1: unique property keys
28
const MY_KEY = Symbol();
const obj = {};
obj[MY_KEY] = 123;
console.log(obj[MY_KEY]); // 123
Use case 2: constants representing concepts
29
const COLOR_RED = Symbol('Red');
const COLOR_ORANGE = Symbol('Orange');
const COLOR_YELLOW = Symbol('Yellow');
function getComplement(color) {
switch (color) {
case COLOR_RED:
return COLOR_GREEN;
case COLOR_ORANGE:
return COLOR_BLUE;
case COLOR_YELLOW:
return COLOR_YELLOW;
default:
throw new Exception('Unknown color:
'+color);
}
}
forin vs forof
30
var arr = [ 3, 5, 7 ];
arr.foo = hello";
for (var i in arr) {
console.log(i) // "0","1","2","foo"
}
for (var i of arr) {
console.log(i) // 3, 5, 7
}

More Related Content

What's hot (20)

Py3k
Py3kPy3k
Py3k
Andrew Svetlov
遺鉛看逮顎姻庄温稼帥掘鉛庄恰庄姻
遺鉛看逮顎姻庄温稼帥掘鉛庄恰庄姻遺鉛看逮顎姻庄温稼帥掘鉛庄恰庄姻
遺鉛看逮顎姻庄温稼帥掘鉛庄恰庄姻
Kent Ohashi
The Ring programming language version 1.7 book - Part 35 of 196
The Ring programming language version 1.7 book - Part 35 of 196The Ring programming language version 1.7 book - Part 35 of 196
The Ring programming language version 1.7 book - Part 35 of 196
Mahmoud Samir Fayed
C++ L08-Classes Part1
C++ L08-Classes Part1C++ L08-Classes Part1
C++ L08-Classes Part1
Mohammad Shaker
Talk Code
Talk CodeTalk Code
Talk Code
Agiliq Solutions
Code as data as code.
Code as data as code.Code as data as code.
Code as data as code.
Mike Fogus
Java Cheat Sheet
Java Cheat SheetJava Cheat Sheet
Java Cheat Sheet
Saeid Zebardast
Groovy puzzlers jug-moscow-part 2
Groovy puzzlers jug-moscow-part 2Groovy puzzlers jug-moscow-part 2
Groovy puzzlers jug-moscow-part 2
Evgeny Borisov
JavaScript ES6
JavaScript ES6JavaScript ES6
JavaScript ES6
Leo Hernandez
C++ L09-Classes Part2
C++ L09-Classes Part2C++ L09-Classes Part2
C++ L09-Classes Part2
Mohammad Shaker
Opp compile
Opp compileOpp compile
Opp compile
Muhammad Faiz
C++ prgms 3rd unit
C++ prgms 3rd unitC++ prgms 3rd unit
C++ prgms 3rd unit
Ananda Kumar HN
Hidden Gems in Swift
Hidden Gems in SwiftHidden Gems in Swift
Hidden Gems in Swift
Netguru
PythonOOP
PythonOOPPythonOOP
PythonOOP
Veera Pendyala
ECMAScript 6 new features
ECMAScript 6 new featuresECMAScript 6 new features
ECMAScript 6 new features
GephenSG
ScalaFlavor4J
ScalaFlavor4JScalaFlavor4J
ScalaFlavor4J
Kazuhiro Sera
Coding Guidelines - Crafting Clean Code
Coding Guidelines - Crafting Clean CodeCoding Guidelines - Crafting Clean Code
Coding Guidelines - Crafting Clean Code
Ganesh Samarthyam
Type Driven Development with TypeScript
Type Driven Development with TypeScriptType Driven Development with TypeScript
Type Driven Development with TypeScript
Garth Gilmour
Pre-Bootcamp introduction to Elixir
Pre-Bootcamp introduction to ElixirPre-Bootcamp introduction to Elixir
Pre-Bootcamp introduction to Elixir
Pawe Dawczak
Fertile Ground: The Roots of Clojure
Fertile Ground: The Roots of ClojureFertile Ground: The Roots of Clojure
Fertile Ground: The Roots of Clojure
Mike Fogus
遺鉛看逮顎姻庄温稼帥掘鉛庄恰庄姻
遺鉛看逮顎姻庄温稼帥掘鉛庄恰庄姻遺鉛看逮顎姻庄温稼帥掘鉛庄恰庄姻
遺鉛看逮顎姻庄温稼帥掘鉛庄恰庄姻
Kent Ohashi
The Ring programming language version 1.7 book - Part 35 of 196
The Ring programming language version 1.7 book - Part 35 of 196The Ring programming language version 1.7 book - Part 35 of 196
The Ring programming language version 1.7 book - Part 35 of 196
Mahmoud Samir Fayed
C++ L08-Classes Part1
C++ L08-Classes Part1C++ L08-Classes Part1
C++ L08-Classes Part1
Mohammad Shaker
Code as data as code.
Code as data as code.Code as data as code.
Code as data as code.
Mike Fogus
Groovy puzzlers jug-moscow-part 2
Groovy puzzlers jug-moscow-part 2Groovy puzzlers jug-moscow-part 2
Groovy puzzlers jug-moscow-part 2
Evgeny Borisov
C++ L09-Classes Part2
C++ L09-Classes Part2C++ L09-Classes Part2
C++ L09-Classes Part2
Mohammad Shaker
Hidden Gems in Swift
Hidden Gems in SwiftHidden Gems in Swift
Hidden Gems in Swift
Netguru
ECMAScript 6 new features
ECMAScript 6 new featuresECMAScript 6 new features
ECMAScript 6 new features
GephenSG
Coding Guidelines - Crafting Clean Code
Coding Guidelines - Crafting Clean CodeCoding Guidelines - Crafting Clean Code
Coding Guidelines - Crafting Clean Code
Ganesh Samarthyam
Type Driven Development with TypeScript
Type Driven Development with TypeScriptType Driven Development with TypeScript
Type Driven Development with TypeScript
Garth Gilmour
Pre-Bootcamp introduction to Elixir
Pre-Bootcamp introduction to ElixirPre-Bootcamp introduction to Elixir
Pre-Bootcamp introduction to Elixir
Pawe Dawczak
Fertile Ground: The Roots of Clojure
Fertile Ground: The Roots of ClojureFertile Ground: The Roots of Clojure
Fertile Ground: The Roots of Clojure
Mike Fogus

Similar to ECMAScript 6 and beyond (20)

Academy PRO: ES2015
Academy PRO: ES2015Academy PRO: ES2015
Academy PRO: ES2015
Binary Studio
ES6 and AngularAMD
ES6 and AngularAMDES6 and AngularAMD
ES6 and AngularAMD
dhaval10690
FalsyValues. Dmitry Soshnikov - ECMAScript 6
FalsyValues. Dmitry Soshnikov - ECMAScript 6FalsyValues. Dmitry Soshnikov - ECMAScript 6
FalsyValues. Dmitry Soshnikov - ECMAScript 6
Dmitry Soshnikov
Javascript
JavascriptJavascript
Javascript
Vlad Ifrim
ECMAScript 6
ECMAScript 6ECMAScript 6
ECMAScript 6
Piotr Lewandowski
React, Redux, ES2015 by Max Petruck
React, Redux, ES2015 by Max PetruckReact, Redux, ES2015 by Max Petruck
React, Redux, ES2015 by Max Petruck
Lingvokot
Workshop 10: ECMAScript 6
Workshop 10: ECMAScript 6Workshop 10: ECMAScript 6
Workshop 10: ECMAScript 6
Visual Engineering
HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6
HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6
HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6
Dmitry Soshnikov
Javascript Basics
Javascript BasicsJavascript Basics
Javascript Basics
msemenistyi
React, Redux, ES2015 by Max Petruck
React, Redux, ES2015   by Max PetruckReact, Redux, ES2015   by Max Petruck
React, Redux, ES2015 by Max Petruck
Maksym Petruk
ES6 Overview
ES6 OverviewES6 Overview
ES6 Overview
Bruno Scopelliti
Es6 hackathon
Es6 hackathonEs6 hackathon
Es6 hackathon
Justin Alexander
ES6: Features + Rails
ES6: Features + RailsES6: Features + Rails
ES6: Features + Rails
Santosh Wadghule
What's New In C# 7
What's New In C# 7What's New In C# 7
What's New In C# 7
Paulo Morgado
JavaScript - Agora nervoso
JavaScript - Agora nervosoJavaScript - Agora nervoso
JavaScript - Agora nervoso
Luis Vendrame
SDC - Einf端hrung in Scala
SDC - Einf端hrung in ScalaSDC - Einf端hrung in Scala
SDC - Einf端hrung in Scala
Christian Baranowski
ES6 PPT FOR 2016
ES6 PPT FOR 2016ES6 PPT FOR 2016
ES6 PPT FOR 2016
Manoj Kumar
Java cheatsheet
Java cheatsheetJava cheatsheet
Java cheatsheet
Anass SABANI
TypeScript Introduction
TypeScript IntroductionTypeScript Introduction
TypeScript Introduction
Dmitry Sheiko
The java language cheat sheet
The java language cheat sheetThe java language cheat sheet
The java language cheat sheet
anand_study
Academy PRO: ES2015
Academy PRO: ES2015Academy PRO: ES2015
Academy PRO: ES2015
Binary Studio
ES6 and AngularAMD
ES6 and AngularAMDES6 and AngularAMD
ES6 and AngularAMD
dhaval10690
FalsyValues. Dmitry Soshnikov - ECMAScript 6
FalsyValues. Dmitry Soshnikov - ECMAScript 6FalsyValues. Dmitry Soshnikov - ECMAScript 6
FalsyValues. Dmitry Soshnikov - ECMAScript 6
Dmitry Soshnikov
React, Redux, ES2015 by Max Petruck
React, Redux, ES2015 by Max PetruckReact, Redux, ES2015 by Max Petruck
React, Redux, ES2015 by Max Petruck
Lingvokot
HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6
HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6
HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6
Dmitry Soshnikov
Javascript Basics
Javascript BasicsJavascript Basics
Javascript Basics
msemenistyi
React, Redux, ES2015 by Max Petruck
React, Redux, ES2015   by Max PetruckReact, Redux, ES2015   by Max Petruck
React, Redux, ES2015 by Max Petruck
Maksym Petruk
What's New In C# 7
What's New In C# 7What's New In C# 7
What's New In C# 7
Paulo Morgado
JavaScript - Agora nervoso
JavaScript - Agora nervosoJavaScript - Agora nervoso
JavaScript - Agora nervoso
Luis Vendrame
ES6 PPT FOR 2016
ES6 PPT FOR 2016ES6 PPT FOR 2016
ES6 PPT FOR 2016
Manoj Kumar
Java cheatsheet
Java cheatsheetJava cheatsheet
Java cheatsheet
Anass SABANI
TypeScript Introduction
TypeScript IntroductionTypeScript Introduction
TypeScript Introduction
Dmitry Sheiko
The java language cheat sheet
The java language cheat sheetThe java language cheat sheet
The java language cheat sheet
anand_study

Recently uploaded (20)

TrustArc Webinar - Building your DPIA/PIA Program: Best Practices & Tips
TrustArc Webinar - Building your DPIA/PIA Program: Best Practices & TipsTrustArc Webinar - Building your DPIA/PIA Program: Best Practices & Tips
TrustArc Webinar - Building your DPIA/PIA Program: Best Practices & Tips
TrustArc
Cloud of everything Tech of the 21 century in Aviation
Cloud of everything Tech of the 21 century in AviationCloud of everything Tech of the 21 century in Aviation
Cloud of everything Tech of the 21 century in Aviation
Assem mousa
Early Adopter's Guide to AI Moderation (Preview)
Early Adopter's Guide to AI Moderation (Preview)Early Adopter's Guide to AI Moderation (Preview)
Early Adopter's Guide to AI Moderation (Preview)
nick896721
UiPath Automation Developer Associate Training Series 2025 - Session 2
UiPath Automation Developer Associate Training Series 2025 - Session 2UiPath Automation Developer Associate Training Series 2025 - Session 2
UiPath Automation Developer Associate Training Series 2025 - Session 2
DianaGray10
Technology use over time and its impact on consumers and businesses.pptx
Technology use over time and its impact on consumers and businesses.pptxTechnology use over time and its impact on consumers and businesses.pptx
Technology use over time and its impact on consumers and businesses.pptx
kaylagaze
The Future of Repair: Transparent and Incremental by Botond Denes
The Future of Repair: Transparent and Incremental by Botond DenesThe Future of Repair: Transparent and Incremental by Botond Denes
The Future of Repair: Transparent and Incremental by Botond Denes
ScyllaDB
Transform Your Future with Front-End Development Training
Transform Your Future with Front-End Development TrainingTransform Your Future with Front-End Development Training
Transform Your Future with Front-End Development Training
Vtechlabs
Integrated Operating Window - A Gateway to PM
Integrated Operating Window - A Gateway to PMIntegrated Operating Window - A Gateway to PM
Integrated Operating Window - A Gateway to PM
Farhan Tariq
[Webinar] Scaling Made Simple: Getting Started with No-Code Web Apps
[Webinar] Scaling Made Simple: Getting Started with No-Code Web Apps[Webinar] Scaling Made Simple: Getting Started with No-Code Web Apps
[Webinar] Scaling Made Simple: Getting Started with No-Code Web Apps
Safe Software
DealBook of Ukraine: 2025 edition | AVentures Capital
DealBook of Ukraine: 2025 edition | AVentures CapitalDealBook of Ukraine: 2025 edition | AVentures Capital
DealBook of Ukraine: 2025 edition | AVentures Capital
Yevgen Sysoyev
BoxLang JVM Language : The Future is Dynamic
BoxLang JVM Language : The Future is DynamicBoxLang JVM Language : The Future is Dynamic
BoxLang JVM Language : The Future is Dynamic
Ortus Solutions, Corp
Replacing RocksDB with ScyllaDB in Kafka Streams by Almog Gavra
Replacing RocksDB with ScyllaDB in Kafka Streams by Almog GavraReplacing RocksDB with ScyllaDB in Kafka Streams by Almog Gavra
Replacing RocksDB with ScyllaDB in Kafka Streams by Almog Gavra
ScyllaDB
Fl studio crack version 12.9 Free Download
Fl studio crack version 12.9 Free DownloadFl studio crack version 12.9 Free Download
Fl studio crack version 12.9 Free Download
kherorpacca127
Understanding Traditional AI with Custom Vision & MuleSoft.pptx
Understanding Traditional AI with Custom Vision & MuleSoft.pptxUnderstanding Traditional AI with Custom Vision & MuleSoft.pptx
Understanding Traditional AI with Custom Vision & MuleSoft.pptx
shyamraj55
SMART SENTRY CYBER THREAT INTELLIGENCE IN IIOT
SMART SENTRY CYBER THREAT INTELLIGENCE IN IIOTSMART SENTRY CYBER THREAT INTELLIGENCE IN IIOT
SMART SENTRY CYBER THREAT INTELLIGENCE IN IIOT
TanmaiArni
Gojek Clone Multi-Service Super App.pptx
Gojek Clone Multi-Service Super App.pptxGojek Clone Multi-Service Super App.pptx
Gojek Clone Multi-Service Super App.pptx
V3cube
Revolutionizing-Government-Communication-The-OSWAN-Success-Story
Revolutionizing-Government-Communication-The-OSWAN-Success-StoryRevolutionizing-Government-Communication-The-OSWAN-Success-Story
Revolutionizing-Government-Communication-The-OSWAN-Success-Story
ssuser52ad5e
Brave Browser Crack 1.45.133 Activated 2025
Brave Browser Crack 1.45.133 Activated 2025Brave Browser Crack 1.45.133 Activated 2025
Brave Browser Crack 1.45.133 Activated 2025
kherorpacca00126
Endpoint Backup: 3 Reasons MSPs Ignore It
Endpoint Backup: 3 Reasons MSPs Ignore ItEndpoint Backup: 3 Reasons MSPs Ignore It
Endpoint Backup: 3 Reasons MSPs Ignore It
MSP360
Technology use over time and its impact on consumers and businesses.pptx
Technology use over time and its impact on consumers and businesses.pptxTechnology use over time and its impact on consumers and businesses.pptx
Technology use over time and its impact on consumers and businesses.pptx
kaylagaze
TrustArc Webinar - Building your DPIA/PIA Program: Best Practices & Tips
TrustArc Webinar - Building your DPIA/PIA Program: Best Practices & TipsTrustArc Webinar - Building your DPIA/PIA Program: Best Practices & Tips
TrustArc Webinar - Building your DPIA/PIA Program: Best Practices & Tips
TrustArc
Cloud of everything Tech of the 21 century in Aviation
Cloud of everything Tech of the 21 century in AviationCloud of everything Tech of the 21 century in Aviation
Cloud of everything Tech of the 21 century in Aviation
Assem mousa
Early Adopter's Guide to AI Moderation (Preview)
Early Adopter's Guide to AI Moderation (Preview)Early Adopter's Guide to AI Moderation (Preview)
Early Adopter's Guide to AI Moderation (Preview)
nick896721
UiPath Automation Developer Associate Training Series 2025 - Session 2
UiPath Automation Developer Associate Training Series 2025 - Session 2UiPath Automation Developer Associate Training Series 2025 - Session 2
UiPath Automation Developer Associate Training Series 2025 - Session 2
DianaGray10
Technology use over time and its impact on consumers and businesses.pptx
Technology use over time and its impact on consumers and businesses.pptxTechnology use over time and its impact on consumers and businesses.pptx
Technology use over time and its impact on consumers and businesses.pptx
kaylagaze
The Future of Repair: Transparent and Incremental by Botond Denes
The Future of Repair: Transparent and Incremental by Botond DenesThe Future of Repair: Transparent and Incremental by Botond Denes
The Future of Repair: Transparent and Incremental by Botond Denes
ScyllaDB
Transform Your Future with Front-End Development Training
Transform Your Future with Front-End Development TrainingTransform Your Future with Front-End Development Training
Transform Your Future with Front-End Development Training
Vtechlabs
Integrated Operating Window - A Gateway to PM
Integrated Operating Window - A Gateway to PMIntegrated Operating Window - A Gateway to PM
Integrated Operating Window - A Gateway to PM
Farhan Tariq
[Webinar] Scaling Made Simple: Getting Started with No-Code Web Apps
[Webinar] Scaling Made Simple: Getting Started with No-Code Web Apps[Webinar] Scaling Made Simple: Getting Started with No-Code Web Apps
[Webinar] Scaling Made Simple: Getting Started with No-Code Web Apps
Safe Software
DealBook of Ukraine: 2025 edition | AVentures Capital
DealBook of Ukraine: 2025 edition | AVentures CapitalDealBook of Ukraine: 2025 edition | AVentures Capital
DealBook of Ukraine: 2025 edition | AVentures Capital
Yevgen Sysoyev
BoxLang JVM Language : The Future is Dynamic
BoxLang JVM Language : The Future is DynamicBoxLang JVM Language : The Future is Dynamic
BoxLang JVM Language : The Future is Dynamic
Ortus Solutions, Corp
Replacing RocksDB with ScyllaDB in Kafka Streams by Almog Gavra
Replacing RocksDB with ScyllaDB in Kafka Streams by Almog GavraReplacing RocksDB with ScyllaDB in Kafka Streams by Almog Gavra
Replacing RocksDB with ScyllaDB in Kafka Streams by Almog Gavra
ScyllaDB
Fl studio crack version 12.9 Free Download
Fl studio crack version 12.9 Free DownloadFl studio crack version 12.9 Free Download
Fl studio crack version 12.9 Free Download
kherorpacca127
Understanding Traditional AI with Custom Vision & MuleSoft.pptx
Understanding Traditional AI with Custom Vision & MuleSoft.pptxUnderstanding Traditional AI with Custom Vision & MuleSoft.pptx
Understanding Traditional AI with Custom Vision & MuleSoft.pptx
shyamraj55
SMART SENTRY CYBER THREAT INTELLIGENCE IN IIOT
SMART SENTRY CYBER THREAT INTELLIGENCE IN IIOTSMART SENTRY CYBER THREAT INTELLIGENCE IN IIOT
SMART SENTRY CYBER THREAT INTELLIGENCE IN IIOT
TanmaiArni
Gojek Clone Multi-Service Super App.pptx
Gojek Clone Multi-Service Super App.pptxGojek Clone Multi-Service Super App.pptx
Gojek Clone Multi-Service Super App.pptx
V3cube
Revolutionizing-Government-Communication-The-OSWAN-Success-Story
Revolutionizing-Government-Communication-The-OSWAN-Success-StoryRevolutionizing-Government-Communication-The-OSWAN-Success-Story
Revolutionizing-Government-Communication-The-OSWAN-Success-Story
ssuser52ad5e
Brave Browser Crack 1.45.133 Activated 2025
Brave Browser Crack 1.45.133 Activated 2025Brave Browser Crack 1.45.133 Activated 2025
Brave Browser Crack 1.45.133 Activated 2025
kherorpacca00126
Endpoint Backup: 3 Reasons MSPs Ignore It
Endpoint Backup: 3 Reasons MSPs Ignore ItEndpoint Backup: 3 Reasons MSPs Ignore It
Endpoint Backup: 3 Reasons MSPs Ignore It
MSP360
Technology use over time and its impact on consumers and businesses.pptx
Technology use over time and its impact on consumers and businesses.pptxTechnology use over time and its impact on consumers and businesses.pptx
Technology use over time and its impact on consumers and businesses.pptx
kaylagaze

ECMAScript 6 and beyond

  • 2. Learn ES2015 let vs const Template Literals Arrow Functions Default ParametersClasses Spread Operator Rest Operator Destructuring Modules for of loop Promises Symbols String Interpolation Enhanced Object Literals Maps Sets 2
  • 5. Arrow Functions // ES5 function expressions const arr = [1, 2, 3]; const squares = arr.map(function (x) { return x * x }); // ES6 fat arrow => syntax const arr = [1, 2, 3]; const squares = arr.map(x => x * x); 5
  • 6. Arrow Function Syntax () => { ... } // no parameter x => { ... } // one parameter (x, y) => { ... } // several parameters x => { return x * x } // block x => x * x // expression 6
  • 8. var vs. let vs. const function order(x, y) { if (x > y) { let tmp = x; x = y; y = tmp; } console.log(tmp===x); // ReferenceError: tmp is not defined return [x, y]; } let works similarly to var, but the variable it declares is block-scoped, it only exists within the current block. var is function-scoped. 8
  • 9. var vs. let vs. const const foo; // SyntaxError: missing = in const declaration const bar = 123; bar = 456; // TypeError: `bar` is read-only const obj = {}; obj.prop = 123; console.log(obj.prop); // 123 const works like let, but the variable you declare must be immediately initialised, with a value that cant be changed afterwards. 9
  • 10. var vs. let vs. const function logArgs(...args) { for (const [index, elem] of args.entries()) { const message = index + '. ' + elem; console.log(message); } } logArgs('Hello', 'everyone'); // Output: // 0. Hello // 1. everyone 10
  • 11. var vs. let vs. const const arr = []; for (var i=0; i < 3; i++) { arr.push(() => i); } arr.map(x => x()); // [3,3,3] const arr = []; for (let i=0; i < 3; i++) { arr.push(() => i); } arr.map(x => x()); // [0,1,2] 11
  • 13. Default Parameters // OK: `y` accesses `x` after it has been declared function foo(x=1, y=x) { return [x, y]; } foo(); // [1,1] // Exception: `x` tries to access `y` within TDZ function bar(x=y, y=2) { return [x, y]; } bar(); // ReferenceError 13
  • 14. Destructuring 14 Extracting values from data stored in objects and Arrays
  • 15. Object Destructuring 15 const obj = { first: 'Jane', last: 'Doe' }; const {first: f, last: l} = obj; // f = 'Jane'; l = 'Doe' // {prop} is short for {prop: prop} const {first, last} = obj; // first = 'Jane'; last = 'Doe'
  • 16. Array Destructuring 16 const iterable = ['a', 'b']; const [x, y] = iterable; // x = 'a'; y = b const [all, year, month, day] = /^(dddd)-(dd)-(dd)$/ .exec(2999-12-31'); [a, b] = [b, a]; //swap values
  • 17. Array Destructuring 17 const [,, x, y] = ['a', 'b', 'c', d']; // x = 'c'; y = d' const [x, ...y] = ['a', 'b', 'c']; // x='a'; y=['b', c'] const {length : len} = 'abc'; // len = 3 const {toString: s} = 123; // s = Number.prototype.toString
  • 18. Rest operator 18 const [x, ...y] = ['a', 'b', 'c']; // x='a'; y=['b', c'] const [x, y, ...z] = ['a']; // x='a'; y=undefined; z=[] const [x, ...[y, z]] = ['a', 'b', 'c']; // x = 'a'; y = 'b'; z = 'c'
  • 19. Spread operator 19 In function and constructor calls, the spread operator turns iterable values into arguments: In Array literals, the spread operator turns iterable values into Array elements: Math.max(-1, 5, 11, 3) // 11 Math.max(...[-1, 5, 11, 3]) // 11 Math.max(-1, ...[-1, 5, 11], 3) // 11 [1, ...[2,3], 4] // [1, 2, 3, 4]
  • 20. Template Literals 20 String literals that can stretch across multiple lines and include interpolated expressions
  • 21. Template Literals 21 const firstName = 'Jane'; console.log(`Hello ${firstName}! How are you today?`); // Output: // Hello Jane! // How are you // today?
  • 22. Tagged template literals 22 tagFunction`Hello ${firstName} $ {lastName}!` tagFunction(['Hello ', ' ', '!'], firstName, lastName) const str = String.raw`This is a text with multiple lines. Escapes are not interpreted, n is not a newline.`;
  • 23. Classes 23 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; } }
  • 25. Multiple named exports 25 //------ lib.js ------ export const sqrt = Math.sqrt; export function square(x) { return x * x; } export function diag(x, y) { return sqrt(square(x) + square(y)); } //------ main.js ------ import { square, diag } from 'lib'; console.log(square(11)); // 121 console.log(diag(4, 3)); // 5
  • 26. Single default exports 26 //------ myFunc.js ------ export default function () { 揃揃揃 } //------ main1.js ------ import myFunc from 'myFunc'; myFunc(); //------ MyClass.js ------ export default class { 揃揃揃 } //------ main2.js ------ import MyClass from 'MyClass'; const inst = new MyClass();
  • 27. Symbols 27 A new primitive type in ECMAScript 6
  • 28. Use case 1: unique property keys 28 const MY_KEY = Symbol(); const obj = {}; obj[MY_KEY] = 123; console.log(obj[MY_KEY]); // 123
  • 29. Use case 2: constants representing concepts 29 const COLOR_RED = Symbol('Red'); const COLOR_ORANGE = Symbol('Orange'); const COLOR_YELLOW = Symbol('Yellow'); function getComplement(color) { switch (color) { case COLOR_RED: return COLOR_GREEN; case COLOR_ORANGE: return COLOR_BLUE; case COLOR_YELLOW: return COLOR_YELLOW; default: throw new Exception('Unknown color: '+color); } }
  • 30. forin vs forof 30 var arr = [ 3, 5, 7 ]; arr.foo = hello"; for (var i in arr) { console.log(i) // "0","1","2","foo" } for (var i of arr) { console.log(i) // 3, 5, 7 }