SlideShare a Scribd company logo
1 of 124
Download to read offline
#devES6
A D D I N G E S 6 T O YO U R T O O L B OX
@ j e f f r e y s t r a u s s
”ES6? That's the new JavaScript!
~ mr. developer
”No!
~ me
ECMAScript
ECMAScript2015
not all-or-nothing
experiment
ES6 IS AN EVOLUTION
experiment
NOT ALL-OR-NOTHING
strings
template literals
var greeting =
"Hello, " + user.first + " " + user.last + "!";
var url =
"www.mysite.com" + "/foo/" + fooId + "/bar/" + barId;
ES5:
`backticks`
${expression}
var greeting =
"Hello, " + user.first + " " + user.last + "!";
var url =
"www.mysite.com" + "/foo/" + fooId + "/bar/" + barId;
ES5:
var greeting = `Hello, ${user.first} ${user.last}!`;
var url = `www.mysite.com/foo/${fooId}/bar/${barId}/`;
ES6:
multi-line strings
var poem = "so much dependsn"
+ "uponnn"
+ "a red wheeln"
+ "barrownn"
+ "glazed with rainn"
+ "waternn"
+ "beside the whiten"
+ "chickens";
ES5:
var poem =
`so much depends
upon
a red wheel
barrow
glazed with rain
water
beside the white
chickens`;
ES6:
default
parameters
var drawRect = function(width, height, color) {
width = width || 1;
height = height || 1;
color = color || 'orange';
/* draw stuff */
return width * height;
};
ES5:
var drawRect = function(width, height, color) {
width = width || 1;
height = height || 1;
color = color || 'orange';
/* draw stuff */
return width * height;
};
drawRect(); // draws orange; returns 1
drawRect(0, 4, 'blue'); // draws blue, but returns 4
ES5:
var drawRect = function(width=1, height=1, color='orange') {
/* draw stuff */
return width * height;
};
ES6:
var drawRect = function(width=1, height=1, color='orange') {
/* draw stuff */
return width * height;
};
drawRect(0, 4, 'blue'); // returns 0
ES6:
collections
arrays
var myStringArray = new Array("blue");
// ["blue"]
var myBoolArray = new Array(false);
// [false]
var myIntArray = new Array(2);
// [undefined x 2]
ES5:
var myIntArray = new Array(2);
// [undefined x 2]
ES5:
var fixedIntArray = Array.of(2);
// [2]
ES6:
function getArgsArray() {
return Array.prototype.slice.apply(arguments);
};
getArgsArray('foo', true, 42);
// ['foo', true, 42]
ES5:
function getArgsArray() {
return Array.prototype.slice.apply(arguments);
};
getArgsArray('foo', true, 42);
// ['foo', true, 42]
ES5:
function getArgsArray() {
return Array.from(arguments);
};
getArgsArray('foo', true, 42);
// ['foo', true, 42]
ES6:
iterables
`for-of` loop
var body = ['head', 'shoulders', 'knees', 'toes'];
for (var i = 0; i < body.length; i++) { tap(body[i]) };
ES5:
var body = ['head', 'shoulders', 'knees', 'toes'];
for (var i = 0; i < body.length; i++) { tap(body[i]) };
ES5:
var body = ['head', 'shoulders', 'knees', 'toes'];
for (var part of body) {
tap(part);
}
ES6:
var alphabet = 'abcdefg';
for (var letter of alphabet) {
sing(letter);
}
ES6:
E
...spread operator
var alphabet = 'abcdefg';
function sing() {
console.log(arguments);
}
sing(...alphabet);
// ["a", "b", "c", "d", "e", "f", "g"]
ES6:
E
destructuring
[ ] = fooArray;
{ } = fooObject;
var x, y, z, coords;
coords = [29, 22, 37];
x = coords[0];
y = coords[1];
z = coords[2];
ES5:
var x, y, z, coords;
coords = [29, 22, 37];
x = coords[0];
y = coords[1];
z = coords[2];
ES5:
var coords = [29, 22, 37];
var [x, y, z] = coords;
ES6:
var x, y, z, coords;
coords = [29, 22, 37];
x = coords[0];
y = coords[1];
z = coords[2];
ES5:
var coords = [29, 22, 37];
var [x, y, z] = coords;
var [foo, , bar] = coords;
ES6:
var user, email, display;
user = { name: 'Jeff', email: 'jeff@aranasoft.com' };
email = user.email;
display = user.name;
ES5:
var user, email, display;
user = { name: 'Jeff', email: 'jeff@aranasoft.com' };
email = user.email;
display = user.name;
ES5:
var user = { name: 'Jeff', email: 'jeff@aranasoft.com' };
var {email} = user;
var {name: display} = user;
ES6:
swapping variables
var temp = x;
x = y;
y = temp;
ES5:
[y, x] = [x, y];
ES6:
process return values
var drawRect = function(width=1, height=1, color='orange') {
/* draw stuff */
return {area: width * height, hex: getHex(color)};
};
ES6:
var drawRect = function(width=1, height=1, color='orange') {
/* draw stuff */
return {area: width * height, hex: getHex(color)};
};
var {area, hex: rgb} = drawRect(4, 6, 'blue');
// area == 24, rgb == '#0000FF'
ES6:
{ scope }
block-scoped vars
function-scoped
var foo = function(bar) {
if (bar) {
var message = 'Hello!'; // declared here
alert(message);
}
return message; // still in scope here
};
ES5:
iife
IMMEDIATELY

