SlideShare a Scribd company logo
EcmaScript 2015impress your friends at the party
What is es6?
Why es6?
Is it Safe to Us Es6?!?!
https://kangax.github.io/compat-table/es6/
How do we use Es6?
Impress Your Friends with EcmaScript 2015
Impress Your Friends with EcmaScript 2015
https://github.com/rauschma/webpack-es6-demo
https://github.com/angular-class/NG6-starter
Syntactic SugAr
Impress Your Friends with EcmaScript 2015
Es6 Classes
Impress Your Friends with EcmaScript 2015
ES6 Classes
• Simple sugar over the the prototype-based
OO pattern

• A more convenient form makes classes
easier to use 

• Classes support inheritance, super calls,
instance and static methods and
constructors
class Project {

constructor(name) {

this.name = name;

}



start() {

return `Project ${this.name} starting`;

}

}



var project = new Project("Website");

project.start(); // "Project Website starting"
Es6 Inheritance
ES6 INheritance
• Inheritance is possible via the prototype

• You can inherit from a base class using the
extends keyword

• You must call super before you can access
this in a subclass
class Shape {

…

toString () {

return `Shape(${this.id})`

}

}

class Rectangle extends Shape {

constructor (id, x, y, width, height) {

super(id, x, y)

…

}

toString () {

return "Rectangle > " + super.toString()

}

}

class Circle extends Shape {

constructor (id, x, y, radius) {

super(id, x, y)

…

}

toString () {

return "Circle > " + super.toString()

}

}
Es6 Modules
ES6 MODULES
• Language-level support for modules for
component definition

• Best of both both CommonJS and AMD 

• Named exports and default exports
//------ 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
//------ main.js ------

import * as lib from 'lib';

console.log(lib.square(11)); // 121

console.log(lib.diag(4, 3)); // 5
//------ myFunc.js ------

export default function () { ... };



//------ main1.js ------

import myFunc from 'myFunc';

myFunc();
//------ MyClass.js ------

export default class { ... };



//------ main2.js ------

import MyClass from 'MyClass';

let inst = new MyClass();
Es6 Block Level Scoping
Impress Your Friends with EcmaScript 2015
ES6 MODULES
• Function scope can be tricky!

• We can scope variables at a block level
using let and const
function printName() {

if(true) {

var name = "Rafael";

}

console.log(name); // Rafael

}
function printName() {

var name; // variable declaration is hoisted to the top

if(true) {

name = "Rafael";

}

console.log(name); // Rafael

}
function printName() {

if(true) {

let name = "Rafael";

}

console.log(name); // ReferenceError: name is not defined

}
function printName() {

var name = "Hey";

if(true) {

let name = "Rafael";

console.log(name); // Rafael

}

console.log(name); // Hey

}
Impress Your Friends with EcmaScript 2015
if (true) { // enter new scope, TDZ starts



// Uninitialized binding for `tmp` is created

tmp = 'abc'; // ReferenceError

console.log(tmp); // ReferenceError



let tmp; // TDZ ends, `tmp` is initialized with `undefined`

console.log(tmp); // undefined



tmp = 123;

console.log(tmp); // 123

}
Es6 Arrow functions
Impress Your Friends with EcmaScript 2015
ES6 Arrow Functions
• Arrow function expression aka fat arrow
functions are a shorter syntax

• Lexically binds the this value

• Arrow functions are always anonymous
var numbers = [1,2,3,4,5];

var timesTwo = numbers.map(function (number) {

return number * 2;

});

console.log(timesTwo); // [2, 4, 6, 8, 10]
var numbers = [1,2,3,4,5];

var timesTwo = numbers.map((number) => number * 2);

console.log(timesTwo); // [2, 4, 6, 8, 10]
var numbers = [1,2,3,4,5];

var timesTwo = numbers.map(number => number * 2);

