Impress Your Friends with EcmaScript 2015

79
EcmaScript 2015 impress your friends at the party

Transcript of Impress Your Friends with EcmaScript 2015

Page 1: Impress Your Friends with EcmaScript 2015

EcmaScript 2015impress your friends at the party

Page 2: Impress Your Friends with EcmaScript 2015

What is es6?

Page 3: Impress Your Friends with EcmaScript 2015

Why es6?

Page 4: Impress Your Friends with EcmaScript 2015

Is it Safe to Us Es6?!?!

Page 5: Impress Your Friends with EcmaScript 2015

https://kangax.github.io/compat-table/es6/

Page 6: Impress Your Friends with EcmaScript 2015

How do we use Es6?

Page 7: Impress Your Friends with EcmaScript 2015
Page 8: Impress Your Friends with EcmaScript 2015
Page 9: Impress Your Friends with EcmaScript 2015

https://github.com/rauschma/webpack-es6-demo

Page 10: Impress Your Friends with EcmaScript 2015

https://github.com/angular-class/NG6-starter

Page 11: Impress Your Friends with EcmaScript 2015

Syntactic SugAr

Page 12: Impress Your Friends with EcmaScript 2015
Page 13: Impress Your Friends with EcmaScript 2015

Es6 Classes

Page 14: Impress Your Friends with EcmaScript 2015
Page 15: Impress Your Friends with EcmaScript 2015

ES6 Classes

• Simple sugar over the the prototype-based OO pattern• A more convenient form makes classes easier to use • Classes support inheritance, super calls, instance and static methods and constructors

Page 16: Impress Your Friends with EcmaScript 2015

class Project { constructor(name) { this.name = name; } start() { return `Project ${this.name} starting`; } } var project = new Project("Website"); project.start(); // "Project Website starting"

Page 17: Impress Your Friends with EcmaScript 2015

Es6 Inheritance

Page 18: Impress Your Friends with EcmaScript 2015

ES6 INheritance

• Inheritance is possible via the prototype• You can inherit from a base class using the extends keyword• You must call super before you can access this in a subclass

Page 19: Impress Your Friends with EcmaScript 2015

