ecmascript 6 new features

30
ECMAScript 6 by Vincent Dupont While42 meeting 8th July 2014

Upload: gephensg

Post on 21-Aug-2015

114 views

Category:

Internet


1 download

TRANSCRIPT

Page 1: ECMAScript 6 new features

ECMAScript 6

by Vincent Dupont

While42 meeting

8th July 2014

Page 2: ECMAScript 6 new features

Plan• Ecma International

• ECMAScript

• JavaScript

• Examples

• Conclusion

• Questions

Page 3: ECMAScript 6 new features

Ecma International

• European Computer Manufacturers Association

• Founded in 1961

• Standardize computer systems

Page 4: ECMAScript 6 new features

ECMASCript

• Specification ECMA-262

• Scripting “client side”

• Implementations (JavaScript, JScript, ActionScript)

Page 5: ECMAScript 6 new features

JavaScript• Brendan Eich (Netscape)

• Mocha, LiveScript

• 1995 (premiere version)

• 2009 version 5

• 2011 version 5.1

• Juin 2015 version 6

Page 6: ECMAScript 6 new features

Multi paradigme

• Scripting

• Imperative

• Functional

• Oriente objet (Prototypage)

Page 7: ECMAScript 6 new features

Constants

const PI = 3.141593

Page 8: ECMAScript 6 new features

letscope problems?

// ES5var hello = 'while 42';{ var hello = 'world';}

console.log(hello); // return 'world'

// ES6let world = 'while 42';

