SlideShare a Scribd company logo
ES6 (2015)
Enterprise Java
ECMAScript 6
Internal workshop es6_2015
History
European Computer Manufacturers' Association, ECMA
What we will see...
● Declaration variables with let, var and const
● Template String
● Arrows
● Destructuring
● Default + Rest + Spread
● Iterators
● Classes
● Module Loaders
● Map + Set + WeakMap + WeakSet
● Promises
Let vs Var
Using let instead of var
prevents variable
declarations from being
moved to the top of the
scope.
Prior to executing our code,
JS moves var declarations
all the way up to the top of
the scope
This is known as: hoisting
http://jsfiddle.net/rmXcF/5/
var is scoped to the nearest function block and let is scoped to the nearest
enclosing block
for(var i = 1; i < 6; i++) {
document.getElementById('my-element' + i) .addEventListener('click',
function() { alert(i) })
}
//reference to the i object is being stored in the click handler closure, rather than
the actual value of i.
We can also use CONST for read-only named contants
● Consts must always be assigned an initial value.
● Consts are scoped by blocks.
Can we safety use let?
● Server side: YES
● Transpiled JS: YES
● Client-Side + Not
Transpiled JS: Check
browsers support
● If you're writing server-side JavaScript code (Node.js), you can safely
use the let statement.
● If you're writing client-side JavaScript code and use a transpiler (like
Traceur), you can safely use the let statement, however your code is
likely to be anything but optimal with respect to performance.
● If you're writing client-side JavaScript code and don't use a transpiler, you
need to consider browser support.
● Today, Feb 2016, these are some browsers that either don't support
let or have only partial support
● Internet explorer 10 and below (no support)
● Firefox 43 and below (no support)
● Safari 9 and below (no support)
● Opera Mini 8 and below (no support)
● Android browser 4 and below (no support)
● Opera 36 and below (partial support)
● Chome 51 and below (partial support)
Template Strings
``
We can interpolate
Strings easily with
new ES6 standard
//ES5
var sayHi = "ola " +
"k " +
"ase ";
//ES6
var sayHi = `ola
k
ase`;
//ES6
let name1 = "JavaScript";
let name2 = "awesome";
console.log(`I just wanna say that ${name1} is
${name2)`;
Arrows
=>
//ES5
var data = [{...}, {...}, {...}, ...];
data.forEach(function(elem){
// We treat the element
console.log(elem)
});
Imagine a variable with
some data that includes
an array of objects
With arrow function, we
can substitute this code
//ES6
var data = [{...}, {...}, {...}, ...];
data.forEach(elem => {
console.log(elem);
});
We could also use it
like this...
//ES5
var myFunction = function(num) {
return num + num;
}
// ES6
var myFunction = (num) => num + num;
Destructuring
Destructuring allows
using pattern matching,
with support for
matching arrays and
objects.
Destructuring is fail-soft,
similar to standard object
lookup foo["bar"],
producing undefined
values when not found.
Arrays
The destructuring assignment uses similar syntax, but on the left-hand side of the assignment
to define what elements to extract from the sourced variable.
var foo = ["one", "two", "three"];
var [one, two, three] = foo;
console.log(one); // "one"
console.log(two); // "two"
console.log(three); // "three"
A variable can be assigned its value via destructuring separate from the variable's declaration.
var a, b;
[a, b] = [1, 2];
console.log(a); // 1
console.log(b); // 2
A variable can be assigned a default, in the case that the value pulled from the array is
undefined.
var a, b;
[a=5, b=7] = [1];
console.log(a); // 1
console.log(b); // 7
We can ignore same returned values and get them from a function call
function f() {
return [1, 2, 3];
}
var [a, , b] = f();
console.log(a); // 1
console.log(b); // 3
Objects
var a, b;
({a, b} = {a:1, b:2});
Default values
var {a=10, b=5} = {a: 3};
console.log(a); // 3
console.log(b); // 5
//ES5
function drawES5Chart(options) {
options = options === undefined ? {} : options;
var size = options.size === undefined ? 'big' : options.size;
var cords = options.cords === undefined ? { x: 0, y: 0 } :
options.cords;
var radius = options.radius === undefined ? 25 : options.radius;
console.log(size, cords, radius);
// now finally do some chart drawing
}
drawES5Chart({
cords: { x: 18, y: 30 },
radius: 30
});
Setting a function
parameter's default value
//ES6
function drawES6Chart({size = 'big', cords = { x: 0, y: 0 }, radius =
25} = {}) {
console.log(size, cords, radius);
// do some chart drawing
}
// In Firefox, default values for destructuring assignments are not
yet implemented (as described below).
// The workaround is to write the parameters in the following way:
// ({size: size = 'big', cords: cords = { x: 0, y: 0 }, radius:
radius = 25} = {})
drawES6Chart({
cords: { x: 18, y: 30 },
radius: 30
});
Setting a function
parameter's default value
Take really care with
Browser compatibility!
Default, Rest + Spread
...
We won´t have to do
function(valor) {
value = value ||
"foo";
}
anymore...now:
//ES6
function(value = "foo")
{...};
(Default) Callee-evaluated default parameter values.
function f(x, y=12) {
// y is 12 if not passed (or passed as undefined)
return x + y;
}
f(3) == 15
(Rest) Turn an array into consecutive arguments in a function call.
function f(x, ...y) {
// y is an Array
return x * y.length;
}
f(3, "hello", true) == 6
(Spread) allows an expression to be expanded in places where multiple
arguments are expected.
function f(x, y, z) {
return x + y + z;
}
//ES5
f.apply(null,[1,2,3]);
// Pass each elem of array as argument
f(...[1,2,3]) == 6
Iterators
for (let … in ...){
...
}
//over an Array
let iterable = [10, 20, 30];
for (let value of iterable) {
console.log(value);
}
//over a Map
let iterable = new Map([["a", 1], ["b", 2], ["c", 3]]);
for (let entry of iterable) {
console.log(entry);
}
// [a, 1]
// [b, 2]
// [c, 3]
for (let [key, value] of iterable) {
console.log(value);
}
var iterable = {
[Symbol.iterator]() {
return {
i: 0,
next() {
if (this.i < 3) {
return { value: this.i++, done: false };
}
return { value: undefined, done: true };
}
};
}
};
for (var value of iterable) {
console.log(value);
}
// 0
// 1
// 2
Iteration is based on
these duck-typed
interfaces (using
TypeScript type
syntax for exposition
only) so...
We can also iterate
over an Iterable
Object
The for...in loop will iterate
over all enumerable
properties of an object.
The for...of syntax is specific
to collections, rather than all
objects. It will iterate in this
manner over the elements of
any collection that has a
[Symbol.iterator] property.
Difference between for...of and for...in
Object.prototype.objCustom = function () {};
Array.prototype.arrCustom = function () {};
let iterable = [3, 5, 7];
iterable.foo = "hello";
for (let i in iterable) {
console.log(i); // logs 0, 1, 2, "foo", "arrCustom", "objCustom"
}
for (let i of iterable) {
console.log(i); // logs 3, 5, 7
Wanna go deeper?
Check Generators!
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Re
ference/Iteration_protocols
Classes
class X extends Y {
}
This is how a Class
looks like in new ES6
class TechnicalBook extends Book {
constructor(theme, pages) {
super(theme, pages);
this.caps = [];
this.price = "";
// ...
}
method() {
// ...
}
_privateMethod() {
// ...
}
}
Modules
import * from modules/*
//File: lib/person.js
module "person" {
export function hello(name) {
return name;
}
}
//File: app.js
import { hello } from "person";
var app = {
foo: function() {
hello("Mike");
}
}
export app;
This is like browserfy
native.
We can import
functions from others
scripts with no need
to import scripts from
the HTML
Map + Set + WeakMap + WeakSet
// Sets
var s = new Set();
s.add("hello").add("goodbye").add("hello");
s.size === 2;
s.has("hello") === true;
// Maps
var m = new Map();
m.set("hello", 42);
m.set(s, 34);
m.get(s) == 34;
// Weak Maps
var wm = new WeakMap();
wm.set(s, { extra: 42 });
wm.size === undefined
WeakMap.prototype.get(key) : any
WeakMap.prototype.set(key, value) : this
WeakMap.prototype.has(key) : boolean
WeakMap.prototype.delete(key) : boolean
// Weak Sets
var ws = new WeakSet();
ws.add({ data: 42 });
// Because the added object has no other references,
it will not be held in the set
WeakSet.prototype.add(value)
WeakSet.prototype.has(value)
WeakSet.prototype.delete(value)
Primitive data types as
keys are not allowed
A WeakMap is a map
(dictionary) where the
keys are weak - that is,
if all references to the
key are lost and there
are no more references
to the value - the value
can be garbage
collected
We can’t iterate over the
contents
No clear method!
When to use what?
http://stackoverflow.com/questions/29413222/what-are-the-
actual-uses-of-es6-weakmap
Promises
//promise implementation
function readFile(filename, enc){
return new Promise(function (fulfill, reject){
fs.readFile(filename, enc, function (err, res){
if (err) reject(err);
else fulfill(res);
});
});
}
//using the promise
function readJSON(filename){
return readFile(filename, 'utf8').then(function (res){
return JSON.parse(res)
})
}
https://www.promisejs.org
What more…?
Proxies
Proxies enable creation of objects with the full range of behaviors available
to host objects. Can be used for interception, object virtualization,
logging/profiling, etc.
Symbols
Symbols allow properties to be keyed by either string (as in ES5) orsymbol
Subclassable Built-ins
In ES6, built-ins like Array, Date and DOM Elements can be subclassed.
What more…?
Math + Number + String + Array + Object APIs
Many new library additions, including core Math libraries, Array conversion
helpers, String helpers, and Object.assign for copying.
Reflect API Full reflection API exposing the runtime-level
meta-operations on objects. This is effectively the inverse of the Proxy API,
and allows making calls corresponding to the same meta-operations as the
proxy traps. Especially useful for implementing proxies.
Tail Calls Calls in tail-position are guaranteed to not grow the stack
unboundedly. Makes recursive algorithms safe in the face of unbounded
inputs.
Thanks!
migueloop.github.io

More Related Content

PDF
ES6 - Next Generation Javascript
PPTX
ES6 in Real Life
PPT
Advance features of C++
TXT
Advance C++notes
PDF
Pybelsberg — Constraint-based Programming in Python
PDF
ECMAScript 6 and beyond
PPTX
My favorite slides
PPTX
Oh Composable World!
ES6 - Next Generation Javascript
ES6 in Real Life
Advance features of C++
Advance C++notes
Pybelsberg — Constraint-based Programming in Python
ECMAScript 6 and beyond
My favorite slides
Oh Composable World!

What's hot (20)

PDF
Futures e abstração - QCon São Paulo 2015
PDF
Hidden Gems in Swift
PPTX
Millionways
PPTX
Groovy vs Boilerplate and Ceremony Code
PDF
Standford 2015 week3: Objective-C Compatibility, Property List, Views
PDF
Introduction to Scala
PDF
미려한 UI/UX를 위한 여정
PPT
Collection v3
PDF
Continuation Passing Style and Macros in Clojure - Jan 2012
PPT
Functional Patterns for the non-mathematician
PDF
Programmation fonctionnelle en JavaScript
PDF
[Let'Swift 2019] 실용적인 함수형 프로그래밍 워크샵
KEY
openFrameworks 007 - utils
PDF
스위프트를 여행하는 히치하이커를 위한 스타일 안내
PDF
OpenXR 1.0 Reference Guide
KEY
Exhibition of Atrocity
PDF
OpenGL 4.4 Reference Card
PDF
JavaScript ES6
PDF
The Macronomicon
PDF
Dynamic C++ ACCU 2013
Futures e abstração - QCon São Paulo 2015
Hidden Gems in Swift
Millionways
Groovy vs Boilerplate and Ceremony Code
Standford 2015 week3: Objective-C Compatibility, Property List, Views
Introduction to Scala
미려한 UI/UX를 위한 여정
Collection v3
Continuation Passing Style and Macros in Clojure - Jan 2012
Functional Patterns for the non-mathematician
Programmation fonctionnelle en JavaScript
[Let'Swift 2019] 실용적인 함수형 프로그래밍 워크샵
openFrameworks 007 - utils
스위프트를 여행하는 히치하이커를 위한 스타일 안내
OpenXR 1.0 Reference Guide
Exhibition of Atrocity
OpenGL 4.4 Reference Card
JavaScript ES6
The Macronomicon
Dynamic C++ ACCU 2013
Ad

Viewers also liked (18)

DOC
Pongo, y la coma
PDF
Henrymena cloud
PDF
[Isentek] eCompass API Quick Start
PPTX
#SpectraCares 2016 Photo Gallery
PDF
Programação e ementa
TXT
Website
PPTX
Act1.1.comercio electrónico.angélicadéfaz
PPTX
A tether-less legged piezoelectric miniature robot for bidirectional motion
PPTX
docente- martha ivvon
PPTX
International Ataturk Alatoo University, International Relations 4-A, Turkey ...
DOC
capstone.doc 1a.4doc.doc final
PPTX
Los inicio de internet nos remontan a los
PDF
Herdeiros de nelson rodrigues
PPTX
Խաշի պատմություն
PPTX
Account Based Marketing Software Product Demo: Winter Release, October 18th 2016
DOCX
Báo cáo thực tập - Nhơn Mỹ - Nguyễn Kỳ Thanh Thảo - 2016
PDF
Elizabeth verar how to use thrive themes to build landing pages
PDF
[Advantech] Modbus protocol training (ModbusTCP, ModbusRTU)
Pongo, y la coma
Henrymena cloud
[Isentek] eCompass API Quick Start
#SpectraCares 2016 Photo Gallery
Programação e ementa
Website
Act1.1.comercio electrónico.angélicadéfaz
A tether-less legged piezoelectric miniature robot for bidirectional motion
docente- martha ivvon
International Ataturk Alatoo University, International Relations 4-A, Turkey ...
capstone.doc 1a.4doc.doc final
Los inicio de internet nos remontan a los
Herdeiros de nelson rodrigues
Խաշի պատմություն
Account Based Marketing Software Product Demo: Winter Release, October 18th 2016
Báo cáo thực tập - Nhơn Mỹ - Nguyễn Kỳ Thanh Thảo - 2016
Elizabeth verar how to use thrive themes to build landing pages
[Advantech] Modbus protocol training (ModbusTCP, ModbusRTU)
Ad

Similar to Internal workshop es6_2015 (20)

PPTX
ES6 and BEYOND
PPTX
Introduction to es6
PDF
ES6: The future is now
PPTX
Getting started with ES6 : Future of javascript
PDF
ECMAScript 6 Review
PDF
Es6 to es5
PPTX
The ES Library for JavaScript Developers
PDF
MCS First Year JavaScript ES6 Features.pdf
PDF
Idiomatic Javascript (ES5 to ES2015+)
PPTX
ECMAScript 2015
PPTX
Javantura v3 - ES6 – Future Is Now – Nenad Pečanac
PDF
Impress Your Friends with EcmaScript 2015
ODP
ES6 PPT FOR 2016
PPTX
ES6 Overview
PDF
Fitc whats new in es6 for web devs
PPTX
Getting started with ES6
PDF
JavaScript - new features in ECMAScript 6
PDF
JavaScript in 2016
PPTX
JavaScript in 2016 (Codemotion Rome)
PDF
What's New in ES6 for Web Devs
ES6 and BEYOND
Introduction to es6
ES6: The future is now
Getting started with ES6 : Future of javascript
ECMAScript 6 Review
Es6 to es5
The ES Library for JavaScript Developers
MCS First Year JavaScript ES6 Features.pdf
Idiomatic Javascript (ES5 to ES2015+)
ECMAScript 2015
Javantura v3 - ES6 – Future Is Now – Nenad Pečanac
Impress Your Friends with EcmaScript 2015
ES6 PPT FOR 2016
ES6 Overview
Fitc whats new in es6 for web devs
Getting started with ES6
JavaScript - new features in ECMAScript 6
JavaScript in 2016
JavaScript in 2016 (Codemotion Rome)
What's New in ES6 for Web Devs

Recently uploaded (20)

PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PPTX
history of c programming in notes for students .pptx
PDF
System and Network Administraation Chapter 3
PDF
Digital Strategies for Manufacturing Companies
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
medical staffing services at VALiNTRY
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PPTX
ai tools demonstartion for schools and inter college
PPTX
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
PDF
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
Understanding Forklifts - TECH EHS Solution
PDF
AI in Product Development-omnex systems
PPTX
ISO 45001 Occupational Health and Safety Management System
PPTX
Operating system designcfffgfgggggggvggggggggg
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
Upgrade and Innovation Strategies for SAP ERP Customers
history of c programming in notes for students .pptx
System and Network Administraation Chapter 3
Digital Strategies for Manufacturing Companies
2025 Textile ERP Trends: SAP, Odoo & Oracle
medical staffing services at VALiNTRY
Navsoft: AI-Powered Business Solutions & Custom Software Development
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Internet Downloader Manager (IDM) Crack 6.42 Build 41
ai tools demonstartion for schools and inter college
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Understanding Forklifts - TECH EHS Solution
AI in Product Development-omnex systems
ISO 45001 Occupational Health and Safety Management System
Operating system designcfffgfgggggggvggggggggg

Internal workshop es6_2015

  • 4. What we will see... ● Declaration variables with let, var and const ● Template String ● Arrows ● Destructuring ● Default + Rest + Spread ● Iterators ● Classes ● Module Loaders ● Map + Set + WeakMap + WeakSet ● Promises
  • 6. Using let instead of var prevents variable declarations from being moved to the top of the scope. Prior to executing our code, JS moves var declarations all the way up to the top of the scope This is known as: hoisting http://jsfiddle.net/rmXcF/5/ var is scoped to the nearest function block and let is scoped to the nearest enclosing block for(var i = 1; i < 6; i++) { document.getElementById('my-element' + i) .addEventListener('click', function() { alert(i) }) } //reference to the i object is being stored in the click handler closure, rather than the actual value of i. We can also use CONST for read-only named contants ● Consts must always be assigned an initial value. ● Consts are scoped by blocks.
  • 7. Can we safety use let? ● Server side: YES ● Transpiled JS: YES ● Client-Side + Not Transpiled JS: Check browsers support ● If you're writing server-side JavaScript code (Node.js), you can safely use the let statement. ● If you're writing client-side JavaScript code and use a transpiler (like Traceur), you can safely use the let statement, however your code is likely to be anything but optimal with respect to performance. ● If you're writing client-side JavaScript code and don't use a transpiler, you need to consider browser support. ● Today, Feb 2016, these are some browsers that either don't support let or have only partial support ● Internet explorer 10 and below (no support) ● Firefox 43 and below (no support) ● Safari 9 and below (no support) ● Opera Mini 8 and below (no support) ● Android browser 4 and below (no support) ● Opera 36 and below (partial support) ● Chome 51 and below (partial support)
  • 9. We can interpolate Strings easily with new ES6 standard //ES5 var sayHi = "ola " + "k " + "ase "; //ES6 var sayHi = `ola k ase`; //ES6 let name1 = "JavaScript"; let name2 = "awesome"; console.log(`I just wanna say that ${name1} is ${name2)`;
  • 11. //ES5 var data = [{...}, {...}, {...}, ...]; data.forEach(function(elem){ // We treat the element console.log(elem) }); Imagine a variable with some data that includes an array of objects With arrow function, we can substitute this code //ES6 var data = [{...}, {...}, {...}, ...]; data.forEach(elem => { console.log(elem); });
  • 12. We could also use it like this... //ES5 var myFunction = function(num) { return num + num; } // ES6 var myFunction = (num) => num + num;
  • 14. Destructuring allows using pattern matching, with support for matching arrays and objects. Destructuring is fail-soft, similar to standard object lookup foo["bar"], producing undefined values when not found. Arrays The destructuring assignment uses similar syntax, but on the left-hand side of the assignment to define what elements to extract from the sourced variable. var foo = ["one", "two", "three"]; var [one, two, three] = foo; console.log(one); // "one" console.log(two); // "two" console.log(three); // "three" A variable can be assigned its value via destructuring separate from the variable's declaration. var a, b; [a, b] = [1, 2]; console.log(a); // 1 console.log(b); // 2
  • 15. A variable can be assigned a default, in the case that the value pulled from the array is undefined. var a, b; [a=5, b=7] = [1]; console.log(a); // 1 console.log(b); // 7 We can ignore same returned values and get them from a function call function f() { return [1, 2, 3]; } var [a, , b] = f(); console.log(a); // 1 console.log(b); // 3
  • 16. Objects var a, b; ({a, b} = {a:1, b:2}); Default values var {a=10, b=5} = {a: 3}; console.log(a); // 3 console.log(b); // 5
  • 17. //ES5 function drawES5Chart(options) { options = options === undefined ? {} : options; var size = options.size === undefined ? 'big' : options.size; var cords = options.cords === undefined ? { x: 0, y: 0 } : options.cords; var radius = options.radius === undefined ? 25 : options.radius; console.log(size, cords, radius); // now finally do some chart drawing } drawES5Chart({ cords: { x: 18, y: 30 }, radius: 30 }); Setting a function parameter's default value
  • 18. //ES6 function drawES6Chart({size = 'big', cords = { x: 0, y: 0 }, radius = 25} = {}) { console.log(size, cords, radius); // do some chart drawing } // In Firefox, default values for destructuring assignments are not yet implemented (as described below). // The workaround is to write the parameters in the following way: // ({size: size = 'big', cords: cords = { x: 0, y: 0 }, radius: radius = 25} = {}) drawES6Chart({ cords: { x: 18, y: 30 }, radius: 30 }); Setting a function parameter's default value
  • 19. Take really care with Browser compatibility!
  • 20. Default, Rest + Spread ...
  • 21. We won´t have to do function(valor) { value = value || "foo"; } anymore...now: //ES6 function(value = "foo") {...}; (Default) Callee-evaluated default parameter values. function f(x, y=12) { // y is 12 if not passed (or passed as undefined) return x + y; } f(3) == 15 (Rest) Turn an array into consecutive arguments in a function call. function f(x, ...y) { // y is an Array return x * y.length; } f(3, "hello", true) == 6 (Spread) allows an expression to be expanded in places where multiple arguments are expected. function f(x, y, z) { return x + y + z; } //ES5 f.apply(null,[1,2,3]); // Pass each elem of array as argument f(...[1,2,3]) == 6
  • 22. Iterators for (let … in ...){ ... }
  • 23. //over an Array let iterable = [10, 20, 30]; for (let value of iterable) { console.log(value); } //over a Map let iterable = new Map([["a", 1], ["b", 2], ["c", 3]]); for (let entry of iterable) { console.log(entry); } // [a, 1] // [b, 2] // [c, 3] for (let [key, value] of iterable) { console.log(value); }
  • 24. var iterable = { [Symbol.iterator]() { return { i: 0, next() { if (this.i < 3) { return { value: this.i++, done: false }; } return { value: undefined, done: true }; } }; } }; for (var value of iterable) { console.log(value); } // 0 // 1 // 2 Iteration is based on these duck-typed interfaces (using TypeScript type syntax for exposition only) so... We can also iterate over an Iterable Object
  • 25. The for...in loop will iterate over all enumerable properties of an object. The for...of syntax is specific to collections, rather than all objects. It will iterate in this manner over the elements of any collection that has a [Symbol.iterator] property. Difference between for...of and for...in Object.prototype.objCustom = function () {}; Array.prototype.arrCustom = function () {}; let iterable = [3, 5, 7]; iterable.foo = "hello"; for (let i in iterable) { console.log(i); // logs 0, 1, 2, "foo", "arrCustom", "objCustom" } for (let i of iterable) { console.log(i); // logs 3, 5, 7
  • 26. Wanna go deeper? Check Generators! https://developer.mozilla.org/en-US/docs/Web/JavaScript/Re ference/Iteration_protocols
  • 28. This is how a Class looks like in new ES6 class TechnicalBook extends Book { constructor(theme, pages) { super(theme, pages); this.caps = []; this.price = ""; // ... } method() { // ... } _privateMethod() { // ... } }
  • 30. //File: lib/person.js module "person" { export function hello(name) { return name; } } //File: app.js import { hello } from "person"; var app = { foo: function() { hello("Mike"); } } export app; This is like browserfy native. We can import functions from others scripts with no need to import scripts from the HTML
  • 31. Map + Set + WeakMap + WeakSet
  • 32. // Sets var s = new Set(); s.add("hello").add("goodbye").add("hello"); s.size === 2; s.has("hello") === true; // Maps var m = new Map(); m.set("hello", 42); m.set(s, 34); m.get(s) == 34;
  • 33. // Weak Maps var wm = new WeakMap(); wm.set(s, { extra: 42 }); wm.size === undefined WeakMap.prototype.get(key) : any WeakMap.prototype.set(key, value) : this WeakMap.prototype.has(key) : boolean WeakMap.prototype.delete(key) : boolean // Weak Sets var ws = new WeakSet(); ws.add({ data: 42 }); // Because the added object has no other references, it will not be held in the set WeakSet.prototype.add(value) WeakSet.prototype.has(value) WeakSet.prototype.delete(value) Primitive data types as keys are not allowed A WeakMap is a map (dictionary) where the keys are weak - that is, if all references to the key are lost and there are no more references to the value - the value can be garbage collected We can’t iterate over the contents No clear method!
  • 34. When to use what? http://stackoverflow.com/questions/29413222/what-are-the- actual-uses-of-es6-weakmap
  • 36. //promise implementation function readFile(filename, enc){ return new Promise(function (fulfill, reject){ fs.readFile(filename, enc, function (err, res){ if (err) reject(err); else fulfill(res); }); }); } //using the promise function readJSON(filename){ return readFile(filename, 'utf8').then(function (res){ return JSON.parse(res) }) } https://www.promisejs.org
  • 37. What more…? Proxies Proxies enable creation of objects with the full range of behaviors available to host objects. Can be used for interception, object virtualization, logging/profiling, etc. Symbols Symbols allow properties to be keyed by either string (as in ES5) orsymbol Subclassable Built-ins In ES6, built-ins like Array, Date and DOM Elements can be subclassed.
  • 38. What more…? Math + Number + String + Array + Object APIs Many new library additions, including core Math libraries, Array conversion helpers, String helpers, and Object.assign for copying. Reflect API Full reflection API exposing the runtime-level meta-operations on objects. This is effectively the inverse of the Proxy API, and allows making calls corresponding to the same meta-operations as the proxy traps. Especially useful for implementing proxies. Tail Calls Calls in tail-position are guaranteed to not grow the stack unboundedly. Makes recursive algorithms safe in the face of unbounded inputs.