View Slide
@Jack_Franklin
@pusher
ES2015: finished!
https://kangax.github.io/compat-table/es6/
It’s happening!• Edge 14: 90%• Chrome 52 / Opera 39: 98%• FF 49: 93%• Safari 10: 100%• Node 6: 93%
Arrow Functions
1 [1, 2, 3].map(function(numbers) {2 return numbers * 2;3 });45 [1, 2, 3].map(numbers => numbers * 2);
undefined 'Tom'undefined 'Zoe'1 var person = {2 name: 'Jack',3 friends: ['Tom', 'Zoe'],4 logFriends: function() {5 this.friends.forEach(function(f) {6 console.log(this.name, f);7 });8 }9 }
'Jack' 'Tom''Jack' 'Zoe'1 var person = {2 name: 'Jack',3 friends: ['Tom', 'Zoe'],4 logFriends: function() {5 this.friends.forEach(f => {6 console.log(this.name, f);7 });8 }9 }
Object Literals
1 var literal = {2 foo: function() {...},3 bar() {...}4 }
1 var newKey = 'foo';2 var newObj = {};3 newObj[newKey] = true;45 var otherNewObj = {6 [newKey]: true7 };
Template Strings
1 var str = '2 + 2 is: ' + (2 + 2);23 var otherStr = `2 + 2 is: ${2 + 2}`;
1 var str = `they can2 span multiple34 lines and stuff5 `;
Destructuring
1 var array = [1, 2, 3];23 var [first, ...rest] = array;45 first // 16 rest // [2, 3]
1 var person = {2 name: 'Jack',3 age: 244 };56 var { name, age } = person;78 name // 'Jack'9 age // 24
1 var getTweetInfo = function() {...};23 var { text, user } = getTweetInfo();
1 var person = {2 name: {3 first: 'Jack',4 last: 'Franklin'5 }6 };78 var { name: { first } } = person;910 first // 'Jack';
1 var person = {2 name: {3 first: 'Jack',4 last: 'Franklin'5 }6 };78 var { name: { first: foo } } = person;910 foo // 'Jack'11 first // ReferenceError
The rest/spread/splatoperator
1 var numbers = [1, 2, 3]2 var moreNumbers = [...numbers, 4, 5];3 // moreNumbers [1, 2, 3, 4, 5]
Function Arguments
1 function foo(x = 1) {2 return x;3 }45 foo() // 16 foo(2) // 2
1 function foo(...args) {2 log('got args', args);3 }45 function bar() {...};67 var args = [1, 2, 3];8 bar.apply(this, args);9 bar(...args);
1 function foo(obj) {2 return obj.x + obj.y;3 };45 function foo({ x, y }) {6 return x + y;7 }
Classes
1 class Person {2 constructor(name) {3 this.name = name;4 }56 fullName() {7 return `Name is ${this.name}`;8 }9 }1011 var jack = new Person('Jack');12 jack.fullName();
Modules
1 // foo.js2 export default function() {3 return 2;4 }56 //main.js7 import foo from './foo';8 foo(); // 2
1 // foo.js2 function foo() {3 return 2;4 }56 export { foo };78 // main.js9 import { foo } from './foo';10 foo(); // 2
each module has its own scope
static imports and exports
tree shaking
1 // some 3rd party library2 export function merge() {...};3 export function filter() {...};4 export function map() {...};56 // your app78 import { filter } from 'third-party';
1 // final bundle2 function filter() {...};3 ...your app code...
const and let
scope
window (global) scopefunction scope
foo = 2bar = 3x = fnbaz = 41 var foo = 2;23 function x() {4 bar = 3;5 var baz = 4;6 }
xfoo = 1bar = 21 function x() {2 var bar = 2;3 if (...) {4 var foo = 1;5 }6 }
window (global) scopefunction scopeblock scopeletconst
xbar = 2foo = 1baz = 31 function x() {2 var bar = 2;3 if (...) {4 let foo = 1;5 const baz = 3;6 }7 }
never var, always let
and maybe even const
1 const x = 2;2 x = 3; // NOPE!34 const y = { a: 1 };5 y = { b: 1 }; // NOPE!67 y.a = 2; //...yeah8 delete y.a; //...yeah9 y.b = 3; //...yeah
so much more!sets, maps, proxies,symbols, generators, newobject APIs
Never again
ES2016** operatorArray.prototype.includes
Thanks!@Jack_Franklin