ECMAScript 6 (ES6)
@gatukgl
#CodeMania100
#ProteusTechnologies #GirlsWhoDev
SUDARAT
CHATTANON
GATUK
Developer
Proteus Technologies
Organiser
Girls Who Dev
ECMAScript 6
ES6
ES2015
ECMAScript 2015
ES6 Compatibility Table
source: http://kangax.github.io/compat-table/es6/
Who will be our hero?
source: http://www.continue-play.com/2015/08/17/baymax-from-big-hero-6-will-be-in-kingdom-hearts-3/
ES6 Transpiler
ES6 ES5
ES6 Features
• const and let • Destructuring Assignment
• Template Literals • Multi-line Strings
• Arrow Function • Rest Parameters
• Module support • Promise
• Iterators • Class
• etc...
7
ES6 Features
• const and let • Destructuring Assignment
• Template Literals • Multi-line Strings
• Arrow Function • Rest Parameters
• Module support • Promise
• Iterators • Class
• etc...
Block Scoping
Source: https://www.youtube.com/watch?v=sjyJBL5fkp8
function doSomething () {
console.log(‘do something’);
for (var i = 0; i < 10; i++) {
console.log(‘internal i’, i);
}
console.log(‘external i’, i);
}
Function Scoping (ES5)
function doSomething () {
console.log(‘do something’)
for (let i = 0; i < 10; i++) {
console.log(‘internal i’, i)
}
console.log(‘external i’, i)
}
Block Scoping (ES6)
const & let
let heart = ‘<3 you’ console.log(‘I', heart) // “I <3 You” heart = ‘<3 Justin Bieber’
console.log(‘I’, heart) // “I <3 Justin Bieber”
let
const
const heart = ‘<3 You’ console.log(‘I’, heart) // “I <3 You” heart = ‘<3 Justin Bieber’ //
SyntaxError: “heart” is read-only
Source: http://www.cs.uni.edu/~wallingf/blog/archives/monthly/2012-10.html
Arrow Function
////////// () => { } //////////
Arrow Function
////////// as an expression body //////////
var myName = ['g', 'a', 't', 'u', ‘k']; var capitalName = myName .map(function (letter) {
return letter.toUpperCase(); }) console.log(capitalName); // [‘G’, ‘A’, ‘T’, ‘U’, ‘K’]
ES5
ES6
const myName = ['g', 'a', 't', 'u', 'k']
const capitalName = myName.map(letter => letter.toUpperCase())
console.log(capitalName) // [‘G’, ‘A’, ‘T’, ‘U’, ‘K’]
var myName = ['g', 'a', 't', 'u', ‘k'];
var capitalName = myName
.map(function (letter) {
return letter.toUpperCase();
})
console.log(capitalName); // [‘G’, ‘A’, ‘T’, ‘U’, ‘K’]
ES5
Arrow Function
////////// as a statement body //////////
function getEvenNumbers (numbers) {
var evenNumbers = []
numbers.forEach ( function (number) {
if (number % 2 == 0) {
evenNumbers.push(number)
}
})
console.log('even numbers', evenNumbers)
}
getEvenNumbers([1, 3, 4, 8, 17, 24])
ES5
ES6
const getEvenNumbers = numbers => { let evenNumbers = []
numbers.forEach(number => { if (number % 2 == 0) {
evenNumbers.push(number) } }) console.log('Even Numbers', evenNumbers) //
[4, 8, 24] } getEvenNumbers([1, 3, 4, 8, 17, 24])
Arrow Function
////////// Lexical this //////////
function main () { console.log(‘fruit:’, this.fruit) // ‘fruit: whatever’ function
getAFruit () { console.log(‘You’ve got’, this.fruit) } getAFruit() // Cannot read
property 'fruit' of undefined } main.call({ fruit: ‘whatever’ })
ES5
function main () { console.log(‘fruit:’, this.fruit) // ‘fruit: whatever’ function
getAFruit () { console.log(‘You’ve got’, this.fruit) } getAFruit.call({ fruit: ‘apple’ })
// You’ve got apple } main.call({ fruit: ‘whatever’ })
ES5
ES6
function main () { console.log(‘fruit:’, this.fruit) // ‘fruit: whatever’ const getAFruit
= () => { console.log(‘You’ve got’, this.fruit) } getAFruit() // You’ve got
whatever } main.call({ fruit: ‘whatever’ })
ES6
function main () { console.log(‘fruit:’, this.fruit) // fruit: apple const getAFruit = ()
=> { console.log(‘You’ve got’, this.fruit) } getAFruit() // You’ve got apple }
main.call({ fruit: ‘apple’ })
Template Literals
////////// Interpolation //////////
var pen = ‘a pen’;
var apple = ‘an apple’;
console.log (‘I have ’ + pen + ‘. I have ’ + apple + ‘.’);
// I have a pen. I have an apple.
ES5
ES6
let pen = ‘a pen’
let pineapple = ‘pineapple’
console.log(`I have ${pen}. I have ${pineapple}.`)
// I have a pen. I have pineapple.
ES6
let pen = ‘a pen’
let pineapple = ‘pineapple’
console.log(`I have ${pen}. I have ${pineapple}.`)
// I have a pen. I have pineapple.
let pen = ‘ pen ’
let apple = ‘ apple ’
let pineapple = ‘ pineapple ’
console.log(
`${pen + pineapple + apple + pen}`
)
Template Literals
////////// Multi-line String //////////
console.log(
`
The quick down fox jump
over the lazy dog.
`
)
ES6
Destructuring
Assignment
////////// Basic variable assignment //////////
const minions = [ , , ] const [stuart, kevin, dave] = minions
console.log(stuart) // console.log(kevin) // console.log(dave) //
const minions = [ , , ] const [stuart, …others] = minions
console.log(stuart) // console.log(others) // [ , ]
const stuart, kevin, dave const [stuart, kevin, dave] = [ , , ]
console.log(stuart) // console.log(kevin) // ,
let stuart, kevin, dave [stuart, kevin, dave] = [ , , ] [kevin, stuart,
dave] = [stuart, dave, kevin] console.log(kevin) // console.log(stuart)
// console.log(dave) //
const stuart, kevin, dave const getMinions = () => [ , , ] [start,
kevin, dave] = getMinions() console.log(kevin) // console.log(stuart)
// console.log(dave) //
Module Support
ES6
// libs/pokemon.js
export const items = {
lureModule: ,
pokeball:
}
const pokemonsList = []
export const catchedPokemon = (pokeball, pokemon) => {
if (pokeball) { pokemonsList.push(pokemon) }
}
ES6
// libs/pokemon.js
export const items = {
lureModule: ,
pokeball:
}
const pokemonsList = []
export const catchedPokemon = (pokeball, pokemon) => {
if (pokeball) { pokemonsList.push(pokemon) }
}
// pokemonGo.js
import {items, cachedPokemon} from ‘libs/pokemon’
catchedPokemon(items.pokeball, )
References
http://es6-features.org/
https://developer.mozilla.org/en-US/docs/Web/JavaScript
https://github.com/lukehoban/es6features
https://webapplog.com/es6/

Ecmascript 6