99
#comdaybe ECMAScript.next! ECMAScript.wtf? e-guidelines - HOWEST @kevinderudder

ECMAScript.Next ECMAScipt 6

Embed Size (px)

DESCRIPTION

A tour on the future features JavaScript

Citation preview

Page 1: ECMAScript.Next ECMAScipt 6

#comdaybe

ECMAScript.next!

ECMAScript.wtf?e-guidelines - HOWEST

@kevinderudder

Page 2: ECMAScript.Next ECMAScipt 6

ECMAScript.sext

ECMAScript sex

Page 3: ECMAScript.Next ECMAScipt 6

This is a JavaScript session

Page 4: ECMAScript.Next ECMAScipt 6

There will be code

Page 5: ECMAScript.Next ECMAScipt 6

@kevinderudder working for eGuidelinesand lecturer at the Technical University of West Flanders.

Contact me on [email protected]

Page 6: ECMAScript.Next ECMAScipt 6
Page 7: ECMAScript.Next ECMAScipt 6
Page 8: ECMAScript.Next ECMAScipt 6

Why this session

Page 9: ECMAScript.Next ECMAScipt 6

probably the most popularprogramming language

JavaScriptis

Page 10: ECMAScript.Next ECMAScipt 6

probably the most misunderstoodprogramming language

JavaScriptis

Page 11: ECMAScript.Next ECMAScipt 6

I <3 JavaScript

Page 12: ECMAScript.Next ECMAScipt 6
Page 13: ECMAScript.Next ECMAScipt 6

GOALIntriguing JavaScript story

What’s new

Code, code and code

Page 14: ECMAScript.Next ECMAScipt 6

The intriguing JavaScript Story

Page 15: ECMAScript.Next ECMAScipt 6

19961995 20051999 2009 2011 20131992 1998

Page 16: ECMAScript.Next ECMAScipt 6
Page 17: ECMAScript.Next ECMAScipt 6

19961995 20051999 2009 2011 20131992 1998

Page 18: ECMAScript.Next ECMAScipt 6
Page 19: ECMAScript.Next ECMAScipt 6

MochaLiveScriptMocha

19961995 20051999 2009 2011 20131992 1998

Page 20: ECMAScript.Next ECMAScipt 6

LiveScript

JavaScript

LiveScript

TM

Java

Page 21: ECMAScript.Next ECMAScipt 6

19961995 20051999 2009 2011 20131992 1998

Page 22: ECMAScript.Next ECMAScipt 6
Page 23: ECMAScript.Next ECMAScipt 6

JScript

19961995 20051999 2009 2011 20131992 1998

Page 24: ECMAScript.Next ECMAScipt 6

ECMAJavaScript

SCRIPT

19961995 20051999 2009 2011 20131992 1998

Page 25: ECMAScript.Next ECMAScipt 6

JAVASCRIPT

ECMASCRIPT

19961995 20051999 2009 2011 20131992 1998

Page 26: ECMAScript.Next ECMAScipt 6

is a dialect of

JAVASCRIPT

ECMASCRIPT

19961995 20051999 2009 2011 20131992 1998

Page 27: ECMAScript.Next ECMAScipt 6

ECMASCRIPT

are dialects of

JAVASCRIPT JSCRIPT ACTIONSCRIPT

19961995 20051999 2009 2011 20131992 1998

Page 28: ECMAScript.Next ECMAScipt 6

ECMASCRIPT~

TC 39 controls whatwill be included inthe spec

19961995 20051999 2009 2011 20131992 1998

Page 29: ECMAScript.Next ECMAScipt 6

ECMASCRIPT 2be inline with excistinginternational standards

~

19961995 20051999 2009 2011 20131992 1998

Page 30: ECMAScript.Next ECMAScipt 6

features that are reallyessential to programming

regular expressions more string methods switch, do while

instanceof exception handling numeric formatting

~ECMASCRIPT 3

19961995 20051999 2009 2011 20131992 1998

Page 31: ECMAScript.Next ECMAScipt 6

anticipated the future~

ECMASCRIPT 4

19961995 20051999 2009 2011 20131992 1998

Page 32: ECMAScript.Next ECMAScipt 6

ECMASCRIPT 4~

Incompatible with ES 3

19961995 20051999 2009 2011 20131992 1998

Page 33: ECMAScript.Next ECMAScipt 6

AJAXBuild complex web apps

~

19961995 20051999 2009 2011 20131992 1998

Page 34: ECMAScript.Next ECMAScipt 6

Object.Create defineProperty Strict

Getter and setters json supportSemantic changes

~ECMASCRIPT 5

19961995 20051999 2009 2011 20131992 1998

