SlideShare a Scribd company logo
6 New features in ES6
Kyle Dorman
Overview
1. Block Scope
2. Template Literals
3. Arrow Functions
4. Maps
5. Classes
6. Exporting/Importing Modules
Block Scope
function varTest() {
var x = 31;
if (true) {
var x = 71; // same var!
console.log(x); // 71
}
console.log(x); // 71
}
function letTest() {
let x = 31;
if (true) {
let x = 71; //different var!
console.log(x); // 71
}
console.log(x); // 31
}
Template Literals
function sayName (person) {
let tpl = `My name is ${person.name}.`;
console.log(tpl);
}
let john = {name: 'John Smith'};
sayName(john);
// My name is John Smith.
Arrow Functions
let x = [0,1,2];
x.map(x => console.log(x * x)); //arrow function
function Car() {
this.speed = 0;
setInterval(() => {
this.speed += 5; //this is from Car
console.log('now going: ' + this.speed);
}, 1000);
}
Maps
let x = new Map([[1, 'is a number key']]);
let today = new Date()
//anything can be a key
x.set(today.toString(), 111)
x.set(today, 222);
x.delete(today.toString());
x.size; // 2
x.has(today); // true
x.has(today.toString()); // false
Classes
class Car {
constructor(make) { //constructors!
this.make = make;
this.speed = 25;
}
printCurrentSpeed(){
console.log(this.make + ' is going ' + this.speed + '
mph.');
}
}
Classes - inheritance
class RaceCar extends Car { //inheritance
constructor(make, topSpeed) {
super(make); //call the parent constructor with super
this.topSpeed = topSpeed;
}
goFast(){
this.currentSpeed = this.topSpeed;
}
}
var stang = new RaceCar('Mustang', 150);
Design Decisions
● Default exports are favored
● Static module structure
● Support for both synchronous and
asynchronous loading
● Support for cyclic dependencies between
modules
Benefits of static module structure
1. faster lookup
2. variable checking
3. ready for macros
4. ready for types
Exporting Named Functions
//------ 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
// OR
//------ main.js ------
import * as lib from 'lib';
console.log(lib.square(11)); // 121
console.log(lib.diag(4, 3)); // 5
Exporting Default Modules
//------ myFunc.js ------
export default function () { ...
};
//------ main1.js ------
import myFunc from 'myFunc';
myFunc();
// or as a class:
//------ MyClass.js ------
export default class { ... };
//------ main2.js ------
import MyClass from 'MyClass';
let inst = new MyClass();
Other Export Options
const MY_CONST = ...;
function myFunc() {
...
}
export { MY_CONST, myFunc };
// OR
export { MY_CONST as THE_CONST, myFunc as theFunc };
Other Import Options
// Renaming: import named1 as myNamed1
import { named1 as myNamed1, named2 } from 'src/mylib';
// Importing the module as an object
// (with one property per named export)
import * as mylib from 'src/mylib';
// Only load the module, don't import anything
import 'src/mylib';
Importing Modules
System.import('some_module')
.then(some_module => {
// Use some_module
})
.catch(error => {
...
});
More System functions
System.module(source, options?);
// evaluates the JavaScript code in source to a module (which is
delivered asynchronously via a promise).
System.set(name, module);
//is for registering a module (e.g. one you have created via
System.module()).
System.define(name, source, options?);
//both evaluates the module code in source and registers the
result.
The future
● No more UMD
● New browser APIs become modules instead
of global variables or properties of navigator.
● No more objects-as-namespaces: i.e. Math,
JSON which serve as namespaces for
functions in ECMAScript 5
Not covered
● Generators
● Promises
● Sets
● Spread Operators
● Rest Parameters
● Default Parameters
● Destructed Assignment
Sources
● Blog: http://www.wintellect.com/devcenter/nstieglitz/5-
great-features-in-es6-harmony
● Modules in depth: http://www.2ality.com/2014/09/es6-
modules-final.html
● ES6 code: http://www.es6fiddle.net/
● ES6 -> ES5: http://babeljs.io/repl/

