SlideShare a Scribd company logo
1 of 102
Download to read offline
ECMAScript 2015
Were you expecting something else?
A History Lesson
A History Lesson
1995
May - Mocha is invented in Netscape by Brendan
Eich.
September - Renamed to LiveScript
December - Renamed to Javascript (Because Java
was popular)
A History Lesson
1996 - JavaScript is taken to standardization in ECMA.
From now on ECMA is the spec. Javascript is an
implementation (ActionScript is another implementation)
1997 - ECMA-262 (ECMAScript)
1998 - ECMAScript 2
1999 - ECMAScript 3
1996
A History Lesson
2005 - Mozilla and Macromedia started Work on
ECMAScript 4. Feature rich and a very major leap
from ECMAScript 3.
Douglas Crockford (Yahoo) And Microsoft opposed
the forming standard.
ECMAScript 3.1 was the compromise.
2005
A History Lesson
2009 - Opposing parties meet in Oslo and achieve
an agreement.
ES3.1 is renamed to ES5 (Which we have today)
In the spirit of peace and agreement, the new
Javascript long term agenda is named “Harmony”
2009
A History Lesson
2015 - ES6 (Part of the “Harmony” umbrella) will be
released.
Starting with ES6 - Version names will be based on
the year of release. so ES6 is ES2015 and ES7
should be ES2016
2015
Declaration and
Scoping
let - A New Hope Scope
if (true) {
var x = 3;
}
console.log(x); // 3
var is function scope
let is block scope
if (true) {
let x = 3;
}
console.log(x); // ReferenceError
To hoist is to raise. Function declarations are hoisted

which means you can do this with functions and vars:
function f() {
return 3;
}
A word about Hoisting
But you can’t do it with classes or let variables
class X{}
f(); //3
new X(); // TypeError
Temporal dead zone
let variable declarations are not hoisted
console.log(x); // ReferenceError
let x = 3;
console.log(x); // 3
The time between block start and declaration is called

“Temporal Dead Zone”. Same with ‘const’
Temporal dead zone
TDZ is based on time and not on location
function block() {
function foo() {
console.log(x);
}
foo(); // ReferenceError
let x = 3;
foo(); // 3
}
Loop scoping
With let You get a fresh iteration per loop
let vals = [];
for (let x = 0; x < 4; x += 1) {
vals.push(() => x);
}
console.log(vals.map(x => x()));
// [0, 1, 2, 3]
* we’re using functions to store by reference

otherwise it won’t prove the point. Full Example here
With var You would get [4, 4, 4, 4] as a result
const
const makes variables immutable
const obj = { par: 3 };
obj = 4; // TypeError
but it doesn't stop you from changing the object values
obj.par = 12; // Fine
you can use Object.freeze() to achieve that.
Object.freeze(obj);
obj.par = 10; // TypeError
console.seal(obj); // 17
obj.newParam = 3 // TypeError
or the Object.seal() which blocks changing object structure
String Templates
String Templates
This is syntactic sugar for string concatenation.
let name = 'John';
`Hi ${name},
Did you know that 5 * 3 = ${5*3}?`
/*
"Hi John,
Did you know that 5 * 3 = 15?"
*/
Anything in ${} is evaluated
Back-ticks are used to enclose the string.
New lines are retained
Enhanced Object
Literals
*
basically just shortening {x:x} to {x}
Shorthand assignment
let x = 3;
let xo = {x};
// xo = { x: 3 }
let x = 3;
let xo = {x:x};
// xo = { x: 3 }
equivalent to:
*
a new syntax for defining methods inside an object literal
Method Definition
let foo = {
f(x) {
return x + 1;
}
};
foo.f(3); // 4
you can make super calls for inherited methods
Super
let foo = {
toString() {
super.toString() + ' with foo!';
}
};
in class constructors you can simply call super() to

call on the parent constructor
foo.toString();
//[object Object] with foo!
*
you can define keys that evaluate on run time inside literals
Computed Properties
let ind = 100;
let foo = {
['question'+ ind]:
'question number ' + ind,
['answer' + ind](x) {
return ind === x;
}
};
foo.question100; // “question number 100”
foo.answer100(100); // true
Symbols
Symbol
a symbol is immutable and unique
let s1 = Symbol('test');
let s2 = Symbol('test');

s1 === s2; // false
the string parameter is for console and debug.
Symbol
let o = {
[Symbol('hellllo')]: 7,
'str': 6
};
JSON.stringify(o); // "{"str":6}"
Object.getOwnPropertyNames(o);
// Array [ "str" ]
Object.getOwnPropertySymbols(o);
// Array [ Symbol(hellllo) ]
since Symbols are hidden, Object allows reflection:
symbol keys are not visible
Symbol
symbols are now used by javascript, internally, with certain
features. for example:
Symbol.iterator

used to provide iterator function
Symbol.hasInstance

used internally for instanceof
Symbol.toPrimitive

used internally to convert to primitives
Classes
Class
class Jedi {
constructor() {
this.forceIsDark = false;
}
toString() {
return (this.forceIsDark ? 'Join' :
'Fear is the path to' ) +
' the dark side';
}
}
this is a class
you cannot define a property inside a class, but getters/
setters can create one to the outside)
Extends
class Sith extends Jedi {
constructor() {
super();
this.forceIsDark = true;
}
}
extends works as you’d expect
super() in a ctor calls the parent ctor
Class
let yoda = new Jedi();
let darth = new Sith();
console.log(yoda.toString());
// Fear is the path to the dark side
console.log(darth.toString());
// Join the dark side
console.log(darth instanceof Sith); //
true
console.log(darth instanceof Jedi); //
true
Static
you can declare static functions
class Util {
static inc(x) { return x + 1 },
static dec(x) { return x - 1 }
}
console.log(Util.inc(3)); // 4
it’s the same as placing them on the prototype object
Class Get/Set
you can define get/set just like in ES5 object literals
class O {
constructor() {
this.mx = 'initial';
}
get x() {
return this.mx;
}
set x(val) {
console.log('x changed');
this.mx = val;
}
}
Class Get/Set
and to the user of the class it would appear like a property
let o = new O();
console.log(o.x); //initial
o.x = 'newval';
// x changed
console.log(o.x); //newval
ES6 classes are not hoisted
var a = new ANumber(3); //ReferenceError
Class Hoisting
class ANumber {
constructor(n) {
this.x = n;
}
}
The Iterator Protocol
[]
{}
“”
Sources
*
on
…
*
Consumers
for
*
The Useless Loop *
Iterating over an array using (for…in) (ES5)
// '0'
// '1'
// '2'
var arr = ['a', 'b', 'c'];
for (var i in arr) {
if (arr.hasOwnProperty(i)) {
console.log(i);
}
}
*
ES6 bring the (for…of) iterator that works as you’d expect
for (let n of ['a', 'b', 'c']) {
console.log(n);
}
The Iterator protocol
// 'a'
// 'b'
// 'c'
*
other syntaxes that expect iterables:
// Spread Operator