Page 35: ECMAScript.Next ECMAScipt 6

Revision of ES5~

ECMASCRIPT 5.1

19961995 20051999 2009 2011 20131992 1998

Page 36: ECMAScript.Next ECMAScipt 6

~

also called ES.next

ECMASCRIPT 6

19961995 20051999 2009 2011 20131992 1998

Page 37: ECMAScript.Next ECMAScipt 6

~

compatible with ES5

ECMASCRIPT 6

19961995 20051999 2009 2011 20131992 1998

Page 38: ECMAScript.Next ECMAScipt 6

~

be a better language

ECMASCRIPT 6

19961995 20051999 2009 2011 20131992 1998

Page 39: ECMAScript.Next ECMAScipt 6

~

Final review of draftin november 2013

ECMASCRIPT 6

19961995 20051999 2009 2011 20131992 1998

Page 40: ECMAScript.Next ECMAScipt 6

~

ECMA approval indecember 2014

ECMASCRIPT 6

19961995 20051999 2009 2011 20131992 1998

Page 41: ECMAScript.Next ECMAScipt 6

ECMAHARMONYSuperset of ES.nextfuture features in ES.nextor ES.next.next

~

19961995 20051999 2009 2011 20131992 1998

Page 42: ECMAScript.Next ECMAScipt 6
Page 44: ECMAScript.Next ECMAScipt 6

most ES.next features don’t work in your browser

Page 45: ECMAScript.Next ECMAScipt 6

What to use??

› Node

› TypeScript

› Transpilers

› Google Traceur

› Esprima

Page 46: ECMAScript.Next ECMAScipt 6

http://kangax.github.io/es5-compat-table/es6/

Page 47: ECMAScript.Next ECMAScipt 6
Page 48: ECMAScript.Next ECMAScipt 6

SHOW ME CODE

Page 49: ECMAScript.Next ECMAScipt 6

Features

• iterators

• const and let

• maps

• rest parameters

• generators

• shorthand literal syntax

• arrow functions

• modules

• template strings

• classes

• symbols

• spread operator

• weakmaps

• new object methods

• default parameters

• binary data…

Page 50: ECMAScript.Next ECMAScipt 6

about variables and scopes

Page 51: ECMAScript.Next ECMAScipt 6

ES 5: function scope

var jamesBondMovies = ["Goldeneye", "Dr No"];function showMovies(){

var i = 0, l = jamesBondMovies.length;for(; i < l ; i++){

alert(jamesBondMovies[i]);}

alert(i);alert(l);

}

showMovies();

› 2› 2

Page 52: ECMAScript.Next ECMAScipt 6

ES.next: Block scope

› 2 new keywords: let and const

› let

› Creates a block scope variable

› const

› Like let but only readable afterdeclaration

Page 53: ECMAScript.Next ECMAScipt 6

let

var jamesBondMovies = ["Goldeneye", "Dr No"];function showMovies(){

let i = 0, l = jamesBondMovies.length;for(; i < l ; i++){

alert(jamesBondMovies[i]);}

alert(i);alert(l);

}

showMovies();

› i is not defined› l is not defined

Page 54: ECMAScript.Next ECMAScipt 6

Shorthand Object Literal Syntax

Page 55: ECMAScript.Next ECMAScipt 6

Shorthand Object Literal Syntax

› Allow us to remove redundant code

var jamesbond = 'Sean Connery';var title = 'Dr No';var actor1 = 'Joseph Wiseman';

var drNO = {'jamesbond': jamesbond,'title': title,'badGuy': actor1

};

var jamesbond = 'Sean Connery';var title = 'Dr No';var actor1 = 'Joseph Wiseman';

var drNO = {jamesbond,title,'badGuy': actor1

};

ES 5 ES.next

Page 56: ECMAScript.Next ECMAScipt 6

Shorthand Object Literal Syntax

function JamesBond(name, f){this.name = name;this.favDrink = f;

}

JamesBond.prototype = {order(howTo){

alert(this.favoriteDrink + ' ' + howTo);},get favoriteDrink(){

return this.favDrink;},set favoriteDrink(f){

this.favDrink = f;}

};

Page 57: ECMAScript.Next ECMAScipt 6

Default Function Parameters

Page 58: ECMAScript.Next ECMAScipt 6
Page 59: ECMAScript.Next ECMAScipt 6

Default Function Parameters

› Have optional function parameters by setting default values

› The parameters are defined when the functionsare defined

Page 60: ECMAScript.Next ECMAScipt 6

Rest parameter

Page 61: ECMAScript.Next ECMAScipt 6

Function problem

› You never know how many arguments are beingpassed to a function

› Use the arguments object to get all the arguments