More Related Content

What's hot

Go Says WAT?
Go Says WAT?Go Says WAT?
Go Says WAT?
jonbodner
 
Grokking TechTalk #16: Maybe functor in javascript
Grokking TechTalk #16: Maybe functor in javascriptGrokking TechTalk #16: Maybe functor in javascript
Grokking TechTalk #16: Maybe functor in javascript
Grokking VN
 
Stack using Linked List
Stack using Linked ListStack using Linked List
Stack using Linked List
Sayantan Sur
 
Alex Troush - IEx Cheat Sheet
Alex Troush - IEx Cheat Sheet Alex Troush - IEx Cheat Sheet
Alex Troush - IEx Cheat Sheet
Elixir Club
 
Python sqlite3 - flask
Python   sqlite3 - flaskPython   sqlite3 - flask
Python sqlite3 - flask
Eueung Mulyana
 
Exploring slides
Exploring slidesExploring slides
Exploring slides
akaptur
 
Alex Troush - IEx Cheat Sheet. Guide to Win with IEx on your Day to Day Job
Alex Troush - IEx Cheat Sheet. Guide to Win with IEx on your Day to Day JobAlex Troush - IEx Cheat Sheet. Guide to Win with IEx on your Day to Day Job
Alex Troush - IEx Cheat Sheet. Guide to Win with IEx on your Day to Day Job
Elixir Club
 
week-15x
week-15xweek-15x
Stack using Array
Stack using ArrayStack using Array
Stack using Array
Sayantan Sur
 
Python modulesfinal
Python modulesfinalPython modulesfinal
Python modulesfinal
Saraswathi Murugan
 
Functional Programming: An Introduction
Functional Programming: An IntroductionFunctional Programming: An Introduction
Functional Programming: An Introduction
drewolson
 
Yurii Bodarev - Ecto DSL
Yurii Bodarev - Ecto DSLYurii Bodarev - Ecto DSL
Yurii Bodarev - Ecto DSL
Elixir Club
 
StackArray stack3
StackArray stack3StackArray stack3
StackArray stack3
Rajendran
 
CONFidence 2015: DTrace + OSX = Fun - Andrzej Dyjak
CONFidence 2015: DTrace + OSX = Fun - Andrzej Dyjak   CONFidence 2015: DTrace + OSX = Fun - Andrzej Dyjak
CONFidence 2015: DTrace + OSX = Fun - Andrzej Dyjak
PROIDEA
 
bpftrace - Tracing Summit 2018
bpftrace - Tracing Summit 2018bpftrace - Tracing Summit 2018
bpftrace - Tracing Summit 2018
AlastairRobertson9
 
Primer Punto
Primer PuntoPrimer Punto
Primer Punto
gustavo206
 

What's hot (16)

Go Says WAT?
Go Says WAT?Go Says WAT?
Go Says WAT?
 
Grokking TechTalk #16: Maybe functor in javascript
Grokking TechTalk #16: Maybe functor in javascriptGrokking TechTalk #16: Maybe functor in javascript
Grokking TechTalk #16: Maybe functor in javascript
 
Stack using Linked List
Stack using Linked ListStack using Linked List
Stack using Linked List
 
Alex Troush - IEx Cheat Sheet
Alex Troush - IEx Cheat Sheet Alex Troush - IEx Cheat Sheet
Alex Troush - IEx Cheat Sheet
 
Python sqlite3 - flask
Python   sqlite3 - flaskPython   sqlite3 - flask
Python sqlite3 - flask
 
Exploring slides
Exploring slidesExploring slides
Exploring slides
 
Alex Troush - IEx Cheat Sheet. Guide to Win with IEx on your Day to Day Job
Alex Troush - IEx Cheat Sheet. Guide to Win with IEx on your Day to Day JobAlex Troush - IEx Cheat Sheet. Guide to Win with IEx on your Day to Day Job
Alex Troush - IEx Cheat Sheet. Guide to Win with IEx on your Day to Day Job
 