INVOKED

FUNCTION

EXPRESSION
var foo = function(bar) {
if (bar) {
(function() {
var message = 'Hello!'; // declared here
alert(message);
})();
}
return message; // ReferenceError: message is not defined
};
ES5:
closure
// assume links has an array of DOM elements
for (var i = 0; i < links.length; i++) {
links[i].onclick = function() {
console.log(i);
};
}
// whoops! all links log the max index!
ES5:
// assume links has an array of DOM elements
for (var i = 0; i < links.length; i++) {
links[i].onclick = (function(x) {
return function() {
console.log(x); // preserved context
};
})(i);
}
// clicking links gives the correct result
ES5:
hoisting & globals
function foo() {
x = 10;
y = 20;
var x, y;
var area = x * y;
return area;
};
ES5:
function foo() {
var x, y;
var area;
x = 10;
y = 20;
area = x * y;
return area;
};
ES5:
function foo() {
var x, y;
// yikes! area may be in the global scope!
x = 10;
y = 20;
area = x * y;
return area;
};
ES5:
let
function(bar) {
if (bar) {
var message = 'Hello!'; // declared here
alert(message);
}
return message; // still in scope here
};
ES5:
function(bar) {
if (bar) {
let message = 'Hello!'; // declared here
alert(message);
}
return message; // ReferenceError: message is not defined
};
ES6:
// assume links has an array of DOM elements
for (var i = 0; i < links.length; i++) {
links[i].onclick = function() {
console.log(i);
};
};
// whoops! all links log the max index!
ES5:
// assume links has an array of DOM elements
for (let i = 0; i < links.length; i++) {
links[i].onclick = function() {
console.log(i);
};
};
// all better, with no IIFE or extra closure!
ES6:
temporal dead zone
function foo() {
x = 10; // ReferenceError: x is not defined
y = 20;
let x, y;
let area = x * y;
return area;
};
ES6:
arrow functions
syntax
(args) => { statements }
function logSum(x, y) {
var sum = x + y;
console.log(sum);
}
ES5:
function logSum(x, y) {
var sum = x + y;
console.log(sum);
}
ES5:
let logSum = (a, b) => {
let sum = a + b;
console.log(sum);
};
ES6:
(args) => expression;
function sum(x, y) {
return x + y;
}
ES5:
let sum = (x, y) => x + y;
ES6:
let speak = () => 'Hello!';
let wrappedMethod = (arg) => innerMethod(arg);
let announce = (name) => `Announcing ${name}!`;
let getUser = () => ( { id: 42, name: 'Jeff' } );
ES6:
let speak = () => 'Hello!';
let wrappedMethod = (arg) => innerMethod(arg);
let announce = name => `Announcing ${name}!`;
let getUser = () => ( { id: 42, name: 'Jeff' } );
ES6:
let speak = () => 'Hello!';
let wrappedMethod = (arg) => innerMethod(arg);
let announce = (name) => `Announcing ${name}!`;
let getUser = () => ( { id: 42, name: 'Jeff' } );
ES6:
lexical `this`
var person = {
name: 'Jeff',
greet: function() {
console.log('Now greeting ' + this.name);
setTimeout(function() {
console.log('Hello, ' + this.name + '!');
}, 3000);
}
};
ES5:
var person = {
name: 'Jeff',
greet: function() {
console.log('Now greeting ' + this.name);
setTimeout(function() {
console.log('Hello, ' + this.name + '!');
}, 3000);
}
};
person.greet();
// Now greeting Jeff
ES5:
var person = {
name: 'Jeff',
greet: function() {
console.log('Now greeting ' + this.name);
setTimeout(function() {
console.log('Hello, ' + this.name + '!');
}, 3000);
}
};
person.greet();
// Now greeting Jeff
// Hello, !
ES5:
that | self
{ }.bind(this)
var person = {
name: 'Jeff',
greet: function() {
console.log('Now greeting ' + this.name);
var that = this;
setTimeout(function() {
console.log('Hello, ' + that.name + '!');
}, 3000);
}
};
ES5:
var person = {
name: 'Jeff',
greet: function() {
console.log('Now greeting ' + this.name);
setTimeout(function() {
console.log('Hello, ' + this.name + '!');
}.bind(this), 3000);
}
};
ES5:
let person = {
name: 'Jeff',
greet: function() {
console.log(`Greeting ${this.name}`);
setTimeout(() => {
console.log(`Hello, ${this.name}!`);
}, 3000);
}
};
ES6:
putting it to use
compatibility
http://kangax.github.io
/compat-table/es6
bit.ly/es6-compat
$ npm install --save-dev babel-cli
$ npm install --save-dev babel-preset-es2015
$ echo '{ "presets": ["es2015"] }' > .babelrc
$ babel mySource.js -o myTranspiled.js
flexibility
flexibility
babeljs.io/repl
not all-or-nothing
N E X T S T E P S
es6 is an evolution
evolve
@ j e f f r e y S t r a u s s
# d e v E S 6
jeff@aranasoft.com
Adding ES6 to Your Developer Toolbox
Adding ES6 to Your Developer Toolbox
Adding ES6 to Your Developer Toolbox
Adding ES6 to Your Developer Toolbox
Adding ES6 to Your Developer Toolbox
Adding ES6 to Your Developer Toolbox
Adding ES6 to Your Developer Toolbox
Adding ES6 to Your Developer Toolbox
Adding ES6 to Your Developer Toolbox
Adding ES6 to Your Developer Toolbox
Adding ES6 to Your Developer Toolbox
Adding ES6 to Your Developer Toolbox
Adding ES6 to Your Developer Toolbox
Adding ES6 to Your Developer Toolbox
Adding ES6 to Your Developer Toolbox
Adding ES6 to Your Developer Toolbox
Adding ES6 to Your Developer Toolbox
Adding ES6 to Your Developer Toolbox
Adding ES6 to Your Developer Toolbox
Adding ES6 to Your Developer Toolbox
Adding ES6 to Your Developer Toolbox
Adding ES6 to Your Developer Toolbox