console.log(timesTwo); // [2, 4, 6, 8, 10]
function FooCtrl (FooService) {

this.foo = 'Hello';

FooService

.doSomething(function (response) {

this.foo = response;

});

}
function FooCtrl (FooService) {

this.foo = 'Hello';

FooService

.doSomething(function (response) {

this.foo = response;

}.bind(this));

}
function FooCtrl (FooService) {

var that = this;

that.foo = 'Hello';

FooService

.doSomething(function (response) {

that.foo = response;

});

}
function FooCtrl (FooService) {

this.foo = 'Hello';

FooService

.doSomething((response) => { // woo, pretty

this.foo = response;

});

}
function FooCtrl (FooService) {

this.foo = 'Hello';

FooService

.doSomething(response => this.foo = response);

}
Es6 Template Strings
ES6 Template strings
• Multi-line strings

• Expression interpolation

• Do not let untrusted users construct
template strings!
console.log("string text line 1n"+

"string text line 2");

// "string text line 1

// string text line 2"
console.log(`string text line 1

string text line 2`);

// "string text line 1

// string text line 2"
var a = 5;

var b = 10;

console.log("Fifteen is " + (a + b) + " andnnot " +
(2 * a + b) + ".");

// "Fifteen is 15 and

// not 20."
var a = 5;

var b = 10;

console.log(`Fifteen is ${a + b} andnnot ${2 * a + b}.`);

// "Fifteen is 15 and

// not 20."
Es6 Object Literals
ES6 Object Literals
• Object literals are extended to be more
convenient and more like defining a class

• Better property and method declarations
var x = 10;

var y = 30;



var coordinates = {

x: x,

y: y

};



console.log(coordinates); // { x: 10, y: 30 }
let x = 10;

let y = 30;



let coordinates = { x, y };



console.log(coordinates); // { x: 10, y: 30 }
let x = 10;

let y = 30;



let coordinates = {

x,

y,

z: 10,

};



console.log(coordinates); // { x: 10, y: 30, z: 10 }
var cart = {

_items: [],

addItem: function(item) {

this._items.push(item);



return this;

},

toString: function() {

return this._items.join(', ');

}

}



cart.addItem('apple')

.addItem('orange')

.addItem('banana');



console.log(cart.toString()); // apple, orange, banana
var cart = {

_items: [],

addItem(item) {

this._items.push(item);



return this;

},

toString() {

return this._items.join(', ');

}

}



cart.addItem('apple')

.addItem('orange')

.addItem('banana');



console.log(cart.toString()); // apple, orange, banana
Es6 Array API
ES6 Array API
• Array.from converts array-like objects into arrays 

• Array.keys, Array.values and Array.entries are
handy for iterating over arrays

• Array.find returns the first element that the
callback returns true
•Array.findIndex returns the index of the first
element that the callback returns true
// Array-like object (arguments) to Array

function f() {

return Array.from(arguments);

}



f(1, 2, 3);

// [1, 2, 3]





// Any iterable object...

// Set

var s = new Set(["foo", window]);

Array.from(s);

// ["foo", window]





// Map

var m = new Map([[1, 2], [2, 4], [4, 8]]);

Array.from(m);

// [[1, 2], [2, 4], [4, 8]]





// String

Array.from("foo");

// ["f", "o", "o"]

var arr = ["a", "b", "c"];

var iterator = arr.keys();



console.log(iterator.next()); // { value: 0, done: false }

console.log(iterator.next()); // { value: 1, done: false }

console.log(iterator.next()); // { value: 2, done: false }

console.log(iterator.next()); // { value: undefined, done: true }
var arr = ['w', 'y', 'k', 'o', 'p'];

var eArr = arr.values();

console.log(eArr.next().value); // w

console.log(eArr.next().value); // y

console.log(eArr.next().value); // k

console.log(eArr.next().value); // o

console.log(eArr.next().value); // p
var arr = ['a', 'b', 'c'];

var eArr = arr.entries();



console.log(eArr.next().value); // [0, 'a']

console.log(eArr.next().value); // [1, 'b']

console.log(eArr.next().value); // [2, 'c']
function isPrime(element, index, array) {

var start = 2;

while (start <= Math.sqrt(element)) {

if (element % start++ < 1) {

return false;

}

}

return element > 1;

}



