es6 at paypal
TRANSCRIPT
// 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”));
// 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”));
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);
}
// 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
});
}
Default Parameters
function printAge(a = 10) {
console.log(a);
}
printAge(); // “10”
printAge(20); // “20”
Default Parameters
function validate(model) {
model = model || this.toJSON();
return model.age >= 21;
}
function validate(model = this.toJSON()) {
return model.age >= 21;
}
Destructuring
var person = {
name: “Simon McKenzie”,
age: 24,
devices: {
iphone: true,
galaxyS3: true
}
};
var name = person.name,
age = person.age,
iphone = person.devices.iphone;
Destructuring
var person = {
name: “Simon McKenzie”,
age: 24,
devices: {
iphone: true,
galaxyS3: true
}
};
var { name, age, devices: { iphone } } = person;
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
});
}
/* … */
}
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
});
}
/* … */
}
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
});
}
/* … */
}
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
});
}
/* … */
}
handleServiceErrors: function(model, { error: { code = ‘’ }, data = {} }) {
if (code.includes('identity')) {
this.set({
user: data.user,
showDOB: code.includes('DOB'),
showAddress: code.includes('ADDRESS')
});
}
/* … */
}
{
“scripts”: {
“start”: “npm run compile && node dist/app”,
“compile”: “6to5 —-out-dir dist src”
}
}
Update package.json
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 :(
Update package.json
{
“scripts”: {
“start”: “npm run compile && node dist/app”,
“compile”: “6to5 -—source-maps —-out-dir dist src”
}
}
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!
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