SlideShare a Scribd company logo
Exploring ES6
Playground
4 npm install babel-node
4 http://babeljs.io/repl
4 chrome://flags/#enable-javascript-harmony
4 Scratchpad in Firefox
Favourite ES6 features
https://docs.google.com/forms/d/
1bhmqePIstN9NNxK4QX1-F8XJcJ8C0HdPao01gAAoh3I/
viewanalytics
-- Axel Rauschmayer
Begin from some sugar syntax
4 Classes
4 Modules
4 Arrow functions
4 Template literal
4 Destructuring
Classes
// ES3
var inherit = (function() {
var F = function() {}
return function(C, P) {
F.prototype = P.prototype
C.prototype = new F
C.prototype.constructor = C
}
})()
function Overlay(options) {}
function Dialog(options) {
Overlay.call(this, options)
}
inherit(Dialog, Overlay)
Classes
// ES5
function Dialog(options) {
Overlay.call(this, options)
}
Dialog.prototype = Object.create(Overlay.prototype)
Dialog.prototype.constructor = Overlay
Classes
// ES6
class Overlay {
constructor(options) {}
}
class Dialog extends Overlay {
constructor(options) {
super(options)
}
open() {}
}
Classes
mixin
import mixin from 'mixin'
class Overlay {
constructor(options) {}
}
class Dialog extends mixin(Overlay, EventEmitter) {
constructor(options) {
super(options)
}
open() {}
}
Classes
// getter/setter
class Polygon {
constructor(height, width) {
this.height = height
this.width = width
}
get area() {
return this.calcArea()
}
calcArea() {
return this.height * this.width
}
}
Classes
// static methods
class Point {
constructor(x, y) {
this.x = x
this.y = y
}
static distance(a, b) {
const dx = a.x - b.x
const dy = a.y - b.y
return Math.sqrt(dx * dx + dy * dy)
}
}
Modules
// CommonJS
// export
// animationFrame.js
module.exports = {
requestAnimationFrame: window.requestAnimationFrame,
cancelAnimationFrame: window.cancelAnimationFrame
}
// import
var raf = require('./animationFrame').requestAnimationFrame
Modules
// es6
// export
export requestAnimationFrame
export cancelAnimationFrame
// import
import { requestAnimationFrame as raf, cancelAnimationFrame as caf } from './animationFrame'
// export
export default React
// import
import React, { Component } from 'react'
Arrow functions
()=>{}
Identifier => Expression
// ES5
var total = values.reduce(function (a, b) {
return a + b;
}, 0);
// ES6
var total = values.reduce((a, b) => a + b, 0);
Template
4 multiple lines
4 expression inside
`Error Code: ${exception.code},
Error Message: ${exception.message}`
Destructuring
// array
var [a, , b] = [1,2,3]
[a, b] = [b, a]
// object
let options = {closeOnPressingESC: false, closeOnClicking: true}
let { closeOnPressingESC: esc, closeOnClicking: click } = options
Setup
4 npm init
4 npm install webpack babel-loader
4 touch webpack.config.js
var webpack = require('webpack'),
path = require('path')
module.exports = {
entry: [
'./src/index.js'
],
output: {
path: path.join(__dirname, 'dist'),
filename: 'bundle.js'
},
module: {
loaders: [{
loaders: ['babel'],
include: path.join(__dirname, 'src')
}]
}
}
More
4 Iterator
4 Generator
4 Proxy
Iterator
// for in (not recommended)
for (var index in values) {
console.log(values[index])
}
// for of
for (var value of values) {
console.log(value)
}
Iterator
Array, String, Map, Set, Array-like objects(NodeLists...)
Iterator
class RangeIterator {
constructor(start, stop) {
this.value = start
this.stop = stop
}
[Symbol.iterator]() { return this; }
next() {
var value = this.value
if (value < this.stop) {
this.value++
return {done: false, value: value}
} else {
return {done: true, value: undefined}
}
}
}
function range(start, stop) {
return new RangeIterator(start, stop)
}
Generator
function* range(start, stop) {
for (var i = start; i < stop; i++)
yield i
}
Generators are iterators.
All generators have a built-in implementation of .next()
and Symbol.iterator.
Generator
request('/api/user/~me', function(res) {
request('/api/statuses/' + res.id, function(res) {
// ...
})
})
Generator
function fetch(url) {
request(url, function(response){
it.next(response)
})
}
function *main() {
var result1 = yield fetch('/api/user/~me')
var result2 = yield request('/api/statuses/' + result1.id )
}
var it = main()
it.next()
Generator
function runGenerator(g) {
var it = g(), ret
(function iterate(val){
ret = it.next( val )
if (!ret.done) {
if ('then' in ret.value) {
ret.value.then( iterate )
}
else {
setTimeout( function(){
iterate( ret.value )
}, 0 )
}
}
})()
}
Generator
runGenerator(function *main(){
var result1 = yield fetch('/api/user/~me')
var result2 = yield fetch('/api/statuses/' + result1.id )
})
https://github.com/tj/co
Proxy
setter/getter in ES5
// ES5
var person = {}
Object.defineProperty(person, 'age', {
set: function(value) {
if (value > 200) {
throw new RangeError('seems invalid')
}
}
})
Proxy
setter/getter, the different way
let validator = {
set(obj, prop, value) {
if (prop === 'age') {
if (!Number.isInteger(value)) {
throw new TypeError('The age is not an integer')
}
if (value > 200) {
throw new RangeError('The age seems invalid')
}
}
// The default behavior to store the value
obj[prop] = value
}
}
let person = new Proxy({}, validator)
Proxy
new Proxy(target, handler)
4 handler.getPrototypeOf()
4 handler.setPrototypeOf()
4 handler.isExtensible()
4 handler.preventExtensions()
4 handler.getOwnPropertyDescriptor()
Proxy
4 handler.defineProperty()
4 handler.has()
4 handler.get()
4 handler.set()
4 handler.deleteProperty()
4 handler.enumerate()
4 handler.ownKeys()
Proxy
4 handler.apply()
4 handler.construct()
References
4 ES6 in depth
4 Learn ECMAScript 6 inside and out
4 Meta programming with ECMAScript 6 proxies