console.log([4, 6, 8, 12].find(isPrime)); // undefined, not found

console.log([4, 5, 8, 12].find(isPrime)); // 5
function isPrime(element, index, array) {

var start = 2;

while (start <= Math.sqrt(element)) {

if (element % start++ < 1) {

return false;

}

}

return element > 1;

}



console.log([4, 6, 8, 12].findIndex(isPrime)); // -1, not found

console.log([4, 6, 7, 12].findIndex(isPrime)); // 2
Es6 SET
ES6 SET
• Lets you store unique values of any type

• You can store primitive values or object
references
var mySet = new Set();



mySet.add(1);

mySet.add(5);

mySet.add("some text");



mySet.has(1); // true

mySet.has(3); // false, 3 has not been added to the set

mySet.has(5); // true

mySet.has(Math.sqrt(25)); // true

mySet.has("Some Text".toLowerCase()); // true



mySet.size; // 3



mySet.delete(5); // removes 5 from the set

mySet.has(5); // false, 5 has been removed



mySet.size; // 2, we just removed one value
Es6 MAP
ES6 MAP
• Simple key/value map

• Any value (object or primitive value) can be
used as either a key or a value
var myMap = new Map();



var keyObj = {},

keyFunc = function () {},

keyString = "a string";



// setting the values

myMap.set(keyString, "value associated with 'a string'");

myMap.set(keyObj, "value associated with keyObj");

myMap.set(keyFunc, "value associated with keyFunc");



myMap.size; // 3



// getting the values

myMap.get(keyString); // "value associated with 'a string'"

myMap.get(keyObj); // "value associated with keyObj"

myMap.get(keyFunc); // "value associated with keyFunc"



myMap.get("a string"); // "value associated with 'a string'"

// because keyString === 'a string'

myMap.get({}); // undefined, because keyObj !== {}

myMap.get(function() {}) // undefined, because keyFunc !==
function () {}
Es6 Resources
https://leanpub.com/exploring-es6/
http://es6-features.org/
http://es6katas.org/
http://www.es6fiddle.net/
http://es6-features.org/
http://www.2ality.com/
https://github.com/lukehoban/es6features
http://codepen.io/bradleyboy/posts/getting-to-know-es6-object-literals
http://jamesknelson.com/es6-the-bits-youll-actually-use
http://jamesknelson.com/es6-cheatsheet.png
Impress Your Friends with EcmaScript 2015
Thank you!

More Related Content

PPSX
Symfony2 meets propel 1.5
PPTX
Programming Java - Lection 07 - Puzzlers - Lavrentyev Fedor
PDF
Scala ActiveRecord
PDF
SQLAlchemy Core: An Introduction
KEY
Google Guava
PDF
8時間耐久CakePHP2 勉強会
PDF
Google guava
PDF
CakeFest 2013 keynote
Symfony2 meets propel 1.5
Programming Java - Lection 07 - Puzzlers - Lavrentyev Fedor
Scala ActiveRecord
SQLAlchemy Core: An Introduction
Google Guava
8時間耐久CakePHP2 勉強会
Google guava
CakeFest 2013 keynote

What's hot (20)