week-15x
week-15xweek-15x
week-15x
 
Stack using Array
Stack using ArrayStack using Array
Stack using Array
 
Python modulesfinal
Python modulesfinalPython modulesfinal
Python modulesfinal
 
Functional Programming: An Introduction
Functional Programming: An IntroductionFunctional Programming: An Introduction
Functional Programming: An Introduction
 
Yurii Bodarev - Ecto DSL
Yurii Bodarev - Ecto DSLYurii Bodarev - Ecto DSL
Yurii Bodarev - Ecto DSL
 
StackArray stack3
StackArray stack3StackArray stack3
StackArray stack3
 
CONFidence 2015: DTrace + OSX = Fun - Andrzej Dyjak
CONFidence 2015: DTrace + OSX = Fun - Andrzej Dyjak   CONFidence 2015: DTrace + OSX = Fun - Andrzej Dyjak
CONFidence 2015: DTrace + OSX = Fun - Andrzej Dyjak
 
bpftrace - Tracing Summit 2018
bpftrace - Tracing Summit 2018bpftrace - Tracing Summit 2018
bpftrace - Tracing Summit 2018
 
Primer Punto
Primer PuntoPrimer Punto
Primer Punto
 

Similar to 6 new ES6 features

Introduction to ES6 with Tommy Cresine
Introduction to ES6 with Tommy CresineIntroduction to ES6 with Tommy Cresine
Introduction to ES6 with Tommy Cresine
Movel
 
Living in harmony - a brief into to ES6
Living in harmony - a brief into to ES6Living in harmony - a brief into to ES6
Living in harmony - a brief into to ES6
Richard Leland
 
Workshop 10: ECMAScript 6
Workshop 10: ECMAScript 6Workshop 10: ECMAScript 6
Workshop 10: ECMAScript 6
Visual Engineering
 
ECMAScript 6
ECMAScript 6ECMAScript 6
ECMAScript 6
WebF
 
Explaining ES6: JavaScript History and What is to Come
Explaining ES6: JavaScript History and What is to ComeExplaining ES6: JavaScript History and What is to Come
Explaining ES6: JavaScript History and What is to Come
Cory Forsyth
 
JavaScript - Agora nervoso
JavaScript - Agora nervosoJavaScript - Agora nervoso
JavaScript - Agora nervoso
Luis Vendrame
 
Javascript
JavascriptJavascript
Javascript
Vlad Ifrim
 
Academy PRO: ES2015
Academy PRO: ES2015Academy PRO: ES2015
Academy PRO: ES2015
Binary Studio
 
ES6 PPT FOR 2016
ES6 PPT FOR 2016ES6 PPT FOR 2016
ES6 PPT FOR 2016
Manoj Kumar
 
FalsyValues. Dmitry Soshnikov - ECMAScript 6
FalsyValues. Dmitry Soshnikov - ECMAScript 6FalsyValues. Dmitry Soshnikov - ECMAScript 6
FalsyValues. Dmitry Soshnikov - ECMAScript 6
Dmitry Soshnikov
 
ES6: Features + Rails
ES6: Features + RailsES6: Features + Rails
ES6: Features + Rails
Santosh Wadghule
 
Data structuresUsing java language and develop a prot.pdf
Data structuresUsing java language and develop a prot.pdfData structuresUsing java language and develop a prot.pdf
Data structuresUsing java language and develop a prot.pdf
armyshoes
 
Thinking Functionally with JavaScript
Thinking Functionally with JavaScriptThinking Functionally with JavaScript
Thinking Functionally with JavaScript
Luis Atencio
 
ECMAScript 6
ECMAScript 6ECMAScript 6
ECMAScript 6
Piotr Lewandowski
 
ECMAScript 6 new features
ECMAScript 6 new featuresECMAScript 6 new features
ECMAScript 6 new features
GephenSG
 
HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6
HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6
HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6
Dmitry Soshnikov
 
React Native One Day
React Native One DayReact Native One Day
React Native One Day
Troy Miles
 
EcmaScript unchained
EcmaScript unchainedEcmaScript unchained
EcmaScript unchained
Eduard Tomàs
 
ESCMAScript 6: Get Ready For The Future. Now
ESCMAScript 6: Get Ready For The Future. NowESCMAScript 6: Get Ready For The Future. Now
ESCMAScript 6: Get Ready For The Future. Now
Krzysztof Szafranek
 
ECMAScript 6 and beyond
ECMAScript 6 and beyondECMAScript 6 and beyond
ECMAScript 6 and beyond
Francis Johny
 

Similar to 6 new ES6 features (20)

Introduction to ES6 with Tommy Cresine
Introduction to ES6 with Tommy CresineIntroduction to ES6 with Tommy Cresine
Introduction to ES6 with Tommy Cresine
 
Living in harmony - a brief into to ES6
Living in harmony - a brief into to ES6Living in harmony - a brief into to ES6
Living in harmony - a brief into to ES6
 
Workshop 10: ECMAScript 6
Workshop 10: ECMAScript 6Workshop 10: ECMAScript 6
Workshop 10: ECMAScript 6
 
ECMAScript 6
ECMAScript 6ECMAScript 6
ECMAScript 6
 
Explaining ES6: JavaScript History and What is to Come
Explaining ES6: JavaScript History and What is to ComeExplaining ES6: JavaScript History and What is to Come
Explaining ES6: JavaScript History and What is to Come
 
JavaScript - Agora nervoso
JavaScript - Agora nervosoJavaScript - Agora nervoso
JavaScript - Agora nervoso
 
Javascript
JavascriptJavascript
Javascript
 
Academy PRO: ES2015
Academy PRO: ES2015Academy PRO: ES2015
Academy PRO: ES2015
 
ES6 PPT FOR 2016
ES6 PPT FOR 2016ES6 PPT FOR 2016
ES6 PPT FOR 2016
 
FalsyValues. Dmitry Soshnikov - ECMAScript 6
FalsyValues. Dmitry Soshnikov - ECMAScript 6FalsyValues. Dmitry Soshnikov - ECMAScript 6
FalsyValues. Dmitry Soshnikov - ECMAScript 6
 
ES6: Features + Rails
ES6: Features + RailsES6: Features + Rails
ES6: Features + Rails
 
Data structuresUsing java language and develop a prot.pdf
Data structuresUsing java language and develop a prot.pdfData structuresUsing java language and develop a prot.pdf
Data structuresUsing java language and develop a prot.pdf
 
Thinking Functionally with JavaScript
Thinking Functionally with JavaScriptThinking Functionally with JavaScript
Thinking Functionally with JavaScript
 
ECMAScript 6
ECMAScript 6ECMAScript 6
ECMAScript 6
 
ECMAScript 6 new features
ECMAScript 6 new featuresECMAScript 6 new features
ECMAScript 6 new features
 
HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6
HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6
HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6
 
React Native One Day
React Native One DayReact Native One Day
React Native One Day
 
EcmaScript unchained
EcmaScript unchainedEcmaScript unchained
EcmaScript unchained
 
ESCMAScript 6: Get Ready For The Future. Now
ESCMAScript 6: Get Ready For The Future. NowESCMAScript 6: Get Ready For The Future. Now
ESCMAScript 6: Get Ready For The Future. Now
 
ECMAScript 6 and beyond
ECMAScript 6 and beyondECMAScript 6 and beyond
ECMAScript 6 and beyond
 