More Related Content

What's hot

"Немного о функциональном программирование в JavaScript" Алексей Коваленко
"Немного о функциональном программирование в JavaScript" Алексей Коваленко"Немного о функциональном программирование в JavaScript" Алексей Коваленко
"Немного о функциональном программирование в JavaScript" Алексей Коваленко
Fwdays
 
Functional programming in Python
Functional programming in PythonFunctional programming in Python
Functional programming in Python
Colin Su
 

What's hot (16)

An Intro To ES6
An Intro To ES6An Intro To ES6
An Intro To ES6
 
EcmaScript 6
EcmaScript 6 EcmaScript 6
EcmaScript 6
 
Imugi: Compiler made with Python
Imugi: Compiler made with PythonImugi: Compiler made with Python
Imugi: Compiler made with Python
 
Pratt Parser in Python
Pratt Parser in PythonPratt Parser in Python
Pratt Parser in Python
 
From Functor Composition to Monad Transformers
From Functor Composition to Monad TransformersFrom Functor Composition to Monad Transformers
From Functor Composition to Monad Transformers
 
4. Обработка ошибок, исключения, отладка
4. Обработка ошибок, исключения, отладка4. Обработка ошибок, исключения, отладка
4. Обработка ошибок, исключения, отладка
 
VTU DSA Lab Manual
VTU DSA Lab ManualVTU DSA Lab Manual
VTU DSA Lab Manual
 
Wakanday JS201 Best Practices
Wakanday JS201 Best PracticesWakanday JS201 Best Practices
Wakanday JS201 Best Practices
 
