Top Banner
Fat Arrows
24

Fat Arrow (ES6)

Jul 15, 2015

Download

Software

Ryan Ewing
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Fat Arrow (ES6)

Fat$Arrows

Page 2: Fat Arrow (ES6)

=>

Page 3: Fat Arrow (ES6)

What?

lightweight(synax(for(lightweight(func3ons

Page 4: Fat Arrow (ES6)

No#Parameters,#No#Statements

let empty = () => {};empty(); // undefined

Page 5: Fat Arrow (ES6)

One$Parameter,$One$Statement

let identity = x => x;identity(1); // 1

implicit'return

Page 6: Fat Arrow (ES6)

Two$Parameters,$One$Expression

let adder = (x,y) => x + y;adder(3,5); // 8

note%the%()%around%arguments

Page 7: Fat Arrow (ES6)

Mul$ple'Statements

let doubleDouble = (value) => { let double = value * 2; return double * 2;};doubleDouble(4); // 16

note%the%{}

Page 8: Fat Arrow (ES6)

Return'Object

let cte = () => ({"travel": true, "expense": true});cte();// Object { travel: true, expense: true }

note%the%()%around%{}

Page 9: Fat Arrow (ES6)

Lexical(vs(Dynamic(this

function hasBeenBought(company, cb){ cb(true); }function Company() { this.name = 'Concur'; }

Company.prototype.newName = function() { console.log(this.name); hasBeenBought(this.name, function(bool) { if (bool) console.log(this.name + ', an SAP Company');

});}let concur = new Company();concur.newName();

Page 10: Fat Arrow (ES6)

Lexical(vs(Dynamic(this

function hasBeenBought(company, cb){ cb(true); }function Company() { this.name = 'Concur'; }

Company.prototype.newName = function() { console.log(this.name); // "Concur" hasBeenBought(this.name, function(bool) { if (bool) console.log(this.name + ', an SAP Company'); // ", an SAP Company" });}let concur = new Company();concur.newName();

Page 11: Fat Arrow (ES6)

Lexical(vs(Dynamic(this(with(=>

function hasBeenBought(company, cb){ cb(true); }function Company() { this.name = 'Concur'; }

Company.prototype.newName = function() { console.log(this.name); hasBeenBought(this.name, bool => { if (bool) console.log(this.name + ', an SAP Company');

});}let concur = new Company();concur.newName();

Page 12: Fat Arrow (ES6)

Lexical(vs(Dynamic(this(with(=>

function hasBeenBought(company, cb){ cb(true); }function Company() { this.name = 'Concur'; }

Company.prototype.newName = function() { console.log(this.name); // "Concur" hasBeenBought(this.name, bool => { if (bool) console.log(this.name + ', an SAP Company'); // "Concur, an SAP Company" });}let concur = new Company();concur.newName();

Page 13: Fat Arrow (ES6)

Lexical(Arguments

function f() { var args = arguments; var g = () => arguments; assert.equal(args, g())}f()

arrow%func*ons%reference%the%arguments%from%the%parent

Page 14: Fat Arrow (ES6)

Yield&with&func.on

function* incr() { var index = 0; while (true) { yield index++; }}let forever = incr();forever.next() // 0forever.next() // 1forever.next() // 2

Page 15: Fat Arrow (ES6)

Yield&with&arrow

noIncr = () => { var index = 0; while (true) { yield index++; }}

// SyntaxError: arrow function may not contain yield

Page 16: Fat Arrow (ES6)

new

function Expense(){}let dinner = new Expense();

// !

Page 17: Fat Arrow (ES6)

new$with$=>

let Expense = () => {};let dinner = new Expense();// TypeError: Expense is not a constructor

Page 18: Fat Arrow (ES6)

Strict&Mode&(example&in&a&Browser&Environment)

var arrow = () => {'use strict'; return this};var notArrow = function() {'use strict'; return this};arrow() === window; // truenotArrow() === window; // false

In#strict#mode#a#func0on's#this#is#undefined

Page 19: Fat Arrow (ES6)

Shorter

var a2 = a.map( function( s ){ return s.length } );

var a3 = a.map( s => s.length );

Page 20: Fat Arrow (ES6)

Real%Example%(Before)

$http({url:url}).then(function(response) { deferred.resolve(response.data.items); },function(response) { deferred.reject(response.data); })

Page 21: Fat Arrow (ES6)

Real%Example%(A,er)

$http({url:url}).then( response => deferred.resolve(response.data.items), error => deferred.reject(error.data))

Page 22: Fat Arrow (ES6)

When%to%use%=>

• you%want%the%correct%this

• when%function(){}%would%be%too%verbose

• list%processing%(map,%filter,%reduce,%etc)

• anonymous%func<ons

Page 23: Fat Arrow (ES6)

When%not%to%use%=>

• you%need%a%constructor%(new)

• you%need%a%generator%(func*)

• you%need%to%touch%this.

• (apply,%call,%bind%won't%change%this%on%arrows)

Page 24: Fat Arrow (ES6)

!