PDF
PofEAA and SQLAlchemy
PDF
Lithium: The Framework for People Who Hate Frameworks, Tokyo Edition
PDF
Google Guava
PPTX
CodeCamp Iasi 10 march 2012 - Practical Groovy
PDF
Google Guava - Core libraries for Java & Android
PDF
Clean code with google guava jee conf
PPTX
Programming Java - Lection 04 - Generics and Lambdas - Lavrentyev Fedor
PDF
The State of Lithium
PDF
Melhorando sua API com DSLs
PDF
Advanced symfony Techniques
PDF
Perl6 grammars
ODP
An introduction to SQLAlchemy
PPT
Corephpcomponentpresentation 1211425966721657-8
PPTX
Promise it's partial
PPTX
Down the rabbit hole, profiling in Django
PPTX
The Groovy Puzzlers – The Complete 01 and 02 Seasons
PDF
The Magic Of Elixir
KEY
Why Learn Python?
PDF
What's New in PHP 5.5
KEY
Can't Miss Features of PHP 5.3 and 5.4
PofEAA and SQLAlchemy
Lithium: The Framework for People Who Hate Frameworks, Tokyo Edition
Google Guava
CodeCamp Iasi 10 march 2012 - Practical Groovy
Google Guava - Core libraries for Java & Android
Clean code with google guava jee conf
Programming Java - Lection 04 - Generics and Lambdas - Lavrentyev Fedor
The State of Lithium
Melhorando sua API com DSLs
Advanced symfony Techniques
Perl6 grammars
An introduction to SQLAlchemy
Corephpcomponentpresentation 1211425966721657-8
Promise it's partial
Down the rabbit hole, profiling in Django
The Groovy Puzzlers – The Complete 01 and 02 Seasons
The Magic Of Elixir
Why Learn Python?
What's New in PHP 5.5
Can't Miss Features of PHP 5.3 and 5.4
Ad

Viewers also liked (13)

PPTX
Introduction to angular 2
PDF
AngularJS Directives - DSL for your HTML
PDF
Turn Your Designers Into Death Stars with Angular
PDF
ngEurope 2014: Become a Realtime Cage Dragon with Firebase and AngularJS
PDF
ngAnimate crash course
PDF
Badges? We don't need no stinkin' badges!
PDF
Go Beast Mode with Realtime Reactive Interfaces in Angular 2 and Firebase
PDF
The REAL Angular Keynote
PDF
Embrace the Angular 2 Ethos in Angular 1.x
PDF
RxJS - The Reactive extensions for JavaScript
PDF
Get that Corner Office with Angular 2 and Electron
PDF
Ionic Crash Course! Hack-a-ton SF
PPTX
Summer internship report | repairwale.com mobile application design and devel...
Introduction to angular 2
AngularJS Directives - DSL for your HTML
Turn Your Designers Into Death Stars with Angular
ngEurope 2014: Become a Realtime Cage Dragon with Firebase and AngularJS
ngAnimate crash course
Badges? We don't need no stinkin' badges!
Go Beast Mode with Realtime Reactive Interfaces in Angular 2 and Firebase
The REAL Angular Keynote
Embrace the Angular 2 Ethos in Angular 1.x
RxJS - The Reactive extensions for JavaScript
Get that Corner Office with Angular 2 and Electron
Ionic Crash Course! Hack-a-ton SF
Summer internship report | repairwale.com mobile application design and devel...
Ad

Similar to Impress Your Friends with EcmaScript 2015 (20)

PDF
Workshop 10: ECMAScript 6
PDF
Adding ES6 to Your Developer Toolbox
PDF
Exploring ES6
PPTX
Es6 hackathon
PDF
"let ECMAScript = 6"
PDF
ECMAScript 6 new features
PDF
ScalaFlavor4J
PDF
Idiomatic Javascript (ES5 to ES2015+)
PPT
JSConf: All You Can Leet
PDF
ES6 Simplified
PDF
Say It With Javascript
DOC
Jsphp 110312161301-phpapp02
PDF
Es6 to es5
PDF
HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6
KEY
JavaScript Classes and Inheritance
PDF
ECMAScript2015
PPTX
ES6: Features + Rails
PPTX
Getting started with ES6 : Future of javascript
PPTX
The ES Library for JavaScript Developers
PDF
ESCMAScript 6: Get Ready For The Future. Now
Workshop 10: ECMAScript 6
Adding ES6 to Your Developer Toolbox
Exploring ES6
Es6 hackathon
"let ECMAScript = 6"
ECMAScript 6 new features
ScalaFlavor4J
Idiomatic Javascript (ES5 to ES2015+)
JSConf: All You Can Leet
ES6 Simplified
Say It With Javascript
Jsphp 110312161301-phpapp02
Es6 to es5
HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6
JavaScript Classes and Inheritance
ECMAScript2015
ES6: Features + Rails
Getting started with ES6 : Future of javascript
The ES Library for JavaScript Developers
ESCMAScript 6: Get Ready For The Future. Now