"Немного о функциональном программирование в JavaScript" Алексей Коваленко
"Немного о функциональном программирование в JavaScript" Алексей Коваленко"Немного о функциональном программирование в JavaScript" Алексей Коваленко
"Немного о функциональном программирование в JavaScript" Алексей Коваленко
 
Swift 3.0 で変わったところ - 厳選 13 項目 #love_swift #cswift
Swift 3.0 で変わったところ - 厳選 13 項目 #love_swift #cswiftSwift 3.0 で変わったところ - 厳選 13 項目 #love_swift #cswift
Swift 3.0 で変わったところ - 厳選 13 項目 #love_swift #cswift
 
ScalaFlavor4J
ScalaFlavor4JScalaFlavor4J
ScalaFlavor4J
 
Developing iOS apps with Swift
Developing iOS apps with SwiftDeveloping iOS apps with Swift
Developing iOS apps with Swift
 
A swift introduction to Swift
A swift introduction to SwiftA swift introduction to Swift
A swift introduction to Swift
 
Functional programming in Python
Functional programming in PythonFunctional programming in Python
Functional programming in Python
 
Introduction to Swift programming language.
Introduction to Swift programming language.Introduction to Swift programming language.
Introduction to Swift programming language.
 
Swift 2
Swift 2Swift 2
Swift 2
 

Viewers also liked

Miami-Oracle-Distraction to Disruption Under 30
Miami-Oracle-Distraction to Disruption Under 30Miami-Oracle-Distraction to Disruption Under 30
Miami-Oracle-Distraction to Disruption Under 30
Kevin D. Bird
 
The Internet of Me
The Internet of MeThe Internet of Me
The Internet of Me
Sherry Jones
 
C.G.Santhosh-Resume
C.G.Santhosh-ResumeC.G.Santhosh-Resume
C.G.Santhosh-Resume
Santhosh Cg
 
Resume_KaiYanTan
Resume_KaiYanTanResume_KaiYanTan
Resume_KaiYanTan
Kai Yan Tan
 

Viewers also liked (20)

MiamiJS - The Future of JavaScript
MiamiJS - The Future of JavaScriptMiamiJS - The Future of JavaScript
MiamiJS - The Future of JavaScript
 
Training presentation
Training presentationTraining presentation
Training presentation
 
TypeScript 2 in action
TypeScript 2 in actionTypeScript 2 in action
TypeScript 2 in action
 
Future of java script web version
Future of java script web versionFuture of java script web version
Future of java script web version
 
When exhausting all efforts
When exhausting all effortsWhen exhausting all efforts
When exhausting all efforts
 
Presentacion jornada entre iguales
Presentacion jornada entre igualesPresentacion jornada entre iguales
Presentacion jornada entre iguales
 
resume dt.01102015
resume dt.01102015resume dt.01102015
resume dt.01102015
 
Miami-Oracle-Distraction to Disruption Under 30
Miami-Oracle-Distraction to Disruption Under 30Miami-Oracle-Distraction to Disruption Under 30
Miami-Oracle-Distraction to Disruption Under 30
 
Promes dan-pemetaan-smtr-1
Promes dan-pemetaan-smtr-1Promes dan-pemetaan-smtr-1
Promes dan-pemetaan-smtr-1
 
The Internet of Me
The Internet of MeThe Internet of Me
The Internet of Me
 
C.G.Santhosh-Resume
C.G.Santhosh-ResumeC.G.Santhosh-Resume
C.G.Santhosh-Resume
 
Training1 at jordy-paenen
Training1 at jordy-paenenTraining1 at jordy-paenen
Training1 at jordy-paenen
 
Presentation1
Presentation1Presentation1
Presentation1
 
Walking as children of lightv
Walking  as  children  of  lightvWalking  as  children  of  lightv
Walking as children of lightv
 
