ES6 is coming and it’s chock-full of really amazing features. PayPal recently started adopting certain ES6 features and Jamund will show what parts of the language they are using, how they are able to use it today and the performance impact.
6. // 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”));
7. // 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”));
10. 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);
}
11. // 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
});
}
30. 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 :(
34. 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!
43. 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
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.
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.
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.
Helps you focus on the things you want to do, not how you’re going to do it.
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.
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!
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.
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!
Oh and there’s more. Yes, let fixes that too! So don’t hate on let. It’s pretty great :) I love let
Pretty straight forward.
Pretty straight forward.
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.
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.
Here is how that might look without destructuring. It’s not terrible to look at, but it’s 3 lines of code.
In a more advanced case you can also create variable declarations from nested objects.
This is from real code at paypal and i think gave me glimpse of just how nice this sugar can feel.
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
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;
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.
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.
Now how about that? That’s much better! ES6 gives us. Now that to me is finally readable.
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.
first step..
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…
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
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.
There are 2 issues with the error.
#1 wrong path
#2 wrong line number
Who’s heard of source maps?
THEY THINK OF EVERYTHING!!!!
Adding —source-maps
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.
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.
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!
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
I’m really excited about it, but it doesn’t support half the things I demoed here today!
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
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