Check out a free preview of the full The Good Parts of JavaScript and the Web course
The "New Good Parts in ES6" Lesson is part of the full, The Good Parts of JavaScript and the Web course featured in this preview video. Here's what you'd learn in this lesson:
ES6 has added more features to the JavaScript languages. Doug talks about what he considers the “new good parts”. These include proper tail calls, the ellipse operator and modules. He also demonstrates how megastring literals can make regular expressions more readable.
Transcript from the "New Good Parts in ES6" Lesson
[00:00:00]
>> [MUSIC]
[00:00:03]
>> Speaker 1: We've got a new version of JavaScript that was approved by the general assembly six months ago and many of the features of it are already in browsers now. And some of them will be coming in later and so this is a good time to look at what's in the new language, did we get any good stuff in it?
[00:00:22]
And the answer is yes we've got actually lot of good stuff in it. So my number one most favorite new feature of the language proper tail calls. So compiler will turn that into a jump instead of a call return, so it'll go a little faster. Take less memory in getting there, it enables continuation passing style in other modes of programming, it's great.
[00:00:46]
So that's my most favorite feature with this JavaScript finally becomes a real functional programming language, which is great. My second most favorite new feature, the ellipsis operator which we've talk about quite a bit yesterday again is two versions of the curry function. There is that one and that one, they both do the same thing.
[00:01:05]
This one's inexcusable, this one's actually pretty nice so that it's going to be a good thing. We've got modules in the language now unfortunately the module system we got is way too complex. But it works, so there's a subset of the module system which is adequate. Which if you just wanna say here's some stuff, I'm gonna export this thing.
[00:01:25]
That's how you use it and that's all it needs to be, and it can do that. So that's good, that's much better than requires, or any of the other module loaders. We've got let in const, which by themselves are not that big a deal. Except they are less confusing to Java programmers.
[00:01:44]
So it's good not to confuse the Java programmers, so I like that. One thing I find confusing about this is that I've seen a lot of developers who are confused about the difference between const and freezing. And so they're surprised that you can put a mutable object in a const and still mutate that object.
[00:02:04]
So constants for variables, freezing is for values, just remember that, it's pretty easy. We have Destructuring, destructuring is something you can do and let and can't statements, and also in assignment statements. Where we can create new variables and initialize them from some object at the same time. So again not a big change, doesn't let us do anything we couldn't do before but it is a convenience, yeah.
[00:02:33]
>> Speaker 2: I think you said freezing is for values, did you mean freezing is for objects?
>> Speaker 1: Objects are values.
>> Speaker 2: Okay, yeah.
>> Speaker 1: In theory you could freeze numbers, except they're already frozen, and strings are already frozen. So you don't need to freeze those.
>> Speaker 2: One follow up, what apart about nested objects?
[00:02:50]
>> Speaker 1: It only freezes the outer one, there is no deep freeze in the language.
>> Speaker 2: So you have to do a recursive freeze.
>> Speaker 1: If you care about that, you have to descend into the language and freeze each level.
>> Speaker 1: Then we have WeakMap, WeakMap is a really nice thing.
[00:03:09]
WeakMap works the way objects should work. With a WeakMap you can take any JavaScript value and use it as a key and then associate any other value with it which is the way object should have worked. And it also has a nice property that it does not route the keys for the purpose of garbage collecting.
[00:03:29]
So if the only place where an object still exists is as the key of a property in a WeakMap, it will get garbage collected automatically, which is brilliant. So that means there is a class of applications which we can write in JavaScript now using WeakMap that we could not write in the language before, which is really nice.
[00:03:49]
The biggest problem with WeakMap is it has maybe the worst name ever put on a feature of a programming language. Because who wants to use something called a WeakMap, right? But it's actually a really good thing. It's just a really bad name and we have Megastring literals. We have the back tick which we can now use to make literals that can spam many lines, so here's are an example of a regular expression.
[00:04:20]
>> Speaker 1: This particular regular expression matches all of the number literals that are an ES6. And you can see why I don't like regular expressions because you can't make any sense out of this right. Just looking at that, it's just noise it's really hard to understand what it's about.
[00:04:37]
So I can make things all easier now, I've got a function here which we'll call the regular expression constructor passing it in a string but first it will remove all of the spaces from that string. And so now, I can use one of these mega literals to write it out with all the whitespace in it.
[00:05:00]
And with whitespace in it, regular expressions aren't quite that bad, we get spaces between. Each of the elements and I can line them up and you can kind of see what's going on, so we now have binary literals. You can have zero B followed by zeros and ones and we've got aka literal which I'm still not sure about those.
[00:05:20]
You've got an 0 for a zero and then octal digits and we've always had the hacks and the floating point. So that's pretty neat, this is a much nicer way of reading regular expressions. There are still things about the mega literals and I don't like. That back tick is the smallest operator we've got and it's used to bracket the biggest structures we've ever had.
[00:05:46]
So there is potential for misreading here so we're gonna have to be cautious with this but I think this is probably a good thing. And again regulex, if you're writing regular expressions even with the whitespace you still wanna be suing regulex. So we've got fat arrow functions or fart for short.
[00:06:10]
So the motivation here, there are people who are complaining that with function you have to type eight letters. You have to F you N, and I said that's too much to type and I said well there are these new things now called keyboard back rows, it's too much to read.
[00:06:30]
Okay, so we added them. So it's a short form for writing a function that will return a value. So we got the parameter list, we got name and then we got the fart and then we've got the return value after it. So, you don't have to write function and you don't have to write return except this will fail.
[00:06:50]
This looks like it's a little mini constructor that's gonna return a new object, right? It's gonna return a new thing where the id is whatever the name is that we passed, this is going to fail. Instead it's going to give you a function that returns undefined, which is not good.
[00:07:06]
And it's because of a syntactic ambiguity in the language, the committee was aware of this when they did it, we just keep adding new bad parts we can't stop doing it. So because of that I'm not a big fan of these.
>> Speaker 3: So it will compile but it won't run.
[00:07:23]
>> Speaker 1: It will compile, it will run, it will return undefined when you call it. You will not get the new object that you're trying to construct. We keep doing this to ourselves, keep doing this to you, how that actually works.
>> Speaker 2: Because the brackets are ambiguous?
>> Speaker 1: Yeah, because the brackets are ambiguous.
[00:07:40]
There are number of ways they could have disambiguated it, but they decide instead to make them.
>> Speaker 3: A couple quick [LAUGH] because that's not what you put inside the brackets isn't really the contents of a function. Typically, a function would return. I'm confused about, I'm confusing this. I just fart with what we're using in another language.
[00:08:03]
In TypeScript, I think, they put var in TypeScript. I've been using it, I'm using it to call a function to do something
>> Speaker 1: So this will work for lots of other purposes, the place where it fails, as if you're trying to return a new object literal.
>> Speaker 3: You would have on the other side of this X var X equals.
[00:08:26]
>> Speaker 1: Yeah, or you pass this more likely pass this as an argument to another function That's one of the most common uses for these.
>> Speaker 1: So anyway moving on, and there are lots of other bad parts in the language too like generators with the yield operator, I think that was probably a mistake.
[00:08:47]
It adds a lot of complexity and not much value and kind of locks people into patterns which I think are things that we should be evolving away from and not getting trapped in. A lot of the other new features we haven't had enough experience with them yet to know.
[00:09:00]
If they're going to be good parts or bad parts but there is one part that I'm confident is going to be a very, very bad part and that is class. This was the most requested new feature for ES6, it was mostly requested by Java programmers who were having to move into JavaScript and were unhappy about it.
[00:09:20]
And saying please can you turn JavaScript into Java so we don't have to learn this crappy language and so we did that. Except we didn't actually add class what we did was we added syntactic sugar on top of the prototype of stuff that's already there. So when you get down into the edges, it's not going to work the way you expect it will.
[00:09:39]
But worse than that is it keeps you trapped in the mindset of the classical model, which means you will never learn to use the functional model. And also you don't have the strong type system that you need in order to deal with classes. And so I'm pretty confident that the people who are gonna be using classes are gonna feel vindicated.
[00:10:01]
It's in the standard, I have a right to use it. It's all that good stuff. But they will never learn to use the language effectively. They will go to their graves never knowing how miserable they were.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops