[go: up one dir, main page]

SlideShare a Scribd company logo
ES6 at PayPal
Early Learnings
Why ES6?
ES6 standardizes
common practices.
ES6 helps us write
more expressive code.
Expressive code is
easier to understand.
// 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”));
SYNTACTIC SUGAR
ES6 Basics
• block scope (let and const)
• default parameters
• destructuring
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 { EventEmitter } = require(“events");
var { map, each } = require(“underscore”);
Destructuring
var person = {
name: “Simon McKenzie”,
age: 24
};
var { name, age } = person;
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;
Real-life Example
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')
});
}
/* … */
}
SUGAR IS AN EASY SELL
Adding ES6 to your
infrastructure is
not complicated.
It’s super easy.
npm install 6to5
{
“scripts”: {
“start”: “npm run compile && node dist/app”,
“compile”: “6to5 —-out-dir dist src”
}
}
Update package.json
npm start
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 :(
Did you know node has
source maps?
Update package.json
{
“scripts”: {
“start”: “npm run compile && node dist/app”,
“compile”: “6to5 -—source-maps —-out-dir dist src”
}
}
require(‘source-map-support’).install()
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!
It’s that simple.
Use ES6 Today
The End
@xjamundx
Demos: https://github.com/xjamundx/es6-test
What about io.js?
ES6 at PayPal
ES6 at PayPal
What about JSHint?
How about the front-
end?
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
header.jsheader.es6
IDE Support

More Related Content

ES6 at PayPal

Editor's Notes

  1. Hi I’m Jamund. I’m a UI Engineer at PayPal and I love JavaScript and I’m excited about where it’s going. We’ve been exploring ES6 at PayPal and think it’s pretty great.
  2. The first thing you might be asking yourself is…..Why do we need MOAR javascript? Javascripts relatively stable. It’s been around for almost 20 years. It now powers systems all around the planet and it’s fairly well understood. So what’s all the fuss about ES6.
  3. From the Class syntax to Promises. ES6 finally gives us the tools we need to write the programs we’ve already been writing. But now we can do it in a standard way and hopefully with less bugs.
  4. Helps you focus on the things you want to do, not how you’re going to do it.
  5. Here’s weird example of some of the rough edges of javascript. We’re using the arguments objects, which is not QUITE an array to add a variable number of people to our world.
  6. With es6 we now have something called REST Parameters, which means I don’t have to dive into the weird arguments object. I have first lass support for this behavior!
  7. Now people call this syntactic sugar. Many of the things I’m going to talk about today are not things you couldn’t do before,but ES6 has made those common patterns a lot easier! I’m going to share some examples.
  8. it’s simpler. to the point. more expressive. it may seem like small thing but it’s actually pretty awesome in cleaning up large functions and keeping like code near itself. It’s easier to read large functions with large if blocks and much easier to re-factor them!
  9. Oh and there’s more. Yes, let fixes that too! So don’t hate on let. It’s pretty great :) I love let
  10. Pretty straight forward.
  11. Pretty straight forward.
  12. This is the simplest example of destructuring with objects. It turns out this comes in handy all the time with node modules that return multiple values.
  13. This is the simplest example of destructuring with objects. It turns out this comes in handy all the time with node modules that return multiple values.
  14. Here is how that might look without destructuring. It’s not terrible to look at, but it’s 3 lines of code.
  15. In a more advanced case you can also create variable declarations from nested objects.
  16. This is from real code at paypal and i think gave me glimpse of just how nice this sugar can feel.
  17. Here’s a fairly clumsy function that handles errors with our services. we parse some data about the error response, check for other data and then based on the error code set various things on the model. The first thing we’re going to do is apply LET
  18. Let will help us move code that only belongs in a single if block out of the function scope, keeping it encapsulated from other blocks of code below. It’s a small thing, but it ends up being really nice and making for much more readable blocks of code. Next up we’ll apply 2 things. This is a combination of restructuring and default paramers;
  19. Let’s see how this works. 1. We are saying that instead of “response” we’re expecting an object with a data property and an error property. The error property is also an object, which has a code property and we’d like that to always exist as an empty string. That just elimated the last 3 function-scoped variables we had defined. and will now guarantee that we have a data object and string called code. It’s pretty amazing. Next up we’ll do some not ES5 stuff.
  20. This is just a small optimization. Now that we’ve moved the showAddress and showDOB functions outside of the function scope, we can see now that they really weren’t that useful, we can just combine the values directly into the object being passed into the set function. But wait ES6 also has some sugar to help us here as well, because this isn’t terrible readable. It’s just not expressive.
  21. Now how about that? That’s much better! ES6 gives us. Now that to me is finally readable.
  22. There’s this guy I work with Paul. he But there’s more. At PayPal so far we’ve found that sugar is an easy sell. It’s easy to understand and easy to teach people. Of course JavaScript today has a lot more than syntactic sugar and new constructs like iterators, promises and generators may yet provide exciting new ways to express complicated async, but it’s okay to start with the easy stuff, the fun stuff. there’s a low barrier to entry.
  23. first step..
  24. install 6to5. this is the MOST compressive and best supported es6 tranpsiler. it’s fantastic. And they just keep adding new things! So let’s open up your package.json file…
  25. So you need to put app.js and all of your files in the src/ directory and then… 6to5 —out-dir dist/ src/ && node dist/app
  26. And it works! It probably adds a second or 2 to your startup depending on the app. But it’s really minimal. And after that there’s virtually no perf difference. You’re running ES5 code through and through. No magic.
  27. There are 2 issues with the error. #1 wrong path #2 wrong line number Who’s heard of source maps?
  28. THEY THINK OF EVERYTHING!!!!
  29. Adding —source-maps
  30. And then in your app.'s file. Go ahead and stick this 1 line in there. This really only does 2 things. It’s not going to crazy and slow you down.. It just Hijacks the stack property of any Error message to give it the right path, and it also hijacks the default uncaughtException handler, which you can easily disable if you want to do your own thing.
  31. With that in place, my stack trace now shows the right line number. And I’ve added exactly 1 line of code my entire project. Bravo. Now there is another way we can add es6 support to our project and I’ve talked to the creator of 6to5 and it’s his preferred approach.
  32. ES6 is fun .It makes your syntax more expressive/readable. There’s so much more than we could cover here, but I hope that Iv’e whet your interest. And that you will go out and install 6to5 and start playing around with theist stuff today!
  33. I use a tool called ESLint, which has limited support for ES6, but literally in the next couple of weeks I expect full support to exist
  34. I’m really excited about it, but it doesn’t support half the things I demoed here today!
  35. So yeah it’s doing way better than node, but it still has a long way to go to catch up to …. you know IE!! And 6to5 and the other transpires
  36. I use a tool called ESLint, which has limited support for ES6, but literally in the next couple of weeks I expect full support to exist
  37. https://www.npmjs.com/package/kraken-devtools-6to5
  38. any files relying on this one can continue using AMD or whatever format and they’ll be oblivious to the change. it just works. opting in is a breeze.
  39. https://www.npmjs.com/package/kraken-devtools-6to5