More Related Content

What's hot

5 Tips for Better JavaScript
5 Tips for Better JavaScript5 Tips for Better JavaScript
5 Tips for Better JavaScriptTodd Anglin
 
The Beauty Of Java Script V5a
The Beauty Of Java Script V5aThe Beauty Of Java Script V5a
The Beauty Of Java Script V5arajivmordani
 
Turn your spaghetti code into ravioli with JavaScript modules
Turn your spaghetti code into ravioli with JavaScript modulesTurn your spaghetti code into ravioli with JavaScript modules
Turn your spaghetti code into ravioli with JavaScript modulesjerryorr
 
Node.js for PHP developers
Node.js for PHP developersNode.js for PHP developers
Node.js for PHP developersAndrew Eddie
 
Coffeescript: No really, it's just Javascript
Coffeescript: No really, it's just JavascriptCoffeescript: No really, it's just Javascript
Coffeescript: No really, it's just JavascriptBrian Mann
 
CoffeeScript
CoffeeScriptCoffeeScript
CoffeeScriptEddie Kao
 
Introduction to CoffeeScript
Introduction to CoffeeScriptIntroduction to CoffeeScript
Introduction to CoffeeScriptStalin Thangaraj
 
Yii, frameworks and where PHP is heading to
Yii, frameworks and where PHP is heading toYii, frameworks and where PHP is heading to
Yii, frameworks and where PHP is heading toAlexander Makarov
 
Your Library Sucks, and why you should use it.
Your Library Sucks, and why you should use it.Your Library Sucks, and why you should use it.
Your Library Sucks, and why you should use it.Peter Higgins
 
JRuby, Ruby, Rails and You on the Cloud
JRuby, Ruby, Rails and You on the CloudJRuby, Ruby, Rails and You on the Cloud
JRuby, Ruby, Rails and You on the CloudHiro Asari
 
JS Level Up: Prototypes
JS Level Up: PrototypesJS Level Up: Prototypes
JS Level Up: PrototypesVernon Kesner
 
Best Practices in Plugin Development (WordCamp Seattle)
Best Practices in Plugin Development (WordCamp Seattle)Best Practices in Plugin Development (WordCamp Seattle)
Best Practices in Plugin Development (WordCamp Seattle)andrewnacin
 
Spring into rails
Spring into railsSpring into rails
Spring into railsHiro Asari
 
RubyMotion
RubyMotionRubyMotion
RubyMotionMark
 
Workshop 4: NodeJS. Express Framework & MongoDB.
Workshop 4: NodeJS. Express Framework & MongoDB.Workshop 4: NodeJS. Express Framework & MongoDB.
Workshop 4: NodeJS. Express Framework & MongoDB.Visual Engineering
 

What's hot (20)

5 Tips for Better JavaScript
5 Tips for Better JavaScript5 Tips for Better JavaScript
5 Tips for Better JavaScript
 
The Beauty of Java Script
The Beauty of Java ScriptThe Beauty of Java Script
The Beauty of Java Script
 
The Beauty Of Java Script V5a
The Beauty Of Java Script V5aThe Beauty Of Java Script V5a
The Beauty Of Java Script V5a
 
Turn your spaghetti code into ravioli with JavaScript modules
Turn your spaghetti code into ravioli with JavaScript modulesTurn your spaghetti code into ravioli with JavaScript modules
Turn your spaghetti code into ravioli with JavaScript modules
 
Node.js for PHP developers
Node.js for PHP developersNode.js for PHP developers
Node.js for PHP developers
 
Coffeescript: No really, it's just Javascript
Coffeescript: No really, it's just JavascriptCoffeescript: No really, it's just Javascript
Coffeescript: No really, it's just Javascript
 
CoffeeScript
CoffeeScriptCoffeeScript
CoffeeScript
 
Introduction to CoffeeScript
Introduction to CoffeeScriptIntroduction to CoffeeScript
Introduction to CoffeeScript
 
Yii, frameworks and where PHP is heading to
Yii, frameworks and where PHP is heading toYii, frameworks and where PHP is heading to
Yii, frameworks and where PHP is heading to
 
Your Library Sucks, and why you should use it.
Your Library Sucks, and why you should use it.Your Library Sucks, and why you should use it.
Your Library Sucks, and why you should use it.
 
