This course has been updated! We now recommend you take the ES6: The Right Parts course.

Check out a free preview of the full JS.Next: ES6 / ES2015 course:
The "Extending Classes" Lesson is part of the full, JS.Next: ES6 / ES2015 course featured in this preview video. Here's what you'd learn in this lesson:

Classes can be extended using the extends keyword. This is very similar to what can be done today with prototypes. Aaron also demonstrates how the extends keyword can be method call that’s evaluated at runtime making the super class dynamically assigned.

Get Unlimited Access Now

Transcript from the "Extending Classes" Lesson

>> [MUSIC]

>> Aaron: You can extend it. So if we make a monster called Godzilla, for him we can just call the super method and pass in Godzilla and 10,000. Cuz we all know Godzilla's pretty tough, right?
>> Speaker 2: [LAUGH]
>> Aaron: So, yeah, so, sorry, I'm skipping. So yeah, you could totally extend like that.

[00:00:25] Is this something you could do in prototypes? Has anyone ever done anything like this in prototypes? Yeah, some of us have. You can totally do all the things we just said in prototypes, okay? So, yeah.
>> Aaron: I just wanted to point that out real quick, that this is definitely not a new thing.

[00:00:53] This is still sugar. The extends expression can be like a method call. It's just gonna be evaluated. So you could say, class mySocket extends getClass. And then getClass says, well if I'm IE, use my IEWebSocket implementation. [LAUGH] Cuz you know someone's gonna do it. And then, otherwise return WebSocket.

[00:01:17] What's up?
>> Speaker 2: Does the declaration have to be outside the class declaration?
>> Aaron: For?
>> Speaker 3: I think he means for the symbol.
>> Aaron: For the symbol, it has to be, because if you put it inside the constructor, I mean okay. Well, I'm gonna go back to the code, just because I think I know what he might be asking.

[00:01:36] Okay so in, I'm gonna delete this code down here just cuz it messes things up. It just makes it look dirty. Okay, so in other languages that have this kind of class syntax, you have room up top where you can make a bunch of variables. So in Java, when you have a class, your first few lines, you can put some constants up there, right?

[00:02:03] And [COUGH] Yeah, so I think he's asking can he say var Monster. Can he put this in here, is basically what he's trying to say. Correct me if I'm wrong. Is that what he's trying to say?
>> Speaker 3: That's my interpretation.
>> Aaron: Yeah, no, you can't say that. I should have just said that, but you, I wanted to make sure I was understanding what he said.

[00:02:29] You can't do that. Yeah and so, this actually this brings up a good point. So this is one of my biggest gripes about JavaScript. Who here works with Angular? Yeah. So, I'm an Angular guy, and I'm gonna just ask you to think about a controller function real quick.

[00:02:47] Think, even in the best code, people do, at the root level of their controller function, they do code, code, code. They declare a function. More code, code, code. More function, more function, code, code. It's like, who wrote this? Who ever thought that that was an okay way to write code?

[00:03:11] The fact that you can write code at root level makes me just wanna stab myself. And so I really like that in classes, you can't do that. I like that in classes here, it kinda takes the, there's no root level code here. You have to put it all inside of a method.

[00:03:27] That's how we did it in other languages, and it worked great, right? And so I really like that it is bringing a little bit of structure back into our code, not letting us just pepper it in wherever we want. I think this is a good thing. So, yeah.

[00:03:41] Yeah, anyway. But you cannot do what that guy wants to do, so, at least that I know of. And that's just today. The spec could change, so yeah. Yeah, so again, the class when you extend something, that could even be dynamic. That can change per browser. So you can say MySocket extends getClass, and the getClass says, hey man, if I'm in IE, return this other class.

[00:04:10] Otherwise, just use the web socket, native web socket. And so, yeah, does that sound like fun? But you could do it. That's the thing that you could do, you don't have to do.