Top Banner
ES6 at PayPal Early Learnings
45

ES6 at PayPal

Jul 15, 2015

Download

Technology

Jamund Ferguson
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: ES6 at PayPal

ES6 at PayPalEarly Learnings

Page 2: ES6 at PayPal

Why ES6?

Page 3: ES6 at PayPal

ES6 standardizes

common practices.

Page 4: ES6 at PayPal

ES6 helps us write

more expressive code.

Page 5: ES6 at PayPal

Expressive code is

easier to understand.

Page 6: ES6 at PayPal

// create a world with no people

var world = { people: [] };

// make it easy to populate the world

world.populate = function () {

for (var i = 0; i < arguments.length; i++) {

world.people.push(arguments[i]);

}

}

// add some people to the world

world.populate(new Person(“Sally”), new Person(“Joan”));

Page 7: ES6 at PayPal

// create a world with no people

var world = { people: [] };

// makes it easier to populate the world

world.populate = function (...people) {

world.people.push(...people);

}

// add some people to the world

world.populate(new Person(“Sally”), new Person(“Joan”));

Page 8: ES6 at PayPal

SYNTACTIC SUGAR

Page 9: ES6 at PayPal

ES6 Basics

• block scope (let and const)

• default parameters

• destructuring

Page 10: ES6 at PayPal

Block Scope

// es6

let name = getName();

if (name === “jamund”) {

let emotion = “happy”;

makePeople(emotion);

}

// es5 version

var name = getName(),

emotion;

if (name === “jamund”) {

emotion = “happy”;

makePeople(emotion);

}

Page 11: ES6 at PayPal

// let fixes the problem

for (let i = 0; i < 5; i++) {

process.nextTick(function() {

console.log(i); // 0 1 2 3 4

});

}

And then there’s the old

async loop problem

// this happens from time to time

for (var i = 0; i < 5; i++) {

process.nextTick(function() {

console.log(i); // 5 5 5 5 5

});

}

Page 12: ES6 at PayPal

Default Parameters

function printAge(a = 10) {

console.log(a);

}

printAge(); // “10”

printAge(20); // “20”

Page 13: ES6 at PayPal

Default Parameters

function validate(model) {

model = model || this.toJSON();

return model.age >= 21;

}

function validate(model = this.toJSON()) {

return model.age >= 21;

}

Page 14: ES6 at PayPal

Destructuring

var { EventEmitter } = require(“events");

var { map, each } = require(“underscore”);

Page 15: ES6 at PayPal

Destructuring

var person = {

name: “Simon McKenzie”,

age: 24

};

var { name, age } = person;

Page 16: ES6 at PayPal

Destructuring

var person = {

name: “Simon McKenzie”,

age: 24,

devices: {

iphone: true,

galaxyS3: true

}

};

var name = person.name,

age = person.age,

iphone = person.devices.iphone;

Page 17: ES6 at PayPal

Destructuring

var person = {

name: “Simon McKenzie”,

age: 24,

devices: {

iphone: true,

galaxyS3: true

}

};

var { name, age, devices: { iphone } } = person;

Page 18: ES6 at PayPal

Real-life Example

Page 19: ES6 at PayPal

handleServiceErrors: function(model, response) {

var error = response.error,

data = (response && response.data) || {},

code = error.code || '',

showDOB,

showAddress;

if (code.indexOf('identity') !== -1) {

showDOB = code.indexOf('DOB') !== -1;

showAddress = code.indexOf('ADDRESS') !== -1;

this.set({

user: data.user,

showAddress: showAddress,

showDOB: showDOB

});

}

/* … */

}

Page 20: ES6 at PayPal

handleServiceErrors: function(model, response) {

var error = response.error,

data = (response && response.data) || {},

code = error.code || ‘';

if (code.indexOf('identity') !== -1) {

let showDOB = code.indexOf('DOB') !== -1,

showAddress = code.indexOf('ADDRESS') !== -1;

this.set({

user: data.user,

showAddress: showAddress,

showDOB: showDOB

});

}

/* … */

}

Page 21: ES6 at PayPal

handleServiceErrors: function(model, { error: { code = ‘’ }, data = {} }) {

if (code.indexOf(‘identity') !== -1) {

let showDOB = code.indexOf('DOB') !== -1,

showAddress = code.indexOf('ADDRESS') !== -1;

this.set({

user: data.user,

showAddress: showAddress,

showDOB: showDOB

});

}

/* … */

}

Page 22: ES6 at PayPal

handleServiceErrors: function(model, { error: { code = ‘’ }, data = {} }) {

if (code.indexOf(‘identity') !== -1) {

this.set({

user: data.user,

showAddress: code.indexOf('ADDRESS') !== -1,

showDOB: code.indexOf('DOB') !== -1

});

}

/* … */

}

Page 23: ES6 at PayPal

handleServiceErrors: function(model, { error: { code = ‘’ }, data = {} }) {

if (code.includes('identity')) {

this.set({

user: data.user,

showDOB: code.includes('DOB'),

showAddress: code.includes('ADDRESS')

});

}

/* … */

}

Page 24: ES6 at PayPal

SUGAR IS AN EASY SELL

Page 25: ES6 at PayPal

Adding ES6 to your

infrastructure is

not complicated.

Page 26: ES6 at PayPal

It’s super easy.

Page 27: ES6 at PayPal

npm install 6to5

Page 28: ES6 at PayPal

{

“scripts”: {

“start”: “npm run compile && node dist/app”,

“compile”: “6to5 —-out-dir dist src”

}

}

Update package.json

Page 29: ES6 at PayPal

npm start

Page 30: ES6 at PayPal

Error: where am i?

at /Users/jamuferguson/dev/es6-test/dist/routes.js:20:34

at process._tickCallback (node.js:442:13)

Wrong path :(

Wrong line number :(

Page 31: ES6 at PayPal

Did you know node has

source maps?

Page 32: ES6 at PayPal

Update package.json

{

“scripts”: {

“start”: “npm run compile && node dist/app”,

“compile”: “6to5 -—source-maps —-out-dir dist src”

}

}

Page 33: ES6 at PayPal

require(‘source-map-support’).install()

Page 34: ES6 at PayPal

Error: where am i?

at /Users/jamuferguson/dev/es6-test/src/routes.js:15:34

at process._tickCallback (node.js:442:13)

Right path!

Right line number!

Page 35: ES6 at PayPal

It’s that simple.

Page 36: ES6 at PayPal

Use ES6 Today

Page 37: ES6 at PayPal

The End

@xjamundx

Demos: https://github.com/xjamundx/es6-test

Page 38: ES6 at PayPal

What about io.js?

Page 39: ES6 at PayPal
Page 40: ES6 at PayPal
Page 41: ES6 at PayPal

What about JSHint?

Page 42: ES6 at PayPal

How about the front-

end?

Page 43: ES6 at PayPal

Front-end

• Our front-end code takes about 1s to build using 6to5

• For development mode we use a custom middleware

that transpiles silently in the in the background

• 2 step process to opt-in to ES6

Page 44: ES6 at PayPal

header.jsheader.es6

Page 45: ES6 at PayPal

IDE Support