JRuby, Ruby, Rails and You on the Cloud
JRuby, Ruby, Rails and You on the CloudJRuby, Ruby, Rails and You on the Cloud
JRuby, Ruby, Rails and You on the Cloud
 
JS Level Up: Prototypes
JS Level Up: PrototypesJS Level Up: Prototypes
JS Level Up: Prototypes
 
Best Practices in Plugin Development (WordCamp Seattle)
Best Practices in Plugin Development (WordCamp Seattle)Best Practices in Plugin Development (WordCamp Seattle)
Best Practices in Plugin Development (WordCamp Seattle)
 
Sprockets
SprocketsSprockets
Sprockets
 
Spring into rails
Spring into railsSpring into rails
Spring into rails
 
Ruby
RubyRuby
Ruby
 
Pi
PiPi
Pi
 
RubyMotion
RubyMotionRubyMotion
RubyMotion
 
Workshop 4: NodeJS. Express Framework & MongoDB.
Workshop 4: NodeJS. Express Framework & MongoDB.Workshop 4: NodeJS. Express Framework & MongoDB.
Workshop 4: NodeJS. Express Framework & MongoDB.
 
Django Heresies
Django HeresiesDjango Heresies
Django Heresies
 

Similar to Adding ES6 to Your Developer Toolbox

Impress Your Friends with EcmaScript 2015
Impress Your Friends with EcmaScript 2015Impress Your Friends with EcmaScript 2015
Impress Your Friends with EcmaScript 2015Lukas Ruebbelke
 
Jsphp 110312161301-phpapp02
Jsphp 110312161301-phpapp02Jsphp 110312161301-phpapp02
Jsphp 110312161301-phpapp02Seri Moth
 
ECMAScript 6 new features
ECMAScript 6 new featuresECMAScript 6 new features
ECMAScript 6 new featuresGephenSG
 
JavaScript for PHP developers
JavaScript for PHP developersJavaScript for PHP developers
JavaScript for PHP developersStoyan Stefanov
 
Essentials and Impactful Features of ES6
Essentials and Impactful Features of ES6Essentials and Impactful Features of ES6
Essentials and Impactful Features of ES6Riza Fahmi
 
ECMAScript2015
ECMAScript2015ECMAScript2015
ECMAScript2015qmmr
 
ES6 and AngularAMD
ES6 and AngularAMDES6 and AngularAMD
ES6 and AngularAMDdhaval10690
 
An Intro To ES6
An Intro To ES6An Intro To ES6
An Intro To ES6FITC
 
Short intro to ES6 Features
Short intro to ES6 FeaturesShort intro to ES6 Features
Short intro to ES6 FeaturesNaing Lin Aung
 
How to write code you won't hate tomorrow
How to write code you won't hate tomorrowHow to write code you won't hate tomorrow
How to write code you won't hate tomorrowPete McFarlane
 
JavaScript - new features in ECMAScript 6
JavaScript - new features in ECMAScript 6JavaScript - new features in ECMAScript 6
JavaScript - new features in ECMAScript 6Solution4Future
 
ES6 PPT FOR 2016
ES6 PPT FOR 2016ES6 PPT FOR 2016
ES6 PPT FOR 2016Manoj Kumar
 

Similar to Adding ES6 to Your Developer Toolbox (20)

Impress Your Friends with EcmaScript 2015
Impress Your Friends with EcmaScript 2015Impress Your Friends with EcmaScript 2015
Impress Your Friends with EcmaScript 2015
 
Jsphp 110312161301-phpapp02
Jsphp 110312161301-phpapp02Jsphp 110312161301-phpapp02
Jsphp 110312161301-phpapp02
 
Es6 hackathon
Es6 hackathonEs6 hackathon
Es6 hackathon
 
ECMAScript 6 new features
ECMAScript 6 new featuresECMAScript 6 new features
ECMAScript 6 new features
 
JavaScript for PHP developers
JavaScript for PHP developersJavaScript for PHP developers
JavaScript for PHP developers
 
Essentials and Impactful Features of ES6
Essentials and Impactful Features of ES6Essentials and Impactful Features of ES6
Essentials and Impactful Features of ES6
 
ES6: Features + Rails
ES6: Features + RailsES6: Features + Rails
ES6: Features + Rails
 
ECMAScript2015
ECMAScript2015ECMAScript2015
ECMAScript2015
 
Workshop 10: ECMAScript 6
Workshop 10: ECMAScript 6Workshop 10: ECMAScript 6
Workshop 10: ECMAScript 6
 
ES6 and AngularAMD
ES6 and AngularAMDES6 and AngularAMD
ES6 and AngularAMD
 
An Intro To ES6
An Intro To ES6An Intro To ES6
An Intro To ES6
 
Short intro to ES6 Features
Short intro to ES6 FeaturesShort intro to ES6 Features
Short intro to ES6 Features
 
How to write code you won't hate tomorrow
How to write code you won't hate tomorrowHow to write code you won't hate tomorrow
How to write code you won't hate tomorrow
 
JavaScript - new features in ECMAScript 6
JavaScript - new features in ECMAScript 6JavaScript - new features in ECMAScript 6
JavaScript - new features in ECMAScript 6
 
CoffeeScript
CoffeeScriptCoffeeScript
CoffeeScript
 
ES6 PPT FOR 2016
ES6 PPT FOR 2016ES6 PPT FOR 2016
ES6 PPT FOR 2016
 
"let ECMAScript = 6"
"let ECMAScript = 6" "let ECMAScript = 6"
"let ECMAScript = 6"
 
ES6: The future is now
ES6: The future is nowES6: The future is now
ES6: The future is now
 
PHP PPT FILE
PHP PPT FILEPHP PPT FILE
PHP PPT FILE
 
Internal workshop es6_2015
Internal workshop es6_2015Internal workshop es6_2015
Internal workshop es6_2015
 

Recently uploaded

Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...Nitya salvi
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnAmarnathKambale
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...Shane Coughlan
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech studentsHimanshiGarg82
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...masabamasaba
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrandmasabamasaba
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfonteinmasabamasaba
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfkalichargn70th171
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park masabamasaba
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrainmasabamasaba
 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfproinshot.com
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 

Recently uploaded (20)

Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdf
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 

Adding ES6 to Your Developer Toolbox

  • 1. #devES6 A D D I N G E S 6 T O YO U R T O O L B OX @ j e f f r e y s t r a u s s
  • 2. ”ES6? That's the new JavaScript! ~ mr. developer
  • 5.
  • 6.
  • 12. var greeting = "Hello, " + user.first + " " + user.last + "!"; var url = "www.mysite.com" + "/foo/" + fooId + "/bar/" + barId; ES5:
  • 15. var greeting = "Hello, " + user.first + " " + user.last + "!"; var url = "www.mysite.com" + "/foo/" + fooId + "/bar/" + barId; ES5: var greeting = `Hello, ${user.first} ${user.last}!`; var url = `www.mysite.com/foo/${fooId}/bar/${barId}/`; ES6:
  • 17. var poem = "so much dependsn" + "uponnn" + "a red wheeln" + "barrownn" + "glazed with rainn" + "waternn" + "beside the whiten" + "chickens"; ES5:
  • 18. var poem = `so much depends upon a red wheel barrow glazed with rain water beside the white chickens`; ES6:
  • 20. var drawRect = function(width, height, color) { width = width || 1; height = height || 1; color = color || 'orange'; /* draw stuff */ return width * height; }; ES5:
  • 21. var drawRect = function(width, height, color) { width = width || 1; height = height || 1; color = color || 'orange'; /* draw stuff */ return width * height; }; drawRect(); // draws orange; returns 1 drawRect(0, 4, 'blue'); // draws blue, but returns 4 ES5:
  • 22. var drawRect = function(width=1, height=1, color='orange') { /* draw stuff */ return width * height; }; ES6:
  • 23. var drawRect = function(width=1, height=1, color='orange') { /* draw stuff */ return width * height; }; drawRect(0, 4, 'blue'); // returns 0 ES6:
  • 26. var myStringArray = new Array("blue"); // ["blue"] var myBoolArray = new Array(false); // [false] var myIntArray = new Array(2); // [undefined x 2] ES5:
  • 27. var myIntArray = new Array(2); // [undefined x 2] ES5: var fixedIntArray = Array.of(2); // [2] ES6:
  • 28. function getArgsArray() { return Array.prototype.slice.apply(arguments); }; getArgsArray('foo', true, 42); // ['foo', true, 42] ES5:
  • 29. function getArgsArray() { return Array.prototype.slice.apply(arguments); }; getArgsArray('foo', true, 42); // ['foo', true, 42] ES5: function getArgsArray() { return Array.from(arguments); }; getArgsArray('foo', true, 42); // ['foo', true, 42] ES6:
  • 32. var body = ['head', 'shoulders', 'knees', 'toes']; for (var i = 0; i < body.length; i++) { tap(body[i]) }; ES5:
  • 33. var body = ['head', 'shoulders', 'knees', 'toes']; for (var i = 0; i < body.length; i++) { tap(body[i]) }; ES5: var body = ['head', 'shoulders', 'knees', 'toes']; for (var part of body) { tap(part); } ES6:
  • 34. var alphabet = 'abcdefg'; for (var letter of alphabet) { sing(letter); } ES6: E
  • 36. var alphabet = 'abcdefg'; function sing() { console.log(arguments); } sing(...alphabet); // ["a", "b", "c", "d", "e", "f", "g"] ES6: E
  • 38. [ ] = fooArray;
  • 39. { } = fooObject;
  • 40. var x, y, z, coords; coords = [29, 22, 37]; x = coords[0]; y = coords[1]; z = coords[2]; ES5:
  • 41. var x, y, z, coords; coords = [29, 22, 37]; x = coords[0]; y = coords[1]; z = coords[2]; ES5: var coords = [29, 22, 37]; var [x, y, z] = coords; ES6:
  • 42. var x, y, z, coords; coords = [29, 22, 37]; x = coords[0]; y = coords[1]; z = coords[2]; ES5: var coords = [29, 22, 37]; var [x, y, z] = coords; var [foo, , bar] = coords; ES6:
  • 43. var user, email, display; user = { name: 'Jeff', email: 'jeff@aranasoft.com' }; email = user.email; display = user.name; ES5:
  • 44. var user, email, display; user = { name: 'Jeff', email: 'jeff@aranasoft.com' }; email = user.email; display = user.name; ES5: var user = { name: 'Jeff', email: 'jeff@aranasoft.com' }; var {email} = user; var {name: display} = user; ES6:
  • 46. var temp = x; x = y; y = temp; ES5: [y, x] = [x, y]; ES6:
  • 48. var drawRect = function(width=1, height=1, color='orange') { /* draw stuff */ return {area: width * height, hex: getHex(color)}; }; ES6:
  • 49. var drawRect = function(width=1, height=1, color='orange') { /* draw stuff */ return {area: width * height, hex: getHex(color)}; }; var {area, hex: rgb} = drawRect(4, 6, 'blue'); // area == 24, rgb == '#0000FF' ES6:
  • 53. var foo = function(bar) { if (bar) { var message = 'Hello!'; // declared here alert(message); } return message; // still in scope here }; ES5:
  • 55. var foo = function(bar) { if (bar) { (function() { var message = 'Hello!'; // declared here alert(message); })(); } return message; // ReferenceError: message is not defined }; ES5:
  • 57. // assume links has an array of DOM elements for (var i = 0; i < links.length; i++) { links[i].onclick = function() { console.log(i); }; } // whoops! all links log the max index! ES5:
  • 58. // assume links has an array of DOM elements for (var i = 0; i < links.length; i++) { links[i].onclick = (function(x) { return function() { console.log(x); // preserved context }; })(i); } // clicking links gives the correct result ES5:
  • 60. function foo() { x = 10; y = 20; var x, y; var area = x * y; return area; }; ES5:
  • 61. function foo() { var x, y; var area; x = 10; y = 20; area = x * y; return area; }; ES5:
  • 62. function foo() { var x, y; // yikes! area may be in the global scope! x = 10; y = 20; area = x * y; return area; }; ES5:
  • 63. let
  • 64. function(bar) { if (bar) { var message = 'Hello!'; // declared here alert(message); } return message; // still in scope here }; ES5:
  • 65. function(bar) { if (bar) { let message = 'Hello!'; // declared here alert(message); } return message; // ReferenceError: message is not defined }; ES6:
  • 66. // assume links has an array of DOM elements for (var i = 0; i < links.length; i++) { links[i].onclick = function() { console.log(i); }; }; // whoops! all links log the max index! ES5:
  • 67. // assume links has an array of DOM elements for (let i = 0; i < links.length; i++) { links[i].onclick = function() { console.log(i); }; }; // all better, with no IIFE or extra closure! ES6:
  • 69. function foo() { x = 10; // ReferenceError: x is not defined y = 20; let x, y; let area = x * y; return area; }; ES6:
  • 72. (args) => { statements }
  • 73. function logSum(x, y) { var sum = x + y; console.log(sum); } ES5:
  • 74. function logSum(x, y) { var sum = x + y; console.log(sum); } ES5: let logSum = (a, b) => { let sum = a + b; console.log(sum); }; ES6:
  • 76. function sum(x, y) { return x + y; } ES5: let sum = (x, y) => x + y; ES6:
  • 77. let speak = () => 'Hello!'; let wrappedMethod = (arg) => innerMethod(arg); let announce = (name) => `Announcing ${name}!`; let getUser = () => ( { id: 42, name: 'Jeff' } ); ES6:
  • 78. let speak = () => 'Hello!'; let wrappedMethod = (arg) => innerMethod(arg); let announce = name => `Announcing ${name}!`; let getUser = () => ( { id: 42, name: 'Jeff' } ); ES6:
  • 79. let speak = () => 'Hello!'; let wrappedMethod = (arg) => innerMethod(arg); let announce = (name) => `Announcing ${name}!`; let getUser = () => ( { id: 42, name: 'Jeff' } ); ES6:
  • 81. var person = { name: 'Jeff', greet: function() { console.log('Now greeting ' + this.name); setTimeout(function() { console.log('Hello, ' + this.name + '!'); }, 3000); } }; ES5:
  • 82. var person = { name: 'Jeff', greet: function() { console.log('Now greeting ' + this.name); setTimeout(function() { console.log('Hello, ' + this.name + '!'); }, 3000); } }; person.greet(); // Now greeting Jeff ES5:
  • 83. var person = { name: 'Jeff', greet: function() { console.log('Now greeting ' + this.name); setTimeout(function() { console.log('Hello, ' + this.name + '!'); }, 3000); } }; person.greet(); // Now greeting Jeff // Hello, ! ES5:
  • 86. var person = { name: 'Jeff', greet: function() { console.log('Now greeting ' + this.name); var that = this; setTimeout(function() { console.log('Hello, ' + that.name + '!'); }, 3000); } }; ES5:
  • 87. var person = { name: 'Jeff', greet: function() { console.log('Now greeting ' + this.name); setTimeout(function() { console.log('Hello, ' + this.name + '!'); }.bind(this), 3000); } }; ES5:
  • 88. let person = { name: 'Jeff', greet: function() { console.log(`Greeting ${this.name}`); setTimeout(() => { console.log(`Hello, ${this.name}!`); }, 3000); } }; ES6:
  • 93.
  • 94.
  • 95. $ npm install --save-dev babel-cli $ npm install --save-dev babel-preset-es2015 $ echo '{ "presets": ["es2015"] }' > .babelrc $ babel mySource.js -o myTranspiled.js
  • 99. not all-or-nothing N E X T S T E P S
  • 100. es6 is an evolution
  • 101. evolve
  • 102. @ j e f f r e y S t r a u s s # d e v E S 6 jeff@aranasoft.com

Editor's Notes

  1. ECMAScript (ECMA-262) is a standard or specification. It is not, in and of itself, a programming language.
  2. JavaScript is a programming language, and just one implementation—admittedly, by far the most widespread—of the ECAMScript specification.
  3. But that does not mean that every feature of ECMAScript is implemented properly in JavaScript; nor does it mean that every JavaScript engine, across all browsers, platforms, and runtimes, has full feature parity. You have seen this yourself in your own experiences with different browsers having different compatibility.
  4. Specifically, "ES6" is the 6th Edition of the ECMA-262 standard. It has also been called by other names, including ECMAScript 2015 and ES Harmony.
  5. Why be so pedantic about the distinction between specification and implementation? This is NOT a new language. It is an evolution and growth of the existing standard. Much of it is stylistic. Your existing ES5 code and patterns will NOT break. It is not like changing from SQL to MongoDB, or even Angular to React. So relax.
  6. In reviewing this talk, if you find only a couple features that will improve your productivity or your codebase, then great! Use those. You don't have to adopt the whole feature set, any more than you use every single feature of C#, Java, or Ruby, today.
  7. Let's begin at the beginning...
  8. This will look a bit familiar to you if you come from Ruby or if you have spent any amount of time with C# 6 and .NET 4.6.
  9. Most people have written code like this, concatenating fragments to build strings. It is annoying and error-prone.
  10. ES6 introduces backtick-enclosed strings, which signify to the runtime that the strings should be interpolated.
  11. Within an interpolated string, you may include tokens. These may be simply variables; or they can contain any valid expression that may be evaluated and returns a resulting value.
  12. Williams, William Carlos, "XXII", Spring and All (New York: Contact Editions / Dijon: Maurice Darantière, 1923).
  13. NOTE: Be mindful of your spacing, because *all* whitespace is now respected, including any indentation on lines 2 and below. Williams, William Carlos, "XXII", Spring and All (New York: Contact Editions / Dijon: Maurice Darantière, 1923).
  14. This is a common way of handling "default" argument values today. Using the logical OR operator, if a value is not passed in, then the function will resort to the backup.
  15. Second example fails because `0` is falsy. Lots of things in JavaScript coerce to false, including: • null • undefined • 0 • empty string ("")
  16. In ES6, we can assign default parameter values in much the same way we have seen in other languages.
  17. Using the available ES6 default parameters in the function definition, you can avoid the falsiness test altogether.
  18. ES6 adds a variety of new native collection types, such as: Maps, Sets, Typed Arrays, WeakSets, and WeakMaps. All of these are worth pursuing in independent research and have their unique use cases.
  19. The existing JavaScript Array constructor is overloaded. If you pass an integer argument, it treats it as a LENGTH and creates an array filled with undefined. Worse, if you pass it a floating-point numeric value, it throws an exception.
  20. Using ES6's Array.of(items) will create an Array and avoid this confusion.
  21. Using the built-in `arguments` collection in ES5 and earlier has some benefits. However, it is NOT a true array, but rather an array-like collection. It contains a length property and index-based access, but nothing else. Converting it to a real Array requires going through messy access to the Prototype.
  22. Array.from(collection) can take any array-like collection and return a new Array.
  23. These new Array functions will work with any "iterable" collection of values. The idea of the @iterable pattern is new in ES6. You can define your own iterator functions for your objects, if you like, or use any of the native types.
  24. Traditional `for` loops are cumbersome, and also have scope problems with the iterator/counter. The Array.forEach function is nice, but is available ONLY for true Arrays, not other array-like or iterable collections.
  25. The new for-of loop cycles through the collection's iterator, emitting each value until the collection is complete. Much like iterating over an IEnumerable in C#.
  26. The new loop—and a number of other features—are usable by ANY iterable object. Even things not ordinarily considered "collections," like strings.
  27. The "spread" operator has the effect of spinning through an entire iterable and "spreading" or "splatting" the whole thing out into a list of results all at once. This is similarly used, as a "rest" operator, in destructuring, which is discussed in the next section.
  28. In this example, the spread operator can be used to pass an unspecified set of arguments into a function. This would work even if the function definition has specific arguments. The spread result will be applied to those named parameters.
  29. Destructuring is using an array- or object-like pattern to extract only the portions of data that you want from a more complex object, and assigning the values to one or more variables.
  30. Notice in this slide and the next that the thing that looks like an "array" or "object literal" is actually the * l-value * on the left-hand side of the assignment operator. This should look unusual to you.
  31. The result is not a new array of [x, y, z], but rather three separate variables assigned the values at position 0, 1, and 2 of the coords array.
  32. You can use a pattern, as shown here, to take only the first and third elements from the array and ignore the one between them.
  33. The examples here scan the `user` object for properties called email and name. If they are found, the values are assigned to new variables. If not, then undefined is assigned. For `name`, the value in user.name is assigned to the new variable called `display`.
  34. No need for the use of a temporary variable to hold one value. They can be swapped using destructuring.
  35. Using destructuring, the function can return an array of values, or a complex object literal, and those multiple return values can be destructured and assigned to a new set of independent variables.
  36. Traditionally, in JavaScript, variables are not block-scoped, meaning they do not automatically go out of scope at a closing curly brace.
  37. Instead, JavaScript (and the ECMAScript standard) are function-scoped. All variables survive for the duration of the functions within which they are declared.
  38. The variable called message does not go out of scope until the END of the function foo. The IF block has nothing to do with its scope.
  39. One "traditional" workaround to limit scope is to utilize IIFEs in our code.
  40. Wrapping the block of code within a function limits scope. There are parentheses to signify to the parser that this is an expression and not a function declaration. Then, the second set of parentheses invokes the method immediately. Causes message to go out of scope when the anonymous IIFE has completed. However, this syntax is confusing, inelegant, and can seem arbitrary.
  41. IIFE is one kind of closure. Generally, closure is a special pattern that combines two things: a function, and the environment in which that function was created. Sometimes variables in the outer execution context continue to change prior to the closure's execution.
  42. This doesn't work. Each one, on click, shows the max index, not its own. This is because the variable `i` is scoped to the global (or parent) scope.
  43. This confusing code is using another kind of IIFE and closure. This time, the closure returns a new anonymous function, creating the variable `x` that closes over each iteration of `i` from the loop. This works, but again, it is confusing.
  44. This is legal in JavaScript, even though the variables are accessed seemingly before they are declared lexically in the code.
  45. The previous example is equivalent—and in fact executed—like this. The variable declarations are "hoisted" to the top of the enclosing function scope and are then accessible, but undefined, until initialized to a value.
  46. If you forget to ever declare the variable, it may become global, or at least will bubble up to the parent scope. Not desirable.
  47. Using let to declare your variables addresses all of these scope issues.
  48. Here is one of our examples from before.
  49. Changing to use `let` restricts the scope to the immediately surrounding block.
  50. Another example from before the previously required a complicated IIFE and closure to hold each copy of `i` for future execution.
  51. The temporal dead zone is a rather morbid name, but refers to the period between the opening of scope and the lexical declaration of a variable. While the parser is aware of the variables existing, the runtime acts as though it does not.
  52. If you use let to declare variables instead of var, then there is no hoisting. The variables may not be accessed until declared.
  53. Unlike CoffeeScript, there is only "fat arrow" and no "skinny arrow" syntax available.
  54. Note that arrow functions are always anonymous and so they must be assigned to variables, unless they are intended to be executed immediately or as anonymous callbacks.
  55. If your method has a single statement that returns a value, then a function expression is used instead of an enclosed code block.
  56. Notice the function expression in the ES6 example does not include the `return` keyword.
  57. Other examples. An empty argument list still requires empty parentheses. The return expression may use interpolated strings, or even other function calls.
  58. In the case of a single-parameter function, the parentheses (in this case, around `name`) are optional. However, for consistency, I prefer to use them.
  59. If your function expression returns an object literal, then surrounding the objection parentheses is REQUIRED, because otherwise the parser interprets the opening brace as the start of a code block.
  60. All arrow functions bind `this` lexically, meaning its `this` is based upon the surrounding scope in which it is physically contained within the code.
  61. Assume an object with a name property and a greet function.
  62. Calling greet immediately greets `this.name`, which is based on the surrounding object.
  63. After three seconds, a timeout callback is executed, but the callback is executed in the global execution context, and so `this` is not bound to `person` and `this.name` is lost.
  64. One way to fix this is assigning `this` to something like that, or self, or even _this.
  65. Another option is calling the Function prototype's bind() method to bind a specified value—in this case, the surrounding context's `this` to the inner function's `this`, no matter when it happens to be executed.
  66. Rather than doing either of these, using an ES6 arrow function for the callback binds `this` lexically, meaning it is ALWAYS bound to the `person` object in which the function is shown in the code.
  67. This site is a great resource for seeing the progress and compatibility status for features, not only in the current spec, but also in future anticipated feature sets and releases. However, it is VERY thorough and overwhelming. The next slide has a bit.ly for it.
  68. My favorite candidate for "worst and most overwhelming slide of all time"! Find current versions of this chart at: bit.ly/es6-compat
  69. Babel, with all of its many variations, is frequently called a transpiler. It converts your code that uses the new ES6 features into ES5-compliant JavaScript.
  70. Starting with v.6.0, you need to provide preset transform configuration to babel. You do not have to use the universal babel-preset-2015. There are individual components, so if you want to exclude `class` or `shorthand properties` or whatever, you are able to pick and choose.
  71. You are by no means limited to the command-line interface. Babel plugins exist for a tremendous variety of development environments, testing platforms, build tools, etc. There is likely a good fit for you.
  72. Chrome Canary is a GREAT developers' browser. It is not recommended for testing production deployment, but as a leading-edge beta browser, it has nearly complete compatibility with ES6 features, for testing how things work.
  73. The babel website has an easy-to-use REPL ("read-evaluate-print loop") for providing ES6 code and getting real-time transpiring to ES5 JavaScript.