[..."abcd"]; 

// Array ["a", "b", "c", “d"]
// Destructure Assignment
[a, b] = "xy";
b; // y
The Iterator protocol
Array.from("123");
// Array [ "1", "2", "3" ]
The Iterator protocol *
manually iterating over an iterable object
let it = [1, 2, 3][Symbol.iterator]();

it.next(); // { value: 1, done: false }

it.next(); // { value: 2, done: false }

it.next(); // { value: 3, done: false }
it.next(); // { value: undefined, done:
true }
The Iterator protocol *
Arrays
Maps
(Not WeakMaps)
Dom queries
Strings
Sets
(Not WeakSets)
Arguments
Iterable data sources
The Iterator protocol *
Anything can be a source for iteration:
function gen(n) {
return {
[Symbol.iterator]() {
let i = 0;
return {
next() { return {
done: (i > n) ? true : false,
value: i++ };
}
};
}
};
}
ITERATOR
ITERABLE
for (let n of gen(10) {
console.log(n);
}
//1
//2
//...
//10
The Iterator protocol
Anything can be a source for iteration:
generators are a great way to create iterables. We’ll get to that
later
Libraries
String
"Hello".startsWith('Hell');
"Goodbye".endsWith('bye');
"Jar".repeat(2); // JarJar
"abcdef".includes("bcd");
String got some new functions
Firefox still uses contains instead of includes. 

That should change
Number
New Number constants and methods
Number.EPSILON

Number.MAX_SAFE_INTEGER

Number.MIN_SAFE_INTEGER
Number.isNaN()
Number.isFinite()
Number.isInteger()
Number.isSafeInteger()
Number.parseFloat()
Number.parseInt()
Array.from()
let arrayLike = {
0: 'zero',
1: 'one',
2: 'two',
3: 'three',
'length' : 4
};
from array-like objects (objects that have indices and length)
Array.from(arrayLike);
// Array ["zero", "one", "two", "three"]
Reminder… *
genTen is a custom iterable that generates 0 to 10
function
[Symbol.iterator]() {
done: (i > n) ?
value: i++ };
}
};
}
};
}
Array.from()
from iterables
Array.from(gen(6));
// Array [ 0, 1, 2, 3, 4, 5, 6 ]
Array.from(gen(6), x => x*x);
//Array [ 0, 1, 4, 9, 16, 25, 36 ]
second parameter is a mapping function
Array.of()
you should use Array.of over Array constructor because:
new Array(2.3);
//RangeError: invalid array length
Array.of(1, 2, 4, 3, 4);
//Array [ 1, 2, 4, 3, 4 ]
a better way to create arrays
Notice how keys() and entries() return an iterator and

not an array
Array.prototype.*
['a','b','c'].keys()
// Array Iterator { }
[...['a','b','c'].keys()]
// Array [ 0, 1, 2 ]
Array.prototype.keys()
Array.from(['a','b','c'].entries())
// [[0,"a"],[1,"b"],[2,"c"]]
Array.prototype.entries()
Array.prototype.*
Array.prototype.find()
[4, 100, 7].find(x => x > 5);
// 100
[4, 100, 7].findIndex(x => x > 5);
// 1
Array.prototype.findIndex()
Array.prototype.fill()
(new Array(7)).fill(2).fill(3, 2, 5);
// Array [ 2, 2, 3, 3, 3, 2, 2 ]
Object
Object.assign()
let x = {a:1};
Object.assign(x, {b:2});
x; // {a:1, b:2}
object.is() checks if two values are the same
Object.is('y', 'y'); // true
Object.is({x:1}, {x:1}); // false
Object.is(NaN, NaN); // true
different than === in (+0 !== -0) and (NaN === NaN)
also - doesn’t coerce values (as == does)
Maps/Sets
Map
ES5 objects are not maps. ES6 introduces real maps
construction
var m = new Map([
[1, ‘first']
]);
m;
// Map { 1: "first"}
Map
ES5 objects are not maps. ES6 introduces real maps
objects can be keys
var m = new Map([
[1, 'first'],
[{}, ‘second']
]);
m;
// Map { 1: "first", Object: "second"}
Map
ES5 objects are not maps. ES6 introduces real maps
functions can be key
var m = new Map([
[1, 'first'],
[{}, 'second']
]);
m.set(x => x+1, ‘third');
// Map { 1: "first", Object: "second",
function (): "third"}
Map
ES5 objects are not maps. ES6 introduces real maps
key equality is === (+NaN is equal to each other)
var m = new Map([
[1, 'first'],
[{}, 'second']
]);
m.set(x => x+1, 'third')
.set({}, 'fourth');
// Map { 1: "first", Object: "second",
function (): "third", Object: "fourth" }
notice how using {} twice creates 2 different keys
Map
var key = {};

var m = new Map([

[key, 'first'], [1, 'second']

]);


m.get(key); // 'first'
m.has({}); // false
m.delete(key); // true
m.size; // 1
m.clear();
m.size; // 0
m.forEach((val, key) => { … });
let’s go over some basic Map api
Mapthere are several ways to iterate over a map
let m =

new Map([...'abcd'].map(x=>[x, x + x]));
JSON.stringify([...m]);
// "[["a","aa"],["b","bb"],["c","cc"],
["d","dd"]]"
JSON.stringify([...m.keys()]);
// "["a","b","c","d"]"
JSON.stringify([...m.values()]);
// "["aa","bb","cc","dd"]"
JSON.stringify([...m.entries()]);
// "[["a","aa"],["b","bb"],["c","cc"],
["d","dd"]]"
WeakMap
WeakMap is a Map which doesn’t prevent garbage collection
• you can’t iterate over it
• no clear() method
• doesn’t have size property
• keys must be objects
WeakMap is good for hiding private class members
Set
Set is a collection of distinct objects
let s = new Set(['red', 'blue']);
s.add(‘yellow’); // adds yellow
s.add(‘red'); // doesn’t add red
s.has('blue'); // true
s.delete('blue'); // true
s.size; // 2
s; // Set ["red", "blue"]
[...s]; // Array [ "red", "blue" ]
s.clear();
Set
for the sake of symmetry with maps, this works:
JSON.stringify([...s.entries()]);
// "[["red","red"],["blue","blue"]]"
JSON.stringify([...s.values()]);
// "["red","blue"]"
sets are iterable
JSON.stringify([...s.keys()]);
// "["red","blue"]"
WeakSets
WeakSet exists but it doesn’t allow iteration. so you can’t do a
whole lot with it. 

You can mark objects (check if in set or not)
Generators
function*
function* genFour() {
yield 1;
yield 2;
yield 3;
return 4;
}
let four = genFour();
the function suspends on every yield and allows other code
to run until the next time it resumes
Consuming
four.next();
// Object { value: 1, done: false }
four.next();
// Object { value: 4, done: true }
four.next();
// Object { value: 2, done: false }
four.next();
// Object { value: 3, done: false }
generators are both an iterator and an iterable
as an iterator:
Consuming
generators are both an iterator and an iterable
[...genFour()];
// Array [ 1, 2, 3, 4 ]
Array.from([...genFour()]);
// Array [ 1, 2, 3, 4 ]
as an iterable:
three.next();
yield 1; // {value:1, done:false}
three.next();
yield 2; // {value:2, done:false}
three.next();
yield 3; // {value:3, done:false}
three.next();
return 4; // {value:4, done:true}
three.next();
// Object { value: undefined,
done: true }
Here is an example consuming an iterator step by step
yield*
yield* yields every yield inside a called generator
function* flatten(arr) {
for (let x of arr) {
if (x instanceof Array) {
yield* flatten(x);
} else {
yield x;
}
}
}
let t = flatten([1, 2, [3, 4]]);
// Array [1, 2, 3, 4]
Arrow Functions
function inc(x) {
return x + 1;
}
let inc = x => x + 1;
is equivalent to:
2 parameters:
let inc = (x, y) => x + y;
no parameters
let inc = () => 7;
The Basics
function inc(x) {
return x + 1;
}
The Basics
more than 1 statement
let inc = (x) => {
console.log(x);
return 7;
}
Lexical this
Arrow functions capture the this value of the enclosing
context. In other words, no more `that`, `self` or `bind(this)`
// wait for it....
// 7
this.x = 7;
setTimeout(() =>
console.log(this.x),
2000);
Promises
Incentive
requestFromServer(function (err, res) {
if (err) {
// do something with error
} else {
// do something with result
doSomethingAsync(function () {
andAnotherAsyncThing(function () {
andAnotherAsyncThing(function () {
andAnotherAsyncThing(function () {
andAnotherAsyncThing(function () {
andAnotherAsyncThing(function () {
andAnotherAsyncThing(function () {
// CALLBACK PYRAMID OF DOOM!!!
});
});
});
});
});
});
});
}
});
Chaining async function calls used to look like this
Using Promises
requestFromServer()
.then(function (res) { return res2;})
.then(function (res2) { return res3;})
// ...
.then(function (res3) { return res4;})
.catch(function (err) { …act…;});
once an error is thrown, it is passed through the chain until it
meets the first catch
Flattening
requestFromServer()
.then(function (res) {
return readFile(res);
}).then(function (file) {
// do something with file
});
if a thenable object is returned inside a then() block, the
param to the next then will already be the result of that object
notice the second then gets a file and not the promise object
returned by readFile(res)
Creating Promises
new Promise(function (resolve, reject) {
if (testSomething()) {
resolve(resolutionValue);
} else {
reject(errorValue);
}
});
pending
creation
resolve
reject
fulfilled
rejected
settledpending
State of a Promise
once a promise has been settled, all calls to resolve() or
reject() will have no effect
Promise.all()
Promise.all(
['1', 'abcde',readFile('notes.txt')])