class Shape { … toString () { return `Shape(${this.id}) ̀ } } class Rectangle extends Shape { constructor (id, x, y, width, height) { super(id, x, y) … } toString () { return "Rectangle > " + super.toString() } } class Circle extends Shape { constructor (id, x, y, radius) { super(id, x, y) … } toString () { return "Circle > " + super.toString() } }

Page 20: Impress Your Friends with EcmaScript 2015

Es6 Modules

Page 21: Impress Your Friends with EcmaScript 2015

ES6 MODULES

• Language-level support for modules for component definition• Best of both both CommonJS and AMD • Named exports and default exports

Page 22: Impress Your Friends with EcmaScript 2015

//------ lib.js ------export const sqrt = Math.sqrt; export function square(x) { return x * x; } export function diag(x, y) { return sqrt(square(x) + square(y)); } //------ main.js ------import { square, diag } from 'lib'; console.log(square(11)); // 121console.log(diag(4, 3)); // 5

Page 23: Impress Your Friends with EcmaScript 2015

//------ main.js ------import * as lib from 'lib'; console.log(lib.square(11)); // 121console.log(lib.diag(4, 3)); // 5

Page 24: Impress Your Friends with EcmaScript 2015

//------ myFunc.js ------export default function () { ... }; //------ main1.js ------import myFunc from 'myFunc'; myFunc();

Page 25: Impress Your Friends with EcmaScript 2015

//------ MyClass.js ------export default class { ... }; //------ main2.js ------import MyClass from 'MyClass'; let inst = new MyClass();

Page 26: Impress Your Friends with EcmaScript 2015

Es6 Block Level Scoping

Page 27: Impress Your Friends with EcmaScript 2015
Page 28: Impress Your Friends with EcmaScript 2015

ES6 MODULES

• Function scope can be tricky!• We can scope variables at a block level using let and const

Page 29: Impress Your Friends with EcmaScript 2015

function printName() { if(true) { var name = "Rafael"; } console.log(name); // Rafael}

Page 30: Impress Your Friends with EcmaScript 2015

function printName() { var name; // variable declaration is hoisted to the top if(true) { name = "Rafael"; } console.log(name); // Rafael}

Page 31: Impress Your Friends with EcmaScript 2015

function printName() { if(true) { let name = "Rafael"; } console.log(name); // ReferenceError: name is not defined}

Page 32: Impress Your Friends with EcmaScript 2015

function printName() { var name = "Hey"; if(true) { let name = "Rafael"; console.log(name); // Rafael } console.log(name); // Hey}

Page 33: Impress Your Friends with EcmaScript 2015
Page 34: Impress Your Friends with EcmaScript 2015

if (true) { // enter new scope, TDZ starts // Uninitialized binding for `tmp` is created tmp = 'abc'; // ReferenceError console.log(tmp); // ReferenceError let tmp; // TDZ ends, `tmp` is initialized with `undefined ̀ console.log(tmp); // undefined tmp = 123; console.log(tmp); // 123}

Page 35: Impress Your Friends with EcmaScript 2015

Es6 Arrow functions

Page 36: Impress Your Friends with EcmaScript 2015
Page 37: Impress Your Friends with EcmaScript 2015

ES6 Arrow Functions

• Arrow function expression aka fat arrow functions are a shorter syntax• Lexically binds the this value• Arrow functions are always anonymous

Page 38: Impress Your Friends with EcmaScript 2015

var numbers = [1,2,3,4,5]; var timesTwo = numbers.map(function (number) { return number * 2; }); console.log(timesTwo); // [2, 4, 6, 8, 10]

Page 39: Impress Your Friends with EcmaScript 2015

var numbers = [1,2,3,4,5]; var timesTwo = numbers.map((number) => number * 2); console.log(timesTwo); // [2, 4, 6, 8, 10]

Page 40: Impress Your Friends with EcmaScript 2015

var numbers = [1,2,3,4,5]; var timesTwo = numbers.map(number => number * 2); console.log(timesTwo); // [2, 4, 6, 8, 10]

Page 41: Impress Your Friends with EcmaScript 2015

function FooCtrl (FooService) { this.foo = 'Hello'; FooService .doSomething(function (response) { this.foo = response; }); }

Page 42: Impress Your Friends with EcmaScript 2015

function FooCtrl (FooService) { this.foo = 'Hello'; FooService .doSomething(function (response) { this.foo = response; }.bind(this)); }

Page 43: Impress Your Friends with EcmaScript 2015

function FooCtrl (FooService) { var that = this; that.foo = 'Hello'; FooService .doSomething(function (response) { that.foo = response; }); }

Page 44: Impress Your Friends with EcmaScript 2015

function FooCtrl (FooService) { this.foo = 'Hello'; FooService .doSomething((response) => { // woo, pretty this.foo = response; }); }

Page 45: Impress Your Friends with EcmaScript 2015

function FooCtrl (FooService) { this.foo = 'Hello'; FooService .doSomething(response => this.foo = response); }

Page 46: Impress Your Friends with EcmaScript 2015

Es6 Template Strings

Page 47: Impress Your Friends with EcmaScript 2015

ES6 Template strings

• Multi-line strings• Expression interpolation• Do not let untrusted users construct template strings!

Page 48: Impress Your Friends with EcmaScript 2015

console.log("string text line 1\n"+ "string text line 2"); // "string text line 1// string text line 2"

Page 49: Impress Your Friends with EcmaScript 2015

console.log(`string text line 1 string text line 2`); // "string text line 1// string text line 2"

Page 50: Impress Your Friends with EcmaScript 2015

var a = 5; var b = 10; console.log("Fifteen is " + (a + b) + " and\nnot " + (2 * a + b) + "."); // "Fifteen is 15 and// not 20."

Page 51: Impress Your Friends with EcmaScript 2015

var a = 5; var b = 10; console.log(`Fifteen is ${a + b} and\nnot ${2 * a + b}.`); // "Fifteen is 15 and// not 20."

Page 52: Impress Your Friends with EcmaScript 2015

Es6 Object Literals

Page 53: Impress Your Friends with EcmaScript 2015

ES6 Object Literals

• Object literals are extended to be more convenient and more like defining a class• Better property and method declarations

Page 54: Impress Your Friends with EcmaScript 2015

var x = 10; var y = 30; var coordinates = { x: x, y: y}; console.log(coordinates); // { x: 10, y: 30 }

Page 55: Impress Your Friends with EcmaScript 2015

let x = 10; let y = 30; let coordinates = { x, y }; console.log(coordinates); // { x: 10, y: 30 }

Page 56: Impress Your Friends with EcmaScript 2015

let x = 10; let y = 30; let coordinates = { x, y, z: 10, }; console.log(coordinates); // { x: 10, y: 30, z: 10 }

Page 57: Impress Your Friends with EcmaScript 2015

var cart = { _items: [], addItem: function(item) { this._items.push(item); return this; }, toString: function() { return this._items.join(', '); } } cart.addItem('apple') .addItem('orange') .addItem('banana'); console.log(cart.toString()); // apple, orange, banana

Page 58: Impress Your Friends with EcmaScript 2015

var cart = { _items: [], addItem(item) { this._items.push(item); return this; }, toString() { return this._items.join(', '); } } cart.addItem('apple') .addItem('orange') .addItem('banana'); console.log(cart.toString()); // apple, orange, banana

Page 59: Impress Your Friends with EcmaScript 2015

Es6 Array API

Page 60: Impress Your Friends with EcmaScript 2015

ES6 Array API

• Array.from converts array-like objects into arrays • Array.keys, Array.values and Array.entries are handy for iterating over arrays• Array.find returns the first element that the callback returns true •Array.findIndex returns the index of the first element that the callback returns true

Page 61: Impress Your Friends with EcmaScript 2015

// Array-like object (arguments) to Arrayfunction f() { return Array.from(arguments); } f(1, 2, 3); // [1, 2, 3]// Any iterable object...// Setvar s = new Set(["foo", window]); Array.from(s); // ["foo", window]// Mapvar m = new Map([[1, 2], [2, 4], [4, 8]]); Array.from(m); // [[1, 2], [2, 4], [4, 8]]// StringArray.from("foo"); // ["f", "o", "o"]

Page 62: Impress Your Friends with EcmaScript 2015

var arr = ["a", "b", "c"]; var iterator = arr.keys(); console.log(iterator.next()); // { value: 0, done: false }console.log(iterator.next()); // { value: 1, done: false }console.log(iterator.next()); // { value: 2, done: false }console.log(iterator.next()); // { value: undefined, done: true }

Page 63: Impress Your Friends with EcmaScript 2015

var arr = ['w', 'y', 'k', 'o', 'p']; var eArr = arr.values(); console.log(eArr.next().value); // wconsole.log(eArr.next().value); // yconsole.log(eArr.next().value); // kconsole.log(eArr.next().value); // oconsole.log(eArr.next().value); // p

Page 64: Impress Your Friends with EcmaScript 2015

var arr = ['a', 'b', 'c']; var eArr = arr.entries(); console.log(eArr.next().value); // [0, 'a']console.log(eArr.next().value); // [1, 'b']console.log(eArr.next().value); // [2, 'c']

Page 65: Impress Your Friends with EcmaScript 2015

function isPrime(element, index, array) { var start = 2; while (start <= Math.sqrt(element)) { if (element % start++ < 1) { return false; } } return element > 1; } console.log([4, 6, 8, 12].find(isPrime)); // undefined, not foundconsole.log([4, 5, 8, 12].find(isPrime)); // 5

Page 66: Impress Your Friends with EcmaScript 2015

function isPrime(element, index, array) { var start = 2; while (start <= Math.sqrt(element)) { if (element % start++ < 1) { return false; } } return element > 1; } console.log([4, 6, 8, 12].findIndex(isPrime)); // -1, not foundconsole.log([4, 6, 7, 12].findIndex(isPrime)); // 2

Page 67: Impress Your Friends with EcmaScript 2015

Es6 SET

Page 68: Impress Your Friends with EcmaScript 2015

ES6 SET

• Lets you store unique values of any type• You can store primitive values or object references

Page 69: Impress Your Friends with EcmaScript 2015

var mySet = new Set(); mySet.add(1); mySet.add(5); mySet.add("some text"); mySet.has(1); // truemySet.has(3); // false, 3 has not been added to the setmySet.has(5); // truemySet.has(Math.sqrt(25)); // truemySet.has("Some Text".toLowerCase()); // truemySet.size; // 3mySet.delete(5); // removes 5 from the setmySet.has(5); // false, 5 has been removedmySet.size; // 2, we just removed one value

Page 70: Impress Your Friends with EcmaScript 2015

Es6 MAP

Page 71: Impress Your Friends with EcmaScript 2015

ES6 MAP

• Simple key/value map• Any value (object or primitive value) can be used as either a key or a value

Page 72: Impress Your Friends with EcmaScript 2015

var myMap = new Map(); var keyObj = {}, keyFunc = function () {}, keyString = "a string"; // setting the valuesmyMap.set(keyString, "value associated with 'a string'"); myMap.set(keyObj, "value associated with keyObj"); myMap.set(keyFunc, "value associated with keyFunc"); myMap.size; // 3// getting the valuesmyMap.get(keyString); // "value associated with 'a string'"myMap.get(keyObj); // "value associated with keyObj"myMap.get(keyFunc); // "value associated with keyFunc"myMap.get("a string"); // "value associated with 'a string'" // because keyString === 'a string' myMap.get({}); // undefined, because keyObj !== {}myMap.get(function() {}) // undefined, because keyFunc !== function () {}

Page 73: Impress Your Friends with EcmaScript 2015

Es6 Resources

Page 74: Impress Your Friends with EcmaScript 2015

https://leanpub.com/exploring-es6/

Page 75: Impress Your Friends with EcmaScript 2015

http://es6-features.org/

Page 76: Impress Your Friends with EcmaScript 2015

http://es6katas.org/

Page 77: Impress Your Friends with EcmaScript 2015

http://www.es6fiddle.net/ http://es6-features.org/ http://www.2ality.com/ https://github.com/lukehoban/es6features http://codepen.io/bradleyboy/posts/getting-to-know-es6-object-literals http://jamesknelson.com/es6-the-bits-youll-actually-use http://jamesknelson.com/es6-cheatsheet.png

Page 78: Impress Your Friends with EcmaScript 2015
Page 79: Impress Your Friends with EcmaScript 2015

Thank you!