6 new ES6 features

  • 1. 6 New features in ES6 Kyle Dorman
  • 2. Overview 1. Block Scope 2. Template Literals 3. Arrow Functions 4. Maps 5. Classes 6. Exporting/Importing Modules
  • 3. Block Scope function varTest() { var x = 31; if (true) { var x = 71; // same var! console.log(x); // 71 } console.log(x); // 71 } function letTest() { let x = 31; if (true) { let x = 71; //different var! console.log(x); // 71 } console.log(x); // 31 }
  • 4. Template Literals function sayName (person) { let tpl = `My name is ${person.name}.`; console.log(tpl); } let john = {name: 'John Smith'}; sayName(john); // My name is John Smith.
  • 5. Arrow Functions let x = [0,1,2]; x.map(x => console.log(x * x)); //arrow function function Car() { this.speed = 0; setInterval(() => { this.speed += 5; //this is from Car console.log('now going: ' + this.speed); }, 1000); }
  • 6. Maps let x = new Map([[1, 'is a number key']]); let today = new Date() //anything can be a key x.set(today.toString(), 111) x.set(today, 222); x.delete(today.toString()); x.size; // 2 x.has(today); // true x.has(today.toString()); // false
  • 7. Classes class Car { constructor(make) { //constructors! this.make = make; this.speed = 25; } printCurrentSpeed(){ console.log(this.make + ' is going ' + this.speed + ' mph.'); } }
  • 8. Classes - inheritance class RaceCar extends Car { //inheritance constructor(make, topSpeed) { super(make); //call the parent constructor with super this.topSpeed = topSpeed; } goFast(){ this.currentSpeed = this.topSpeed; } } var stang = new RaceCar('Mustang', 150);
  • 9. Design Decisions ● Default exports are favored ● Static module structure ● Support for both synchronous and asynchronous loading ● Support for cyclic dependencies between modules
  • 10. Benefits of static module structure 1. faster lookup 2. variable checking 3. ready for macros 4. ready for types
  • 11. Exporting Named Functions //------ 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 // OR //------ main.js ------ import * as lib from 'lib'; console.log(lib.square(11)); // 121 console.log(lib.diag(4, 3)); // 5
  • 12. Exporting Default Modules //------ myFunc.js ------ export default function () { ... }; //------ main1.js ------ import myFunc from 'myFunc'; myFunc(); // or as a class: //------ MyClass.js ------ export default class { ... }; //------ main2.js ------ import MyClass from 'MyClass'; let inst = new MyClass();
  • 13. Other Export Options const MY_CONST = ...; function myFunc() { ... } export { MY_CONST, myFunc }; // OR export { MY_CONST as THE_CONST, myFunc as theFunc };
  • 14. Other Import Options // Renaming: import named1 as myNamed1 import { named1 as myNamed1, named2 } from 'src/mylib'; // Importing the module as an object // (with one property per named export) import * as mylib from 'src/mylib'; // Only load the module, don't import anything import 'src/mylib';
  • 15. Importing Modules System.import('some_module') .then(some_module => { // Use some_module }) .catch(error => { ... });
  • 16. More System functions System.module(source, options?); // evaluates the JavaScript code in source to a module (which is delivered asynchronously via a promise). System.set(name, module); //is for registering a module (e.g. one you have created via System.module()). System.define(name, source, options?); //both evaluates the module code in source and registers the result.
  • 17. The future ● No more UMD ● New browser APIs become modules instead of global variables or properties of navigator. ● No more objects-as-namespaces: i.e. Math, JSON which serve as namespaces for functions in ECMAScript 5
  • 18. Not covered ● Generators ● Promises ● Sets ● Spread Operators ● Rest Parameters ● Default Parameters ● Destructed Assignment
  • 19. Sources ● Blog: http://www.wintellect.com/devcenter/nstieglitz/5- great-features-in-es6-harmony ● Modules in depth: http://www.2ality.com/2014/09/es6- modules-final.html ● ES6 code: http://www.es6fiddle.net/ ● ES6 -> ES5: http://babeljs.io/repl/