› Arguments is not an array!!!

› Solution: rest parameter

› use ‘…’ to denote a variable number of arguments

Page 62: ECMAScript.Next ECMAScipt 6

Rest Parameters

› Pass a number of args to a named parameter

› Choos a name for your parameter

› Instead of using the arguments object

› Variabe name preceded with …

function addMovies(...movies){movies.forEach(function(m){

alert(m);});

}addMovies("dr No", "Goldeneye");addMovies("dr No", "licence to kill", "Goldfinger");

Page 63: ECMAScript.Next ECMAScipt 6

Rest Parameters

function JamesBond(name, favoriteDrink, ...gadgets) {

for(var i = 0, l = gadgets.length;i<l;i++){alert(gadgets[i]);

}this.name = name;this.favoriteDrink = favoriteDrink;

}

var sean = new JamesBond('Sean Connery', 'Martini','Watch with a laser','Aston Martin','Mojo');

Page 64: ECMAScript.Next ECMAScipt 6

Spread operator

Page 65: ECMAScript.Next ECMAScipt 6

Spread operator

› Opposite of rest parameters

› Pass number of arguments to function

function commentMovie(date, m, comment, by){log("comment on '" + m+ "' by " + by + " on " + date);

}

function getComment(){return ["Goldeneye", "my favorite", "kevin"];

}

commentMovie("20/06/2013", ...getComment());

Page 66: ECMAScript.Next ECMAScipt 6

Spread Operator

function Cocktail(name, type, ...ingredients){alert(name + '(' + type + ')');alert('ingredients');alert(' ' + ingredients);

var i = 0, l = ingredients.length;

}

var vesper = ['shortdrink', 'lillet blanc, wodka, gin'];

var v = new Cocktail('Vesper Martini', ...vesper);

Page 67: ECMAScript.Next ECMAScipt 6

Spread operator and rest parameters combined

function Cocktail(name, type, ...ingredients){alert(name + '(' + type + ')');alert('ingredients');alert(' ' + ingredients);

var i = 0, l = ingredients.length;for(;i<l;i++){

alert(' ' + ingredients[i]);}

}

var vesper = ['shortdrink','lillet','blanc','wodka','gin'];

var v = new Cocktail('Vesper Martini', ...vesper);

Page 68: ECMAScript.Next ECMAScipt 6

Classes

Page 69: ECMAScript.Next ECMAScipt 6

Classes

› A class is a representation of an object

› Blueprint to create objects

› Until now, we’ve faked it

› functions and prototypes to implement classes

Page 70: ECMAScript.Next ECMAScipt 6

Fake itfunction JamesBond(name, favoriteDrink) {this.name = name;this.favDrink = favoriteDrink;

}

JamesBond.prototype = {/* PROPERTIES */get favoriteDrink() { return this.favDrink },set favoriteDrink(v) { this.favDrink = v },

/* BEHAVIOR */orderDrink: function () { alert('order'); }

};

var daniel = new JamesBond('Daniel Craig', 'Vesper Martini');

alert(daniel.favoriteDrink);daniel.orderDrink();

Page 71: ECMAScript.Next ECMAScipt 6

Classes in ES6class JamesBond{

constructor(name, favoriteDrink){this.name = name;this.favDrink = favoriteDrink;

}

get favoriteDrink() { return this.favDrink }set favoriteDrink(v) { this.favDrink = v }

orderDrink(how) { alert(this.favDrink + '\n ' + how);

}}var daniel = new JamesBond('Daniel Craig'

, 'Vesper Martini');

daniel.orderDrink('shaken not stirred');

Page 72: ECMAScript.Next ECMAScipt 6

Extending classes

class Movie{constructor(title){

this.title = title;}

}

class jamesBondMovie extends Movie{constructor(title, jamesBond, badGuy, omgGirl){super(title);

this.jamesBond = jamesBond;this.badGuy = badGuy;this.omgGirl = omgGirl;

}};

Page 73: ECMAScript.Next ECMAScipt 6

Modules

Page 74: ECMAScript.Next ECMAScipt 6

Modules

› Group functionality into a module

› Define which function you want to make availableexternally

› Import module in your code

› Define which functionality you want to include

Page 75: ECMAScript.Next ECMAScipt 6

Modules

module helper{export function print(array){

for(var a of array){log(a);

}}

}

import {print} from helper;

print(["Pierce","Sean"]);

Page 76: ECMAScript.Next ECMAScipt 6

Remote modules

module JSON at 'http://json.org/modules/json2.js';

alert(JSON.stringify({'hi': 'world'}));

Page 77: ECMAScript.Next ECMAScipt 6

Module Loaders

› Dynamic API for loading modules in controlledand selectively isolated contexts

