JS.Next: ES6 / ES2015 Extending Classes
This course has been updated! We now recommend you take the ES6: The Right Parts course.
Transcript from the "Extending Classes" Lesson
>> 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: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.