BnSeries_Textures
BnSeries_TexturesBnSeries_Textures
BnSeries_Textures
 
Wild salmon
Wild salmonWild salmon
Wild salmon
 
Resume_KaiYanTan
Resume_KaiYanTanResume_KaiYanTan
Resume_KaiYanTan
 
My Work Experience
My Work ExperienceMy Work Experience
My Work Experience
 
Grupo
GrupoGrupo
Grupo
 
SOLOMOTO_Продвижение через социальную сеть "ВКонтакте"
SOLOMOTO_Продвижение через социальную сеть "ВКонтакте"SOLOMOTO_Продвижение через социальную сеть "ВКонтакте"
SOLOMOTO_Продвижение через социальную сеть "ВКонтакте"
 

Similar to Exploring ES6

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
 
JavaScript - new features in ECMAScript 6
JavaScript - new features in ECMAScript 6JavaScript - new features in ECMAScript 6
JavaScript - new features in ECMAScript 6
Solution4Future
 

Similar to Exploring ES6 (20)

Impress Your Friends with EcmaScript 2015
Impress Your Friends with EcmaScript 2015Impress Your Friends with EcmaScript 2015
Impress Your Friends with EcmaScript 2015
 
Workshop 10: ECMAScript 6
Workshop 10: ECMAScript 6Workshop 10: ECMAScript 6
Workshop 10: ECMAScript 6
 
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
 
JSConf: All You Can Leet
JSConf: All You Can LeetJSConf: All You Can Leet
JSConf: All You Can Leet
 
ES6 PPT FOR 2016
ES6 PPT FOR 2016ES6 PPT FOR 2016
ES6 PPT FOR 2016
 
Txjs
TxjsTxjs
Txjs
 
Es6 hackathon
Es6 hackathonEs6 hackathon
Es6 hackathon
 
Javascript
JavascriptJavascript
Javascript
 
"let ECMAScript = 6"
"let ECMAScript = 6" "let ECMAScript = 6"
"let ECMAScript = 6"
 
EcmaScript 6 - The future is here
EcmaScript 6 - The future is hereEcmaScript 6 - The future is here
EcmaScript 6 - The future is here
 
ES6 and BEYOND
ES6 and BEYONDES6 and BEYOND
ES6 and BEYOND
 
JavaScript - new features in ECMAScript 6
JavaScript - new features in ECMAScript 6JavaScript - new features in ECMAScript 6
JavaScript - new features in ECMAScript 6
 
Ecmascript 2015 – best of new features()
Ecmascript 2015 – best of new features()Ecmascript 2015 – best of new features()
Ecmascript 2015 – best of new features()
 
Say It With Javascript
Say It With JavascriptSay It With Javascript
Say It With Javascript
 
ES6 Overview
ES6 OverviewES6 Overview
ES6 Overview
 
Thinking Functionally with JavaScript
Thinking Functionally with JavaScriptThinking Functionally with JavaScript
Thinking Functionally with JavaScript
 
Idiomatic Javascript (ES5 to ES2015+)
Idiomatic Javascript (ES5 to ES2015+)Idiomatic Javascript (ES5 to ES2015+)
Idiomatic Javascript (ES5 to ES2015+)
 
ES6 and AngularAMD
ES6 and AngularAMDES6 and AngularAMD
ES6 and AngularAMD
 
Es6 to es5
Es6 to es5Es6 to es5
Es6 to es5
 
ECMAScript 6 Review
ECMAScript 6 ReviewECMAScript 6 Review
ECMAScript 6 Review
 

Recently uploaded

Article writing on excessive use of internet.pptx
Article writing on excessive use of internet.pptxArticle writing on excessive use of internet.pptx
Article writing on excessive use of internet.pptx
abhinandnam9997
 