Loader.load('http://json.org/modules/json2.js',function(JSON) {

alert(JSON.stringify([0, {a: true}]));});

Page 78: ECMAScript.Next ECMAScipt 6

Destructuring

Page 79: ECMAScript.Next ECMAScipt 6

Destructuring

› Extract values from an object using patterns

› Get zone number out of telephone number

› Syntactic sugar

› Is already supported in a number of languages

› Python: sequence unpacking

› Coffeescript: destructuring

Page 80: ECMAScript.Next ECMAScipt 6

Array destructuring

› Variables can be initialized in one statement

› Swapping 2 variables

var [d, m, y] = [20,6,2013];

var d = 20, m = 6, y = 2013;

var [value1, value2] = [10, 30];[value1, value2] = [value2, value1]

Page 81: ECMAScript.Next ECMAScipt 6

Good case: return multiple values

› Return multiple

function getMovieInfo(){return ["Dr No", "Sean Connery", "Joseph Wiseman"];

}

var [title, jamesbond, badGuy] = getMovieInfo();

Page 82: ECMAScript.Next ECMAScipt 6

Object destructuring

› Similar to array pattern

› Map on object properties

var movie = { "name": "Sean Connery“, "movie": "Dr No"};

var {name: actor, movie: title} = movie;

Page 83: ECMAScript.Next ECMAScipt 6

Object destructuring

movies.forEach(function({name: actor, movie: movie}){// ...

});

Page 84: ECMAScript.Next ECMAScipt 6

Maps, WeakMaps and sets

Page 85: ECMAScript.Next ECMAScipt 6

Maps, WeakMaps and Sets

› Until now, Array was (is) the only type of collection

› ES6 introduces 3 new types of collections

› Set:

› Map:

› WeakMap:

Page 86: ECMAScript.Next ECMAScipt 6

Sets

› List of unique array elements

var movies = new Set();movies.add("Dr No");movies.add("GoldFinger");

console.log(movies.size()); // --> 2

var movies = new Set();movies.add("Dr No");movies.add("GoldFinger");movies.add("Dr No"); // --> already in the set

console.log(movies.size()); // --> 2

Page 87: ECMAScript.Next ECMAScipt 6

Maps

› Map a value to a unique key

› cfr: a Dictionary

var movies = new Map();movies.set(1, "Dr No");movies.set(2, "GoldFinger");

var m = movies.get(1);movies.delete(2);

Page 88: ECMAScript.Next ECMAScipt 6

WeakMaps

› Like maps

› But: key must be an object

cannot be a primitive value

› When the reference gets garbage collected, the weakmap will remove the item

Page 89: ECMAScript.Next ECMAScipt 6

WeakMaps

var drNo = new Movie(...params);

var movies = new WeakMap();movies.set(drNo, {'jamesbond': 'Sean'});

var m = movies.get(drNo); // OK

drNo = null;

var m = movies.get(drNo); // UNDEFINED

Page 90: ECMAScript.Next ECMAScipt 6

Iterators

Page 91: ECMAScript.Next ECMAScipt 6

Iterators

› JavaScript has the for – in

› In an array, the for-in gives the indexes and not the values

var actors = ["Sean", "Timothy", "Roger”];

for (var a in actors) {alert(a); // 0,1,2

}

Page 92: ECMAScript.Next ECMAScipt 6

for-of

var actors = ["Sean", "Timothy", "Roger", "Pierce"];for (var a of actors) {

alert(a); // 0,1,2,3}

var movies = new Map();movies.set(1, "Dr No");movies.set(2, "GoldFinger");

for(var [id, title] n movies){alert(title);

}

Page 93: ECMAScript.Next ECMAScipt 6

Arrow functions

Page 94: ECMAScript.Next ECMAScipt 6

Arrow Functions

› JavaScript developers frequently use functionexpressions

$("#button").click(function(){ /*...*/ });

forEach(function(item){ /*...*/ });

Page 95: ECMAScript.Next ECMAScipt 6

Arrow Functions

function addMovies(...movies){movies.forEach(function(m){

alert(m);});

}

addMovies("dr No", "Goldeneye");

function addMovies(...movies){movies.forEach(m => alert(m));

}

addMovies("dr No", "Goldeneye");

Page 96: ECMAScript.Next ECMAScipt 6

I’m sorry but no time for

Page 97: ECMAScript.Next ECMAScipt 6

• Iterators

• Symbols

• Binary data

• Tail-call optomization

• Proxies

• Reflection methods

• …

Other cool stuff

Page 98: ECMAScript.Next ECMAScipt 6
Page 99: ECMAScript.Next ECMAScipt 6

Q&A