Ignite es6

22

Transcript of Ignite es6

Page 1: Ignite es6
Page 2: Ignite es6

The Future of JavaScript with ES6

Page 3: Ignite es6
Page 4: Ignite es6

Banning the var statement

function loop (array) { for (var index = 0; i < array.length; index++) { /* do stuff */ } console.log(index); // => STILL AVAILABLE??? }

Page 5: Ignite es6

Use let instead

function loop (array) { for (let index = 0; i < array.length; index++) { /* do stuff */ } console.log(index); // Not anymore!!! }

Page 6: Ignite es6

There’s also const

const x = 1; x = 2; // not possible!

const obj = { x: 1 }; obj.x = 2; // possible!

Page 7: Ignite es6

Remember anonymous functions?

[1,2,3,4].map(function(n){ return n * n; });

Page 8: Ignite es6

Hello arrow functions!

[1,2,3,4].map((n) => n * n);

Page 9: Ignite es6

Also default parameters are in town!

function (x, y = 2) { return x + y);

Page 10: Ignite es6

Consider this

Page 11: Ignite es6

Getting the rest

function addPlayersToTeam (team /*, players*/) { var players = arguments.slice(1); for (var i = 0; args.length; i++) { team.addPlayer(args[i]); } }

Page 12: Ignite es6

Thank you rest parameter

function addPlayersToTeam (team, …rest) { rest.forEach((player) => team.addPlayer(player)); }

Page 13: Ignite es6

Destructing your object

var {a, c} = {a: 1, b: 2, c: 3}

console.log(a); // logs 1

Page 14: Ignite es6

Destructing arrays

var foo = [“one”, “two”, “three" ] var one = foo[0]; var [one, two] = foo;

Page 15: Ignite es6

Easier Recursive

function sum ([x, …rest]) { if (x) return x + sum(rest); else return 0;

}

console.log(sum([1,2,3]));

Page 16: Ignite es6

Fetch me some data!

fetch(‘/some/url’) .then(function(response){ return response.json(); }) .then(function(json) {

// something with json }).catch(function(err) {

// error :( });

Page 17: Ignite es6

Fetch me some data!

fetch(‘/some/url’) .then( (response) => response.json() ) .then( (json) => doStuff(json) ) .catch( (error) => handleError(error) );

Page 18: Ignite es6

Template my string

var a = 5; var b = 10; console.log(`Fifteen is ${a+b} and not ${2*a + b}`);

Page 19: Ignite es6

Template my stringvar user = { … }; var html = ` <div> <span>${user.username}</span>

</div> `

Page 20: Ignite es6

Enhanced Object Literals

var obj = { __proto__: protoObj, handler

}

Page 21: Ignite es6

Enhanced Object Literals

var obj = { toString () { return super.toString(); }, [‘prop_’+key()]: key()

}

Page 22: Ignite es6

How to use ES6 NOW