audience research (emma) 1.pptxkkkkkkkkkkkkkkkkk
audience research (emma) 1.pptxkkkkkkkkkkkkkkkkkaudience research (emma) 1.pptxkkkkkkkkkkkkkkkkk
audience research (emma) 1.pptxkkkkkkkkkkkkkkkkk
lolsDocherty
 
一比一原版UTS毕业证悉尼科技大学毕业证成绩单如何办理
一比一原版UTS毕业证悉尼科技大学毕业证成绩单如何办理一比一原版UTS毕业证悉尼科技大学毕业证成绩单如何办理
一比一原版UTS毕业证悉尼科技大学毕业证成绩单如何办理
aagad
 

Recently uploaded (13)

ER(Entity Relationship) Diagram for online shopping - TAE
ER(Entity Relationship) Diagram for online shopping - TAEER(Entity Relationship) Diagram for online shopping - TAE
ER(Entity Relationship) Diagram for online shopping - TAE
 
Bug Bounty Blueprint : A Beginner's Guide
Bug Bounty Blueprint : A Beginner's GuideBug Bounty Blueprint : A Beginner's Guide
Bug Bounty Blueprint : A Beginner's Guide
 
Pvtaan Social media marketing proposal.pdf
Pvtaan Social media marketing proposal.pdfPvtaan Social media marketing proposal.pdf
Pvtaan Social media marketing proposal.pdf
 
The+Prospects+of+E-Commerce+in+China.pptx
The+Prospects+of+E-Commerce+in+China.pptxThe+Prospects+of+E-Commerce+in+China.pptx
The+Prospects+of+E-Commerce+in+China.pptx
 
Article writing on excessive use of internet.pptx
Article writing on excessive use of internet.pptxArticle writing on excessive use of internet.pptx
Article writing on excessive use of internet.pptx
 
Case study on merger of Vodafone and Idea (VI).pptx
Case study on merger of Vodafone and Idea (VI).pptxCase study on merger of Vodafone and Idea (VI).pptx
Case study on merger of Vodafone and Idea (VI).pptx
 
Multi-cluster Kubernetes Networking- Patterns, Projects and Guidelines
Multi-cluster Kubernetes Networking- Patterns, Projects and GuidelinesMulti-cluster Kubernetes Networking- Patterns, Projects and Guidelines
Multi-cluster Kubernetes Networking- Patterns, Projects and Guidelines
 
How Do I Begin the Linksys Velop Setup Process?
How Do I Begin the Linksys Velop Setup Process?How Do I Begin the Linksys Velop Setup Process?
How Do I Begin the Linksys Velop Setup Process?
 
audience research (emma) 1.pptxkkkkkkkkkkkkkkkkk
audience research (emma) 1.pptxkkkkkkkkkkkkkkkkkaudience research (emma) 1.pptxkkkkkkkkkkkkkkkkk
audience research (emma) 1.pptxkkkkkkkkkkkkkkkkk
 
The AI Powered Organization-Intro to AI-LAN.pdf
The AI Powered Organization-Intro to AI-LAN.pdfThe AI Powered Organization-Intro to AI-LAN.pdf
The AI Powered Organization-Intro to AI-LAN.pdf
 
一比一原版UTS毕业证悉尼科技大学毕业证成绩单如何办理
一比一原版UTS毕业证悉尼科技大学毕业证成绩单如何办理一比一原版UTS毕业证悉尼科技大学毕业证成绩单如何办理
一比一原版UTS毕业证悉尼科技大学毕业证成绩单如何办理
 
The Use of AI in Indonesia Election 2024: A Case Study
The Use of AI in Indonesia Election 2024: A Case StudyThe Use of AI in Indonesia Election 2024: A Case Study
The Use of AI in Indonesia Election 2024: A Case Study
 
The Best AI Powered Software - Intellivid AI Studio
The Best AI Powered Software - Intellivid AI StudioThe Best AI Powered Software - Intellivid AI Studio
The Best AI Powered Software - Intellivid AI Studio
 

Exploring ES6