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

DOCX
งานย่อยที่ 7
DOCX
การหาปริมาตรของปร ซึม
PDF
함수형사고 4장 열심히보다는현명하게
PPT
Stack queue
PPTX
Load-time Hacking using LD_PRELOAD
PDF
Show Us: SS7 Update
PDF
ESUG15: SS7 Update
PDF
Javascript basics
งานย่อยที่ 7
การหาปริมาตรของปร ซึม
함수형사고 4장 열심히보다는현명하게
Stack queue
Load-time Hacking using LD_PRELOAD
Show Us: SS7 Update
ESUG15: SS7 Update
Javascript basics

What's hot (16)

PDF
Go Says WAT?
PPTX
Grokking TechTalk #16: Maybe functor in javascript
PPTX
Stack using Linked List
PDF
Alex Troush - IEx Cheat Sheet
PDF
Python sqlite3 - flask
PDF
Exploring slides
PDF
Alex Troush - IEx Cheat Sheet. Guide to Win with IEx on your Day to Day Job
PPTX
Stack using Array
PPTX
Python modulesfinal
PDF
Functional Programming: An Introduction
PDF
Yurii Bodarev - Ecto DSL
PPT
StackArray stack3
PPTX
CONFidence 2015: DTrace + OSX = Fun - Andrzej Dyjak
PDF
bpftrace - Tracing Summit 2018
DOCX
Primer Punto
Go Says WAT?
Grokking TechTalk #16: Maybe functor in javascript
Stack using Linked List
Alex Troush - IEx Cheat Sheet
Python sqlite3 - flask
Exploring slides
Alex Troush - IEx Cheat Sheet. Guide to Win with IEx on your Day to Day Job
Stack using Array
Python modulesfinal
Functional Programming: An Introduction
Yurii Bodarev - Ecto DSL
StackArray stack3
CONFidence 2015: DTrace + OSX = Fun - Andrzej Dyjak
bpftrace - Tracing Summit 2018
Primer Punto
Ad

Similar to 6 new ES6 features (20)

PPTX
Getting started with ES6 : Future of javascript
PPTX
EcmaScript unchained
PDF
JavaScript in 2016
PPTX
JavaScript in 2016 (Codemotion Rome)
PPTX
ECMAScript 2015
PPTX
Getting started with ES6
PPTX
Introduction to es6
PDF
Impress Your Friends with EcmaScript 2015
PDF
Fitc whats new in es6 for web devs
PDF
Es.next
PDF
Es6 to es5
PDF
What's New in ES6 for Web Devs
PPTX
ES6: Features + Rails
PPTX
SenchaCon 2016: Learn the Top 10 Best ES2015 Features - Lee Boonstra
PPTX
Es6 day1
PDF
ECMAScript 6 and beyond
PDF
Effective ES6
PPTX
Javantura v3 - ES6 – Future Is Now – Nenad Pečanac
PPTX
ES6 - JavaCro 2016
PDF
ES6: The future is now
Getting started with ES6 : Future of javascript
EcmaScript unchained
JavaScript in 2016
JavaScript in 2016 (Codemotion Rome)
ECMAScript 2015
Getting started with ES6
Introduction to es6
Impress Your Friends with EcmaScript 2015
Fitc whats new in es6 for web devs
Es.next
Es6 to es5
What's New in ES6 for Web Devs
ES6: Features + Rails
SenchaCon 2016: Learn the Top 10 Best ES2015 Features - Lee Boonstra
Es6 day1
ECMAScript 6 and beyond
Effective ES6
Javantura v3 - ES6 – Future Is Now – Nenad Pečanac
ES6 - JavaCro 2016
ES6: The future is now
Ad

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/