.then(function (res) {
console.log(res);
});
// Array [ "1", "abcde", "file content" ]
read more (good stuff): 2ality
Promise.all() resolves when all operations and values passed
are resolved.
Destructuring
The Basics
You're used to structuring source objects in ES5
let a = {x:1, y:2};
let {x:x} = a; // = {x:1, y:2}
console.log(x); // 1
in ES6, you can destructure the target
notice we only took x’s value
The Basics
remember: param key is the source, value is the target.
let a = {x:1, y:2};
let {x:x, y:z} = a;
console.log('x=' + x); // 1
console.log('z=' + z); // 2
also, the shorthand for {x:x} is {x}
let a = {x:1, y:2};
let {y} = a;
console.log('y=' + y); // 2
The Basics - Objects
nesting works
let a = {x: { y: 3 }};
let {x: { y: z}} = a;
console.log('z=' + z); // 3
any object property can be read:
let {length: len} = [1,2,3,4];
console.log(len); // 4
primitives are coerced to objects
let {length: len} = ‘abcd‘;
console.log(len); // 4
The Basics - Arrays
with arrays it’s similar
let [a, [b]] = [3, [5]];
console.log(b); // 5
destructuring to array will iterate on the source
function* genInf() {
for (let i =0;;i++) yield i;
}
let [a,b,c] = genInf();
// a === 0, b === 1, c === 2
The Basics - Arrays
another example with regular expressions
let [all, prefix, host] =
/(http[s]?)://([^/]*)/
.exec('http://www.wix.com/my-
account');
console.log(prefix); // rest
console.log(host); // www.wix.com
Default Values
default value is used if match on src is undefined (either
missing or actual value)
let [a,b = 3, c = 7] = [1, undefined];
// a === 1, b === 3, c === 7
works on both arrays and objects
let {x:x = 1 ,y:y = 2,z:z = 3} =
{ x: undefined, z: 7};
// x === 1, y === 2, z === 7
Default Values
default values are lazily evaluated
let [x = con()] = [5];
// x === 5
let [x = con()] = [];
// TEST
// x === 10
function con() {
console.log('TEST');
return 10;
}
Default Values
default values evaluation is equivalent to a list of let
declarations
let [x = 7, y = x] = [];
// x === 7, y === 7
this works
let [x = y, y = 0] = [];
// ReferenceError
this fails
Rest Operator
rest (…) operator allows to extract remaining values into array
let [,, ...y] = [1,2,3,4,5];
console.log(y); // Array [3, 4, 5]
notice we omitted value to skip them. (Elision)
Destructuring Parameters
function parameters are destructured
let reverse = ([x, ...y]) =>
(y.length > 0)?[...reverse(y), x]:[x];
the equivalent to the parameter destructuring we did is:
let [x, ...y] = [1,2,3,4,5,6];
reverse([1,2,3,4,5,6]);
// [6,5,4,3,2,1]
notice we used the spread (…) operator which flattens an
array
Spread
flatten arrays
[1, 2, ...[3, 4]] // Array [1, 2, 3, 4]
Math.max(-5, ...[2, 5, 4], 1); // 5
use arrays as function parameters
Spread
spread iterables into array or params
function* squares(n) {
for (let i = 1; i < n; i += 1) {
yield Math.pow(i,2);
}
}
[...squares(10)];
// [1, 4, 9, 16, 25, 36, 49, 64, 81]
reminder: generators are iterable
Modules
Modules
Modules allow you to load code on demand (async) and to
provide a level of abstraction
//***** Shape.js ******
export default class Shape {
getColor() { /*...*/ }
}
this method exports a single default value for the module
//***** main.js ******
import Shape from 'Shape';
let shape = new Shape();
Modules
you can also export by name
//***** Circle.js ******
export function diameter(r) {
return 2 * Math.PI * r;
}
export let area = r =>
Math.pow(Math.PI*r, 2);
Modules
and then import by name
//***** main.js ******
import {area as circleArea, diameter}

from 'Circle';
import area as cubeArea from 'Cube';
import * as tri from 'Triangle';


circleArea(5);
cubeArea(4);
tri.area(3);
Modules
• modules structure is statically defined.
• dependencies can be resolved statically.
• you can still load modules programatically if you choose
System.import('module')
.then(module => {})
.catch(error => {});
Proxy
Proxy
Proxy is an object that allows you to trap an operation and
manipulate it.
Handler
Target
proxy
Proxy
for example, here we trap the function application
let target = function () {};
let handler = {
apply(target, thisArg, argsList) {
return 42;
}
}
let p = new Proxy(target, handler);
console.log(p()); // 42
Proxy
and here we trap a get operation on an object property
let target = { a: 7 };
let handler = {
get(target, name) {
return target[name] + 10;
}
}
let p = new Proxy(target, handler);
console.log(p.a); // 17
console.log(p.b); // NaN
the end
whaddya wanna know?
98

More Related Content

What's hot

TypeScript Introduction
TypeScript IntroductionTypeScript Introduction
TypeScript IntroductionDmitry Sheiko
 
Unit Testing in Angular
Unit Testing in AngularUnit Testing in Angular
Unit Testing in AngularKnoldus Inc.
 
Understanding react hooks
Understanding react hooksUnderstanding react hooks
Understanding react hooksSamundra khatri
 
Introduction to AngularJS
Introduction to AngularJSIntroduction to AngularJS
Introduction to AngularJSDavid Parsons
 
TypeScript Best Practices
TypeScript Best PracticesTypeScript Best Practices
TypeScript Best Practicesfelixbillon
 
How to implement internationalization (i18n) in angular application(multiple ...
How to implement internationalization (i18n) in angular application(multiple ...How to implement internationalization (i18n) in angular application(multiple ...
How to implement internationalization (i18n) in angular application(multiple ...Katy Slemon
 
Angular and The Case for RxJS
Angular and The Case for RxJSAngular and The Case for RxJS
Angular and The Case for RxJSSandi Barr
 
Javascript arrays
Javascript arraysJavascript arrays
Javascript arraysHassan Dar
 
Typescript in React: HOW & WHY?
Typescript in React: HOW & WHY?Typescript in React: HOW & WHY?
Typescript in React: HOW & WHY?Saulius Skeirys
 
Asynchronous JavaScript Programming with Callbacks & Promises
Asynchronous JavaScript Programming with Callbacks & PromisesAsynchronous JavaScript Programming with Callbacks & Promises
Asynchronous JavaScript Programming with Callbacks & PromisesHùng Nguyễn Huy
 
ES6 presentation
ES6 presentationES6 presentation
ES6 presentationritika1
 

What's hot (20)

TypeScript Introduction
TypeScript IntroductionTypeScript Introduction
TypeScript Introduction
 
Loops in java script
Loops in java scriptLoops in java script
Loops in java script
 
Unit Testing in Angular
Unit Testing in AngularUnit Testing in Angular
Unit Testing in Angular
 
Understanding react hooks
Understanding react hooksUnderstanding react hooks
Understanding react hooks
 
Introduction to AngularJS
Introduction to AngularJSIntroduction to AngularJS
Introduction to AngularJS
 
TypeScript Best Practices
TypeScript Best PracticesTypeScript Best Practices
TypeScript Best Practices
 
Java Unit Testing
Java Unit TestingJava Unit Testing
Java Unit Testing
 
How to implement internationalization (i18n) in angular application(multiple ...
How to implement internationalization (i18n) in angular application(multiple ...How to implement internationalization (i18n) in angular application(multiple ...
How to implement internationalization (i18n) in angular application(multiple ...
 
Angular Observables & RxJS Introduction
Angular Observables & RxJS IntroductionAngular Observables & RxJS Introduction
Angular Observables & RxJS Introduction
 
Angular and The Case for RxJS
Angular and The Case for RxJSAngular and The Case for RxJS
Angular and The Case for RxJS
 
Javascript arrays
Javascript arraysJavascript arrays
Javascript arrays
 
TypeScript - An Introduction
TypeScript - An IntroductionTypeScript - An Introduction
TypeScript - An Introduction
 
JS Event Loop
JS Event LoopJS Event Loop
JS Event Loop
 
Puppeteer
PuppeteerPuppeteer
Puppeteer
 
Introduction To C#
Introduction To C#Introduction To C#
Introduction To C#
 
Typescript in React: HOW & WHY?
Typescript in React: HOW & WHY?Typescript in React: HOW & WHY?
Typescript in React: HOW & WHY?
 
Asynchronous JavaScript Programming with Callbacks & Promises
Asynchronous JavaScript Programming with Callbacks & PromisesAsynchronous JavaScript Programming with Callbacks & Promises
Asynchronous JavaScript Programming with Callbacks & Promises
 
Brief History of JavaScript
Brief History of JavaScriptBrief History of JavaScript
Brief History of JavaScript
 
ES6 presentation
ES6 presentationES6 presentation
ES6 presentation
 
React hooks
React hooksReact hooks
React hooks
 

Similar to ES2015 (ES6) Overview

ES6 - Next Generation Javascript
ES6 - Next Generation JavascriptES6 - Next Generation Javascript
ES6 - Next Generation JavascriptRamesh Nair
 
Game Design and Development Workshop Day 1
Game Design and Development Workshop Day 1Game Design and Development Workshop Day 1
Game Design and Development Workshop Day 1Troy Miles
 
ECMAScript2015
ECMAScript2015ECMAScript2015
ECMAScript2015qmmr
 
Explaining ES6: JavaScript History and What is to Come
Explaining ES6: JavaScript History and What is to ComeExplaining ES6: JavaScript History and What is to Come
Explaining ES6: JavaScript History and What is to ComeCory Forsyth
 
FalsyValues. Dmitry Soshnikov - ECMAScript 6
FalsyValues. Dmitry Soshnikov - ECMAScript 6FalsyValues. Dmitry Soshnikov - ECMAScript 6
FalsyValues. Dmitry Soshnikov - ECMAScript 6Dmitry Soshnikov
 
JavaScript - new features in ECMAScript 6
JavaScript - new features in ECMAScript 6JavaScript - new features in ECMAScript 6
JavaScript - new features in ECMAScript 6Solution4Future
 
Idiomatic Javascript (ES5 to ES2015+)
Idiomatic Javascript (ES5 to ES2015+)Idiomatic Javascript (ES5 to ES2015+)
Idiomatic Javascript (ES5 to ES2015+)David Atchley
 
What can be done with Java, but should better be done with Erlang (@pavlobaron)
What can be done with Java, but should better be done with Erlang (@pavlobaron)What can be done with Java, but should better be done with Erlang (@pavlobaron)
What can be done with Java, but should better be done with Erlang (@pavlobaron)Pavlo Baron
 
ECMAScript 6 and beyond
ECMAScript 6 and beyondECMAScript 6 and beyond
ECMAScript 6 and beyondFrancis Johny
 
Javascript Basics
Javascript BasicsJavascript Basics
Javascript Basicsmsemenistyi
 
TypeScript Introduction
TypeScript IntroductionTypeScript Introduction
TypeScript IntroductionHans Höchtl
 
JavaScript Editions ES7, ES8 and ES9 vs V8
JavaScript Editions ES7, ES8 and ES9 vs V8JavaScript Editions ES7, ES8 and ES9 vs V8
JavaScript Editions ES7, ES8 and ES9 vs V8Rafael Casuso Romate
 
EcmaScript 6 - The future is here
EcmaScript 6 - The future is hereEcmaScript 6 - The future is here
EcmaScript 6 - The future is hereSebastiano Armeli
 

Similar to ES2015 (ES6) Overview (20)

ES6: Features + Rails
ES6: Features + RailsES6: Features + Rails
ES6: Features + Rails
 
ES6 - Next Generation Javascript
ES6 - Next Generation JavascriptES6 - Next Generation Javascript
ES6 - Next Generation Javascript
 
Game Design and Development Workshop Day 1
Game Design and Development Workshop Day 1Game Design and Development Workshop Day 1
Game Design and Development Workshop Day 1
 
ECMAScript2015
ECMAScript2015ECMAScript2015
ECMAScript2015
 
"let ECMAScript = 6"
"let ECMAScript = 6" "let ECMAScript = 6"
"let ECMAScript = 6"
 
Explaining ES6: JavaScript History and What is to Come
Explaining ES6: JavaScript History and What is to ComeExplaining ES6: JavaScript History and What is to Come
Explaining ES6: JavaScript History and What is to Come
 
Es6 to es5
Es6 to es5Es6 to es5
Es6 to es5
 
Modern JS with ES6
Modern JS with ES6Modern JS with ES6
Modern JS with ES6
 
Workshop 10: ECMAScript 6
Workshop 10: ECMAScript 6Workshop 10: ECMAScript 6
Workshop 10: ECMAScript 6
 
FalsyValues. Dmitry Soshnikov - ECMAScript 6
FalsyValues. Dmitry Soshnikov - ECMAScript 6FalsyValues. Dmitry Soshnikov - ECMAScript 6
FalsyValues. Dmitry Soshnikov - ECMAScript 6
 
JavaScript - new features in ECMAScript 6
JavaScript - new features in ECMAScript 6JavaScript - new features in ECMAScript 6
JavaScript - new features in ECMAScript 6
 
Idiomatic Javascript (ES5 to ES2015+)
Idiomatic Javascript (ES5 to ES2015+)Idiomatic Javascript (ES5 to ES2015+)
Idiomatic Javascript (ES5 to ES2015+)
 
What can be done with Java, but should better be done with Erlang (@pavlobaron)
What can be done with Java, but should better be done with Erlang (@pavlobaron)What can be done with Java, but should better be done with Erlang (@pavlobaron)
What can be done with Java, but should better be done with Erlang (@pavlobaron)
 
ECMAScript 6 and beyond
ECMAScript 6 and beyondECMAScript 6 and beyond
ECMAScript 6 and beyond
 
Javascript Basics
Javascript BasicsJavascript Basics
Javascript Basics
 
TypeScript Introduction
TypeScript IntroductionTypeScript Introduction
TypeScript Introduction
 
JavaScript Editions ES7, ES8 and ES9 vs V8
JavaScript Editions ES7, ES8 and ES9 vs V8JavaScript Editions ES7, ES8 and ES9 vs V8
JavaScript Editions ES7, ES8 and ES9 vs V8
 
Internal workshop es6_2015
Internal workshop es6_2015Internal workshop es6_2015
Internal workshop es6_2015
 
ECMAScript 2015
ECMAScript 2015ECMAScript 2015
ECMAScript 2015
 
EcmaScript 6 - The future is here
EcmaScript 6 - The future is hereEcmaScript 6 - The future is here
EcmaScript 6 - The future is here
 

Recently uploaded

Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)jennyeacort
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceBrainSell Technologies
 
A healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfA healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfMarharyta Nedzelska
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odishasmiwainfosol
 
How to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationHow to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationBradBedford3
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Mater
 
PREDICTING RIVER WATER QUALITY ppt presentation
PREDICTING  RIVER  WATER QUALITY  ppt presentationPREDICTING  RIVER  WATER QUALITY  ppt presentation
PREDICTING RIVER WATER QUALITY ppt presentationvaddepallysandeep122
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...Technogeeks
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作qr0udbr0
 
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Cizo Technology Services
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfAlina Yurenko
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024StefanoLambiase
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsAhmed Mohamed
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaHanief Utama
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 

Recently uploaded (20)

Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. Salesforce
 
A healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfA healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdf
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
 
How to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationHow to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion Application
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)
 
PREDICTING RIVER WATER QUALITY ppt presentation
PREDICTING  RIVER  WATER QUALITY  ppt presentationPREDICTING  RIVER  WATER QUALITY  ppt presentation
PREDICTING RIVER WATER QUALITY ppt presentation
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...
 
Advantages of Odoo ERP 17 for Your Business
Advantages of Odoo ERP 17 for Your BusinessAdvantages of Odoo ERP 17 for Your Business
Advantages of Odoo ERP 17 for Your Business
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作
 
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML Diagrams
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief Utama
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 

ES2015 (ES6) Overview

  • 1. ECMAScript 2015 Were you expecting something else?
  • 3. A History Lesson 1995 May - Mocha is invented in Netscape by Brendan Eich. September - Renamed to LiveScript December - Renamed to Javascript (Because Java was popular)
  • 4. A History Lesson 1996 - JavaScript is taken to standardization in ECMA. From now on ECMA is the spec. Javascript is an implementation (ActionScript is another implementation) 1997 - ECMA-262 (ECMAScript) 1998 - ECMAScript 2 1999 - ECMAScript 3 1996
  • 5. A History Lesson 2005 - Mozilla and Macromedia started Work on ECMAScript 4. Feature rich and a very major leap from ECMAScript 3. Douglas Crockford (Yahoo) And Microsoft opposed the forming standard. ECMAScript 3.1 was the compromise. 2005
  • 6. A History Lesson 2009 - Opposing parties meet in Oslo and achieve an agreement. ES3.1 is renamed to ES5 (Which we have today) In the spirit of peace and agreement, the new Javascript long term agenda is named “Harmony” 2009
  • 7. A History Lesson 2015 - ES6 (Part of the “Harmony” umbrella) will be released. Starting with ES6 - Version names will be based on the year of release. so ES6 is ES2015 and ES7 should be ES2016 2015
  • 9. let - A New Hope Scope if (true) { var x = 3; } console.log(x); // 3 var is function scope let is block scope if (true) { let x = 3; } console.log(x); // ReferenceError
  • 10. To hoist is to raise. Function declarations are hoisted
 which means you can do this with functions and vars: function f() { return 3; } A word about Hoisting But you can’t do it with classes or let variables class X{} f(); //3 new X(); // TypeError
  • 11. Temporal dead zone let variable declarations are not hoisted console.log(x); // ReferenceError let x = 3; console.log(x); // 3 The time between block start and declaration is called
 “Temporal Dead Zone”. Same with ‘const’
  • 12. Temporal dead zone TDZ is based on time and not on location function block() { function foo() { console.log(x); } foo(); // ReferenceError let x = 3; foo(); // 3 }
  • 13. Loop scoping With let You get a fresh iteration per loop let vals = []; for (let x = 0; x < 4; x += 1) { vals.push(() => x); } console.log(vals.map(x => x())); // [0, 1, 2, 3] * we’re using functions to store by reference
 otherwise it won’t prove the point. Full Example here With var You would get [4, 4, 4, 4] as a result
  • 14. const const makes variables immutable const obj = { par: 3 }; obj = 4; // TypeError but it doesn't stop you from changing the object values obj.par = 12; // Fine you can use Object.freeze() to achieve that. Object.freeze(obj); obj.par = 10; // TypeError console.seal(obj); // 17 obj.newParam = 3 // TypeError or the Object.seal() which blocks changing object structure
  • 16. String Templates This is syntactic sugar for string concatenation. let name = 'John'; `Hi ${name}, Did you know that 5 * 3 = ${5*3}?` /* "Hi John, Did you know that 5 * 3 = 15?" */ Anything in ${} is evaluated Back-ticks are used to enclose the string. New lines are retained
  • 18. * basically just shortening {x:x} to {x} Shorthand assignment let x = 3; let xo = {x}; // xo = { x: 3 } let x = 3; let xo = {x:x}; // xo = { x: 3 } equivalent to:
  • 19. * a new syntax for defining methods inside an object literal Method Definition let foo = { f(x) { return x + 1; } }; foo.f(3); // 4
  • 20. you can make super calls for inherited methods Super let foo = { toString() { super.toString() + ' with foo!'; } }; in class constructors you can simply call super() to
 call on the parent constructor foo.toString(); //[object Object] with foo!
  • 21. * you can define keys that evaluate on run time inside literals Computed Properties let ind = 100; let foo = { ['question'+ ind]: 'question number ' + ind, ['answer' + ind](x) { return ind === x; } }; foo.question100; // “question number 100” foo.answer100(100); // true
  • 23. Symbol a symbol is immutable and unique let s1 = Symbol('test'); let s2 = Symbol('test');
 s1 === s2; // false the string parameter is for console and debug.
  • 24. Symbol let o = { [Symbol('hellllo')]: 7, 'str': 6 }; JSON.stringify(o); // "{"str":6}" Object.getOwnPropertyNames(o); // Array [ "str" ] Object.getOwnPropertySymbols(o); // Array [ Symbol(hellllo) ] since Symbols are hidden, Object allows reflection: symbol keys are not visible
  • 25. Symbol symbols are now used by javascript, internally, with certain features. for example: Symbol.iterator
 used to provide iterator function Symbol.hasInstance
 used internally for instanceof Symbol.toPrimitive
 used internally to convert to primitives
  • 27. Class class Jedi { constructor() { this.forceIsDark = false; } toString() { return (this.forceIsDark ? 'Join' : 'Fear is the path to' ) + ' the dark side'; } } this is a class you cannot define a property inside a class, but getters/ setters can create one to the outside)
  • 28. Extends class Sith extends Jedi { constructor() { super(); this.forceIsDark = true; } } extends works as you’d expect super() in a ctor calls the parent ctor
  • 29. Class let yoda = new Jedi(); let darth = new Sith(); console.log(yoda.toString()); // Fear is the path to the dark side console.log(darth.toString()); // Join the dark side console.log(darth instanceof Sith); // true console.log(darth instanceof Jedi); // true
  • 30. Static you can declare static functions class Util { static inc(x) { return x + 1 }, static dec(x) { return x - 1 } } console.log(Util.inc(3)); // 4 it’s the same as placing them on the prototype object
  • 31. Class Get/Set you can define get/set just like in ES5 object literals class O { constructor() { this.mx = 'initial'; } get x() { return this.mx; } set x(val) { console.log('x changed'); this.mx = val; } }
  • 32. Class Get/Set and to the user of the class it would appear like a property let o = new O(); console.log(o.x); //initial o.x = 'newval'; // x changed console.log(o.x); //newval
  • 33. ES6 classes are not hoisted var a = new ANumber(3); //ReferenceError Class Hoisting class ANumber { constructor(n) { this.x = n; } }
  • 35. The Useless Loop * Iterating over an array using (for…in) (ES5) // '0' // '1' // '2' var arr = ['a', 'b', 'c']; for (var i in arr) { if (arr.hasOwnProperty(i)) { console.log(i); } }
  • 36. * ES6 bring the (for…of) iterator that works as you’d expect for (let n of ['a', 'b', 'c']) { console.log(n); } The Iterator protocol // 'a' // 'b' // 'c'
  • 37. * other syntaxes that expect iterables: // Spread Operator
 [..."abcd"]; 
 // Array ["a", "b", "c", “d"] // Destructure Assignment [a, b] = "xy"; b; // y The Iterator protocol Array.from("123"); // Array [ "1", "2", "3" ]
  • 38. The Iterator protocol * manually iterating over an iterable object let it = [1, 2, 3][Symbol.iterator]();
 it.next(); // { value: 1, done: false }
 it.next(); // { value: 2, done: false }
 it.next(); // { value: 3, done: false } it.next(); // { value: undefined, done: true }
  • 39. The Iterator protocol * Arrays Maps (Not WeakMaps) Dom queries Strings Sets (Not WeakSets) Arguments Iterable data sources
  • 40. The Iterator protocol * Anything can be a source for iteration: function gen(n) { return { [Symbol.iterator]() { let i = 0; return { next() { return { done: (i > n) ? true : false, value: i++ }; } }; } }; } ITERATOR ITERABLE
  • 41. for (let n of gen(10) { console.log(n); } //1 //2 //... //10 The Iterator protocol Anything can be a source for iteration: generators are a great way to create iterables. We’ll get to that later
  • 43. String "Hello".startsWith('Hell'); "Goodbye".endsWith('bye'); "Jar".repeat(2); // JarJar "abcdef".includes("bcd"); String got some new functions Firefox still uses contains instead of includes. 
 That should change
  • 44. Number New Number constants and methods Number.EPSILON
 Number.MAX_SAFE_INTEGER
 Number.MIN_SAFE_INTEGER Number.isNaN() Number.isFinite() Number.isInteger() Number.isSafeInteger() Number.parseFloat() Number.parseInt()
  • 45. Array.from() let arrayLike = { 0: 'zero', 1: 'one', 2: 'two', 3: 'three', 'length' : 4 }; from array-like objects (objects that have indices and length) Array.from(arrayLike); // Array ["zero", "one", "two", "three"]
  • 46. Reminder… * genTen is a custom iterable that generates 0 to 10 function [Symbol.iterator]() { done: (i > n) ? value: i++ }; } }; } }; }
  • 47. Array.from() from iterables Array.from(gen(6)); // Array [ 0, 1, 2, 3, 4, 5, 6 ] Array.from(gen(6), x => x*x); //Array [ 0, 1, 4, 9, 16, 25, 36 ] second parameter is a mapping function
  • 48. Array.of() you should use Array.of over Array constructor because: new Array(2.3); //RangeError: invalid array length Array.of(1, 2, 4, 3, 4); //Array [ 1, 2, 4, 3, 4 ] a better way to create arrays
  • 49. Notice how keys() and entries() return an iterator and
 not an array Array.prototype.* ['a','b','c'].keys() // Array Iterator { } [...['a','b','c'].keys()] // Array [ 0, 1, 2 ] Array.prototype.keys() Array.from(['a','b','c'].entries()) // [[0,"a"],[1,"b"],[2,"c"]] Array.prototype.entries()
  • 50. Array.prototype.* Array.prototype.find() [4, 100, 7].find(x => x > 5); // 100 [4, 100, 7].findIndex(x => x > 5); // 1 Array.prototype.findIndex() Array.prototype.fill() (new Array(7)).fill(2).fill(3, 2, 5); // Array [ 2, 2, 3, 3, 3, 2, 2 ]
  • 51. Object Object.assign() let x = {a:1}; Object.assign(x, {b:2}); x; // {a:1, b:2} object.is() checks if two values are the same Object.is('y', 'y'); // true Object.is({x:1}, {x:1}); // false Object.is(NaN, NaN); // true different than === in (+0 !== -0) and (NaN === NaN) also - doesn’t coerce values (as == does)
  • 53. Map ES5 objects are not maps. ES6 introduces real maps construction var m = new Map([ [1, ‘first'] ]); m; // Map { 1: "first"}
  • 54. Map ES5 objects are not maps. ES6 introduces real maps objects can be keys var m = new Map([ [1, 'first'], [{}, ‘second'] ]); m; // Map { 1: "first", Object: "second"}
  • 55. Map ES5 objects are not maps. ES6 introduces real maps functions can be key var m = new Map([ [1, 'first'], [{}, 'second'] ]); m.set(x => x+1, ‘third'); // Map { 1: "first", Object: "second", function (): "third"}
  • 56. Map ES5 objects are not maps. ES6 introduces real maps key equality is === (+NaN is equal to each other) var m = new Map([ [1, 'first'], [{}, 'second'] ]); m.set(x => x+1, 'third') .set({}, 'fourth'); // Map { 1: "first", Object: "second", function (): "third", Object: "fourth" } notice how using {} twice creates 2 different keys
  • 57. Map var key = {};
 var m = new Map([
 [key, 'first'], [1, 'second']
 ]); 
 m.get(key); // 'first' m.has({}); // false m.delete(key); // true m.size; // 1 m.clear(); m.size; // 0 m.forEach((val, key) => { … }); let’s go over some basic Map api
  • 58. Mapthere are several ways to iterate over a map let m =
 new Map([...'abcd'].map(x=>[x, x + x])); JSON.stringify([...m]); // "[["a","aa"],["b","bb"],["c","cc"], ["d","dd"]]" JSON.stringify([...m.keys()]); // "["a","b","c","d"]" JSON.stringify([...m.values()]); // "["aa","bb","cc","dd"]" JSON.stringify([...m.entries()]); // "[["a","aa"],["b","bb"],["c","cc"], ["d","dd"]]"
  • 59. WeakMap WeakMap is a Map which doesn’t prevent garbage collection • you can’t iterate over it • no clear() method • doesn’t have size property • keys must be objects WeakMap is good for hiding private class members
  • 60. Set Set is a collection of distinct objects let s = new Set(['red', 'blue']); s.add(‘yellow’); // adds yellow s.add(‘red'); // doesn’t add red s.has('blue'); // true s.delete('blue'); // true s.size; // 2 s; // Set ["red", "blue"] [...s]; // Array [ "red", "blue" ] s.clear();
  • 61. Set for the sake of symmetry with maps, this works: JSON.stringify([...s.entries()]); // "[["red","red"],["blue","blue"]]" JSON.stringify([...s.values()]); // "["red","blue"]" sets are iterable JSON.stringify([...s.keys()]); // "["red","blue"]"
  • 62. WeakSets WeakSet exists but it doesn’t allow iteration. so you can’t do a whole lot with it. 
 You can mark objects (check if in set or not)
  • 64. function* function* genFour() { yield 1; yield 2; yield 3; return 4; } let four = genFour(); the function suspends on every yield and allows other code to run until the next time it resumes
  • 65. Consuming four.next(); // Object { value: 1, done: false } four.next(); // Object { value: 4, done: true } four.next(); // Object { value: 2, done: false } four.next(); // Object { value: 3, done: false } generators are both an iterator and an iterable as an iterator:
  • 66. Consuming generators are both an iterator and an iterable [...genFour()]; // Array [ 1, 2, 3, 4 ] Array.from([...genFour()]); // Array [ 1, 2, 3, 4 ] as an iterable:
  • 67. three.next(); yield 1; // {value:1, done:false} three.next(); yield 2; // {value:2, done:false} three.next(); yield 3; // {value:3, done:false} three.next(); return 4; // {value:4, done:true} three.next(); // Object { value: undefined, done: true } Here is an example consuming an iterator step by step
  • 68. yield* yield* yields every yield inside a called generator function* flatten(arr) { for (let x of arr) { if (x instanceof Array) { yield* flatten(x); } else { yield x; } } } let t = flatten([1, 2, [3, 4]]); // Array [1, 2, 3, 4]
  • 70. function inc(x) { return x + 1; } let inc = x => x + 1; is equivalent to: 2 parameters: let inc = (x, y) => x + y; no parameters let inc = () => 7; The Basics
  • 71. function inc(x) { return x + 1; } The Basics more than 1 statement let inc = (x) => { console.log(x); return 7; }
  • 72. Lexical this Arrow functions capture the this value of the enclosing context. In other words, no more `that`, `self` or `bind(this)` // wait for it.... // 7 this.x = 7; setTimeout(() => console.log(this.x), 2000);
  • 74. Incentive requestFromServer(function (err, res) { if (err) { // do something with error } else { // do something with result doSomethingAsync(function () { andAnotherAsyncThing(function () { andAnotherAsyncThing(function () { andAnotherAsyncThing(function () { andAnotherAsyncThing(function () { andAnotherAsyncThing(function () { andAnotherAsyncThing(function () { // CALLBACK PYRAMID OF DOOM!!! }); }); }); }); }); }); }); } }); Chaining async function calls used to look like this
  • 75. Using Promises requestFromServer() .then(function (res) { return res2;}) .then(function (res2) { return res3;}) // ... .then(function (res3) { return res4;}) .catch(function (err) { …act…;}); once an error is thrown, it is passed through the chain until it meets the first catch
  • 76. Flattening requestFromServer() .then(function (res) { return readFile(res); }).then(function (file) { // do something with file }); if a thenable object is returned inside a then() block, the param to the next then will already be the result of that object notice the second then gets a file and not the promise object returned by readFile(res)
  • 77. Creating Promises new Promise(function (resolve, reject) { if (testSomething()) { resolve(resolutionValue); } else { reject(errorValue); } });
  • 78. pending creation resolve reject fulfilled rejected settledpending State of a Promise once a promise has been settled, all calls to resolve() or reject() will have no effect
  • 79. Promise.all() Promise.all( ['1', 'abcde',readFile('notes.txt')])
 .then(function (res) { console.log(res); }); // Array [ "1", "abcde", "file content" ] read more (good stuff): 2ality Promise.all() resolves when all operations and values passed are resolved.
  • 81. The Basics You're used to structuring source objects in ES5 let a = {x:1, y:2}; let {x:x} = a; // = {x:1, y:2} console.log(x); // 1 in ES6, you can destructure the target notice we only took x’s value
  • 82. The Basics remember: param key is the source, value is the target. let a = {x:1, y:2}; let {x:x, y:z} = a; console.log('x=' + x); // 1 console.log('z=' + z); // 2 also, the shorthand for {x:x} is {x} let a = {x:1, y:2}; let {y} = a; console.log('y=' + y); // 2
  • 83. The Basics - Objects nesting works let a = {x: { y: 3 }}; let {x: { y: z}} = a; console.log('z=' + z); // 3 any object property can be read: let {length: len} = [1,2,3,4]; console.log(len); // 4 primitives are coerced to objects let {length: len} = ‘abcd‘; console.log(len); // 4
  • 84. The Basics - Arrays with arrays it’s similar let [a, [b]] = [3, [5]]; console.log(b); // 5 destructuring to array will iterate on the source function* genInf() { for (let i =0;;i++) yield i; } let [a,b,c] = genInf(); // a === 0, b === 1, c === 2
  • 85. The Basics - Arrays another example with regular expressions let [all, prefix, host] = /(http[s]?)://([^/]*)/ .exec('http://www.wix.com/my- account'); console.log(prefix); // rest console.log(host); // www.wix.com
  • 86. Default Values default value is used if match on src is undefined (either missing or actual value) let [a,b = 3, c = 7] = [1, undefined]; // a === 1, b === 3, c === 7 works on both arrays and objects let {x:x = 1 ,y:y = 2,z:z = 3} = { x: undefined, z: 7}; // x === 1, y === 2, z === 7
  • 87. Default Values default values are lazily evaluated let [x = con()] = [5]; // x === 5 let [x = con()] = []; // TEST // x === 10 function con() { console.log('TEST'); return 10; }
  • 88. Default Values default values evaluation is equivalent to a list of let declarations let [x = 7, y = x] = []; // x === 7, y === 7 this works let [x = y, y = 0] = []; // ReferenceError this fails
  • 89. Rest Operator rest (…) operator allows to extract remaining values into array let [,, ...y] = [1,2,3,4,5]; console.log(y); // Array [3, 4, 5] notice we omitted value to skip them. (Elision)
  • 90. Destructuring Parameters function parameters are destructured let reverse = ([x, ...y]) => (y.length > 0)?[...reverse(y), x]:[x]; the equivalent to the parameter destructuring we did is: let [x, ...y] = [1,2,3,4,5,6]; reverse([1,2,3,4,5,6]); // [6,5,4,3,2,1] notice we used the spread (…) operator which flattens an array
  • 91. Spread flatten arrays [1, 2, ...[3, 4]] // Array [1, 2, 3, 4] Math.max(-5, ...[2, 5, 4], 1); // 5 use arrays as function parameters
  • 92. Spread spread iterables into array or params function* squares(n) { for (let i = 1; i < n; i += 1) { yield Math.pow(i,2); } } [...squares(10)]; // [1, 4, 9, 16, 25, 36, 49, 64, 81] reminder: generators are iterable
  • 94. Modules Modules allow you to load code on demand (async) and to provide a level of abstraction //***** Shape.js ****** export default class Shape { getColor() { /*...*/ } } this method exports a single default value for the module //***** main.js ****** import Shape from 'Shape'; let shape = new Shape();
  • 95. Modules you can also export by name //***** Circle.js ****** export function diameter(r) { return 2 * Math.PI * r; } export let area = r => Math.pow(Math.PI*r, 2);
  • 96. Modules and then import by name //***** main.js ****** import {area as circleArea, diameter}
 from 'Circle'; import area as cubeArea from 'Cube'; import * as tri from 'Triangle'; 
 circleArea(5); cubeArea(4); tri.area(3);
  • 97. Modules • modules structure is statically defined. • dependencies can be resolved statically. • you can still load modules programatically if you choose System.import('module') .then(module => {}) .catch(error => {});
  • 98. Proxy
  • 99. Proxy Proxy is an object that allows you to trap an operation and manipulate it. Handler Target proxy
  • 100. Proxy for example, here we trap the function application let target = function () {}; let handler = { apply(target, thisArg, argsList) { return 42; } } let p = new Proxy(target, handler); console.log(p()); // 42
  • 101. Proxy and here we trap a get operation on an object property let target = { a: 7 }; let handler = { get(target, name) { return target[name] + 10; } } let p = new Proxy(target, handler); console.log(p.a); // 17 console.log(p.b); // NaN