Recently uploaded (20)

PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PDF
Approach and Philosophy of On baking technology
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PPTX
Big Data Technologies - Introduction.pptx
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Encapsulation theory and applications.pdf
PDF
NewMind AI Monthly Chronicles - July 2025
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Empathic Computing: Creating Shared Understanding
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
cuic standard and advanced reporting.pdf
PPTX
Cloud computing and distributed systems.
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Approach and Philosophy of On baking technology
Chapter 3 Spatial Domain Image Processing.pdf
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Big Data Technologies - Introduction.pptx
Advanced methodologies resolving dimensionality complications for autism neur...
Encapsulation theory and applications.pdf
NewMind AI Monthly Chronicles - July 2025
Per capita expenditure prediction using model stacking based on satellite ima...
Understanding_Digital_Forensics_Presentation.pptx
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Empathic Computing: Creating Shared Understanding
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
The Rise and Fall of 3GPP – Time for a Sabbatical?
Dropbox Q2 2025 Financial Results & Investor Presentation
cuic standard and advanced reporting.pdf
Cloud computing and distributed systems.
Mobile App Security Testing_ A Comprehensive Guide.pdf
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Building Integrated photovoltaic BIPV_UPV.pdf

Impress Your Friends with EcmaScript 2015

  • 1. EcmaScript 2015impress your friends at the party
  • 4. Is it Safe to Us Es6?!?!
  • 6. How do we use Es6?
  • 15. ES6 Classes • Simple sugar over the the prototype-based OO pattern • A more convenient form makes classes easier to use • Classes support inheritance, super calls, instance and static methods and constructors
  • 16. class Project {
 constructor(name) {
 this.name = name;
 }
 
 start() {
 return `Project ${this.name} starting`;
 }
 }
 
 var project = new Project("Website");
 project.start(); // "Project Website starting"
  • 18. ES6 INheritance • Inheritance is possible via the prototype • You can inherit from a base class using the extends keyword • You must call super before you can access this in a subclass
  • 19. class Shape {
 …
 toString () {
 return `Shape(${this.id})`
 }
 }
 class Rectangle extends Shape {
 constructor (id, x, y, width, height) {
 super(id, x, y)
 …
 }
 toString () {
 return "Rectangle > " + super.toString()
 }
 }
 class Circle extends Shape {
 constructor (id, x, y, radius) {
 super(id, x, y)
 …
 }
 toString () {
 return "Circle > " + super.toString()
 }
 }
  • 21. ES6 MODULES • Language-level support for modules for component definition • Best of both both CommonJS and AMD • Named exports and default exports
  • 22. //------ 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
  • 23. //------ main.js ------
 import * as lib from 'lib';
 console.log(lib.square(11)); // 121
 console.log(lib.diag(4, 3)); // 5
  • 24. //------ myFunc.js ------
 export default function () { ... };
 
 //------ main1.js ------
 import myFunc from 'myFunc';
 myFunc();
  • 25. //------ MyClass.js ------
 export default class { ... };
 
 //------ main2.js ------
 import MyClass from 'MyClass';
 let inst = new MyClass();
  • 26. Es6 Block Level Scoping
  • 28. ES6 MODULES • Function scope can be tricky! • We can scope variables at a block level using let and const
  • 29. function printName() {
 if(true) {
 var name = "Rafael";
 }
 console.log(name); // Rafael
 }
  • 30. function printName() {
 var name; // variable declaration is hoisted to the top
 if(true) {
 name = "Rafael";
 }
 console.log(name); // Rafael
 }
  • 31. function printName() {
 if(true) {
 let name = "Rafael";
 }
 console.log(name); // ReferenceError: name is not defined
 }
  • 32. function printName() {
 var name = "Hey";
 if(true) {
 let name = "Rafael";
 console.log(name); // Rafael
 }
 console.log(name); // Hey
 }
  • 34. if (true) { // enter new scope, TDZ starts
 
 // Uninitialized binding for `tmp` is created
 tmp = 'abc'; // ReferenceError
 console.log(tmp); // ReferenceError
 
 let tmp; // TDZ ends, `tmp` is initialized with `undefined`
 console.log(tmp); // undefined
 
 tmp = 123;
 console.log(tmp); // 123
 }
  • 37. ES6 Arrow Functions • Arrow function expression aka fat arrow functions are a shorter syntax • Lexically binds the this value • Arrow functions are always anonymous
  • 38. var numbers = [1,2,3,4,5];
 var timesTwo = numbers.map(function (number) {
 return number * 2;
 });
 console.log(timesTwo); // [2, 4, 6, 8, 10]
  • 39. var numbers = [1,2,3,4,5];
 var timesTwo = numbers.map((number) => number * 2);
 console.log(timesTwo); // [2, 4, 6, 8, 10]
  • 40. var numbers = [1,2,3,4,5];
 var timesTwo = numbers.map(number => number * 2);
 console.log(timesTwo); // [2, 4, 6, 8, 10]
  • 41. function FooCtrl (FooService) {
 this.foo = 'Hello';
 FooService
 .doSomething(function (response) {
 this.foo = response;
 });
 }
  • 42. function FooCtrl (FooService) {
 this.foo = 'Hello';
 FooService
 .doSomething(function (response) {
 this.foo = response;
 }.bind(this));
 }
  • 43. function FooCtrl (FooService) {
 var that = this;
 that.foo = 'Hello';
 FooService
 .doSomething(function (response) {
 that.foo = response;
 });
 }
  • 44. function FooCtrl (FooService) {
 this.foo = 'Hello';
 FooService
 .doSomething((response) => { // woo, pretty
 this.foo = response;
 });
 }
  • 45. function FooCtrl (FooService) {
 this.foo = 'Hello';
 FooService
 .doSomething(response => this.foo = response);
 }
  • 47. ES6 Template strings • Multi-line strings • Expression interpolation • Do not let untrusted users construct template strings!
  • 48. console.log("string text line 1n"+
 "string text line 2");
 // "string text line 1
 // string text line 2"
  • 49. console.log(`string text line 1
 string text line 2`);
 // "string text line 1
 // string text line 2"
  • 50. var a = 5;
 var b = 10;
 console.log("Fifteen is " + (a + b) + " andnnot " + (2 * a + b) + ".");
 // "Fifteen is 15 and
 // not 20."
  • 51. var a = 5;
 var b = 10;
 console.log(`Fifteen is ${a + b} andnnot ${2 * a + b}.`);
 // "Fifteen is 15 and
 // not 20."
  • 53. ES6 Object Literals • Object literals are extended to be more convenient and more like defining a class • Better property and method declarations
  • 54. var x = 10;
 var y = 30;
 
 var coordinates = {
 x: x,
 y: y
 };
 
 console.log(coordinates); // { x: 10, y: 30 }
  • 55. let x = 10;
 let y = 30;
 
 let coordinates = { x, y };
 
 console.log(coordinates); // { x: 10, y: 30 }
  • 56. let x = 10;
 let y = 30;
 
 let coordinates = {
 x,
 y,
 z: 10,
 };
 
 console.log(coordinates); // { x: 10, y: 30, z: 10 }
  • 57. var cart = {
 _items: [],
 addItem: function(item) {
 this._items.push(item);
 
 return this;
 },
 toString: function() {
 return this._items.join(', ');
 }
 }
 
 cart.addItem('apple')
 .addItem('orange')
 .addItem('banana');
 
 console.log(cart.toString()); // apple, orange, banana
  • 58. var cart = {
 _items: [],
 addItem(item) {
 this._items.push(item);
 
 return this;
 },
 toString() {
 return this._items.join(', ');
 }
 }
 
 cart.addItem('apple')
 .addItem('orange')
 .addItem('banana');
 
 console.log(cart.toString()); // apple, orange, banana
  • 60. ES6 Array API • Array.from converts array-like objects into arrays • Array.keys, Array.values and Array.entries are handy for iterating over arrays • Array.find returns the first element that the callback returns true •Array.findIndex returns the index of the first element that the callback returns true
  • 61. // Array-like object (arguments) to Array
 function f() {
 return Array.from(arguments);
 }
 
 f(1, 2, 3);
 // [1, 2, 3]
 
 
 // Any iterable object...
 // Set
 var s = new Set(["foo", window]);
 Array.from(s);
 // ["foo", window]
 
 
 // Map
 var m = new Map([[1, 2], [2, 4], [4, 8]]);
 Array.from(m);
 // [[1, 2], [2, 4], [4, 8]]
 
 
 // String
 Array.from("foo");
 // ["f", "o", "o"]

  • 62. var arr = ["a", "b", "c"];
 var iterator = arr.keys();
 
 console.log(iterator.next()); // { value: 0, done: false }
 console.log(iterator.next()); // { value: 1, done: false }
 console.log(iterator.next()); // { value: 2, done: false }
 console.log(iterator.next()); // { value: undefined, done: true }
  • 63. var arr = ['w', 'y', 'k', 'o', 'p'];
 var eArr = arr.values();
 console.log(eArr.next().value); // w
 console.log(eArr.next().value); // y
 console.log(eArr.next().value); // k
 console.log(eArr.next().value); // o
 console.log(eArr.next().value); // p
  • 64. var arr = ['a', 'b', 'c'];
 var eArr = arr.entries();
 
 console.log(eArr.next().value); // [0, 'a']
 console.log(eArr.next().value); // [1, 'b']
 console.log(eArr.next().value); // [2, 'c']
  • 65. function isPrime(element, index, array) {
 var start = 2;
 while (start <= Math.sqrt(element)) {
 if (element % start++ < 1) {
 return false;
 }
 }
 return element > 1;
 }
 
 console.log([4, 6, 8, 12].find(isPrime)); // undefined, not found
 console.log([4, 5, 8, 12].find(isPrime)); // 5
  • 66. function isPrime(element, index, array) {
 var start = 2;
 while (start <= Math.sqrt(element)) {
 if (element % start++ < 1) {
 return false;
 }
 }
 return element > 1;
 }
 
 console.log([4, 6, 8, 12].findIndex(isPrime)); // -1, not found
 console.log([4, 6, 7, 12].findIndex(isPrime)); // 2
  • 68. ES6 SET • Lets you store unique values of any type • You can store primitive values or object references
  • 69. var mySet = new Set();
 
 mySet.add(1);
 mySet.add(5);
 mySet.add("some text");
 
 mySet.has(1); // true
 mySet.has(3); // false, 3 has not been added to the set
 mySet.has(5); // true
 mySet.has(Math.sqrt(25)); // true
 mySet.has("Some Text".toLowerCase()); // true
 
 mySet.size; // 3
 
 mySet.delete(5); // removes 5 from the set
 mySet.has(5); // false, 5 has been removed
 
 mySet.size; // 2, we just removed one value
  • 71. ES6 MAP • Simple key/value map • Any value (object or primitive value) can be used as either a key or a value
  • 72. var myMap = new Map();
 
 var keyObj = {},
 keyFunc = function () {},
 keyString = "a string";
 
 // setting the values
 myMap.set(keyString, "value associated with 'a string'");
 myMap.set(keyObj, "value associated with keyObj");
 myMap.set(keyFunc, "value associated with keyFunc");
 
 myMap.size; // 3
 
 // getting the values
 myMap.get(keyString); // "value associated with 'a string'"
 myMap.get(keyObj); // "value associated with keyObj"
 myMap.get(keyFunc); // "value associated with keyFunc"
 
 myMap.get("a string"); // "value associated with 'a string'"
 // because keyString === 'a string'
 myMap.get({}); // undefined, because keyObj !== {}
 myMap.get(function() {}) // undefined, because keyFunc !== function () {}