{ // let solve block scoping let world = 'world';}

console.log(world); // return ‘while 42'

Page 9: ECMAScript 6 new features

// ES5var fs = [];for (var i = 0; i < 5; i++) { fs.push(function() { console.log(i); })}

fs.forEach(function(f) { f(); // return 5});

// ES6fs = [];for (let j = 0; j < 5; j++) { fs.push(function() { console.log(j); })}

fs.forEach(function(f) { f(); // return 0 to 4});

Page 10: ECMAScript 6 new features

Arrow// ES5var sayHello = function (who) { console.log('hello ' + who); };sayHello('while 42');

// ES6var sayHello = who => console.log('hello ' + who);sayHello('world');

var sayWhat = (say, what) => console.log(say + ' ' + what);sayWhat('je dis', 'bonjour');

Syntactic sugar?

Page 11: ECMAScript 6 new features

var oldDeliveryBoy = { name: 'Vincent', deliver: function(msg, handler) { handler(msg); }, receive: function() { var that = this; this.deliver('Hello ', function(msg) { console.log(msg + that.name); }) }};

oldDeliveryBoy.receive();

var newDeliveryBoy = { name: 'Vincent', deliver: function(msg, handler) { handler(msg); }, receive: function() { this.deliver('Hello ', msg => console.log(msg + this.name)); }};

newDeliveryBoy.receive();

Page 12: ECMAScript 6 new features

Default Parameter Valuesfunction greet(firstName = 'John', lastName = 'Smith') { console.log(firstName + ' ' + lastName);}

greet();greet('Paul');

var oldFnDefFnValue = function (fn = function () { console.log('complete old'); }) { fn();};

oldFnDefFnValue();

var newFnDefFnValue = (fn = () => console.log('complete new')) => fn();

newFnDefFnValue();

Page 13: ECMAScript 6 new features

String templatesvar greeting = 'world';var template = `

hello ${greeting} crazy ${greeting}`;console.log(template);

var x = 1;var y = 2;var equation = `${ x } + ${ y } = ${ x + y }`;console.log(equation);

hello world crazy world

1 + 2 = 3

Page 14: ECMAScript 6 new features

var parseString = (strings, ...values) => { console.log(strings); console.log(values); if (values[0] < 20) { values[1] = 'awake'; } else { values[1] = 'sleeping'; } return `${strings[0]}${values[0]}${strings[1]}${values[1]}`};

var templateResult = parseString`It's ${new Date().getHours()}, I'm ${""}`;console.log(templateResult);

[ 'It\'s ', ', I\'m ', '' ] [ 21, '' ]

It's 21, I'm sleeping

Page 15: ECMAScript 6 new features

Enhanced object propertiesvar color = 'red';var speed = 100;function go() { console.log('vroom');}var action = 'start';

var car = { color, speed, go, horn() { console.log('honk honk'); }, [action]: function () { console.log('start'); }}; // same as : var car = {color: color, speed: speed};

console.log(car.color); // return redconsole.log(car.speed); // return 100car.go(); // return vroomcar.horn(); // return honk honkcar.start(); // return start

Page 16: ECMAScript 6 new features

Spread operatorconsole.log([1, 2, 3]); // return [1, 2, 3]console.log(...[1, 2, 3]); // return 1 2 3

let first = [1, 2, 3];let second = [4, 5, 6];first.push(second);console.log(first); // return [1, 2, 3, [4, 5, 6]]

first = [1, 2, 3];second = [4, 5, 6];first.push(...second);console.log(first); // return [1, 2, 3, 4, 5, 6]

function addThreeThings(a, b, c) { return a + b + c;}

console.log(addThreeThings(...first)); // return 6

Page 17: ECMAScript 6 new features

Destructuring assignment

var {firstName, lastName} = { // could also receive a function returning an object firstName: 'vincent', lastName: 'dupont'};

console.log(firstName);console.log(lastName);

var {firstName: prenom, lastName: nom} = { // could also receive a function returning an object firstName: 'vincent', lastName: 'dupont'};

console.log(prenom);console.log(nom);

var [one,,,last] = [1, 2, 3, 4];console.log(one); // return 1console.log(last); // return 4

Page 18: ECMAScript 6 new features

var beatles = [ {firstName: 'John', lastName: 'Lennon'}, {firstName: 'Paul', lastName: 'McCartney'}, {firstName: 'Ringo', lastName: 'Starr'}, {firstName: 'Georges', lastName: 'Harrison'}];

beatles.forEach(({firstName}) => console.log(firstName));

function logLastName({lastName}) { console.log(lastName); }

var [, paul] = beatles;

logLastName(paul); // return McCartney

Page 19: ECMAScript 6 new features

Array comprehension

var nums = [1, 2, 3, 4, 5];var aboveTwo = [num for(num of nums) if(num > 2)];console.log(aboveTwo); // return [3, 4, 5]

Page 20: ECMAScript 6 new features

Generatorsfunction* gen() { console.log(`You called 'next()'`); yield 'hello'; console.log(`You called 'next()' again`); yield 'world';}

let genResult = gen();console.log(genResult);let next = genResult.next();console.log(next);let done = genResult.next();console.log(done);

for (let output of gen()) { console.log(output);}

Page 21: ECMAScript 6 new features

Build things through iteration process

function* gen2() { let temp = yield 'how'; temp = yield temp + 'is'; yield temp + 'possible?'; } let gen2result = gen2(); console.log(gen2result.next().value); console.log(gen2result.next(' the heck ').value); console.log(gen2result.next(' this crazy ').value);

how the heck is

this crazy possible?

Page 22: ECMAScript 6 new features

work with infinite sequence

function* seq() { let x = 0; let y = 0; while (true) { yield x + y; x += 1; y += 2; }}var seqGen = seq();console.log(seqGen.next().value);console.log(seqGen.next().value);console.log(seqGen.next().value);

Page 23: ECMAScript 6 new features

Classes// ES5var Shape = function (id, x, y) { this.id = id; this.move(x, y);};Shape.prototype.move = function (x, y) { this.x = x; this.y = y;};

// ES6class Shape { constructor (id, x, y) { this.id = id this.move(x, y) } move (x, y) { this.x = x this.y = y }}

Page 24: ECMAScript 6 new features

Class Inheritance// ES5var Rectangle = function (id, x, y, width, height) { Shape.call(this, id, x, y); this.width = width; this.height = height;};

Rectangle.prototype = Object.create(Shape.prototype);Rectangle.prototype.constructor = Rectangle;var Circle = function (id, x, y, radius) { Shape.call(this, id, x, y); this.radius = radius;};Circle.prototype = Object.create(Shape.prototype);Circle.prototype.constructor = Circle;

// ES6class Rectangle extends Shape { constructor (id, x, y, width, height) { super(id, x, y) this.width = width this.height = height }}class Circle extends Shape { constructor (id, x, y, radius) { super(id, x, y) this.radius = radius }}

Page 25: ECMAScript 6 new features

Binary and Octal Literals

// ES60b111110111 === 503 // true0o767 === 503 // true

// ES5parseInt("111110111", 2) === 503;parseInt("767", 8) === 503;0767 === 503; // only in non-strict, backward compatibility mode

Page 26: ECMAScript 6 new features

Meta-Programminglet target = { foo: "Welcome, foo"}let proxy = new Proxy(target, { get (receiver, name) { return name in receiver ? receiver[name] : `Hello, ${name}` }})proxy.foo === "Welcome, foo"proxy.world === "Hello, world"

Proxy

let obj = { a: 1 }Object.defineProperty(obj, "b", { value: 2 })obj[Symbol("c")] = 3Reflect.ownKeys(obj) // [ "a", "b", Symbol(c) ]

Reflection

Page 27: ECMAScript 6 new features

Some more

• Typed arrays

• Promises

• Modules improvement

Page 28: ECMAScript 6 new features

Conclusion

Some very good features… but not only :)

Page 29: ECMAScript 6 new features

?

Page 30: ECMAScript 6 new features

References

• https://egghead.io

• https://github.com/lukehoban/es6features

• http://es6-features.org/

• https://en.wikipedia.org/wiki/ECMAScript