This course has been updated! We now recommend you take the ES6: The Right Parts course.
Transcript from the "Additional Class Features" Lesson
[00:00:00]
>> [MUSIC]
[00:00:03]
>> Aaron Frost: There is some, remember I said it is a little bit up for discussion still? Take a look at it real quick, I'm gonna zoom in on it.
>> Aaron Frost: So around the calls to super(), this might change a little bit. So what they're thinking is, and I actually wanted to ask everyone here because I just barely found this out.
[00:00:28] And I just barely found it out because they just barely are talking about it. This is brand new, this is still subject to change on classes. So when we say this thing's still being talked about, it's totally still being talked about as we speak. So the thought is, if I have this Godzilla class that extends Monster and someone calls attack() on Godzilla.
[00:00:59] And I wanna do some Godzilla things, but I also want to call my super.attack(). Does it make more sense just to call super() or does it make more sense to call super.attack()? That was I wanted to ask you guys, cuz that's what's clearly up for discussion. So the argument is if you're in the attack method and you call super(), it should look for the parent class and just call the function of the same name.
[00:01:27] That's what some people are saying. So they think that the super just calling super() is good. And other people are like just dropping the word super in random places doesn't make any sense to me, I want you to make me say super.attack(). So, what do you guys think?
[00:01:46]
>> Off Camera 1: If you wanted to do a different super function, right, how the prepare to attack method on super.
>> Aaron Frost: Yeah well you could always call it. If it's a different function name you don't really run the risk of calling this dot prepare for attack, right? You would be fine if you said, you could say, this dot prepare for attack because that's not cyclical, that's a totally another function.
[00:02:11] We're just talking about the two that are called the same thing. If I call this dot that attack, I'm effed, right? Cuz it's just going to start all over. So the question is specifically around the two methods is called the same thing.
>> Off Camera 1: I prefer the first.
>> Aaron Frost: You prefer the first, yeah, yeah.
[00:02:25]
>> Off Camera 2: Or you're overriding basically the attack() function but what if your super class doesn't do certain things that you want the attack() function to do? Like if you attack, you also want to subtract from your action point pool. But you have a separate function that would subtract from the action point pool [CROSSTALK].
[00:02:41] So you could do super dot-
>> Aaron Frost: Exactly, but what if you have like a really good dodger, that was like, hey dude, I'm gonna dodge stuff as I attack, I don't know. So my attack() method here is incomplete, you could have more code beside just calling super. I could have code that could like do a million things, and then call super to attack.
[00:03:04] Or what if you just want to call three super functions, but not have any of those functions on your extended class?
>> Off Camera 1: That's how you'd call this guy.
>> Aaron Frost: Yeah I see what you're saying. And if that's something that would make sense, and I'm not sure that it does, and I'm not sure that doesn't.
[00:03:25] I just haven't thought about it, but if that was what it was then this the super.attack() would be better, right? The dot syntax would better than just the super syntax. Anyway guys see these guys, these super smart people they sit at the TC39 things and they're like, they talk about that like at length.
[00:03:50] We talked about a for five minutes and I'm done. Like I already want to give up and go home. They talk about this and they email about it for like days, okay? So this is what the TC39 does. So this is something that still subject for change which is why I'm not going to teach it to you.
[00:04:04] Like I just wanted to point out that that's a thing that might change, make sense? Okay, all right?
>> Aaron Frost: You can't use it before it's been it's like let like it's looks like. It's not gonna. Okay, what I meant to say it's not going to get hoisted, okay?
[00:04:26] So if you have class Bar{} on line ten, and you try and new one up on line one, you are gonna get an error because they don't hoist. At least they shouldn't hoist. You guys might try it out on tracer and it works, but they shouldn't hoist. So if no constructor default behavior, yeah, so if you don't have a If you don't have it is a constructor.
[00:04:49] Like let's say you just make a class called foo, and you don't give it a constructor. Or you're like foo, and it extends something else. If you don't give it a constructor, it's gonna do the same thing that we're used to other languages doing, which is just calling the super constructor and passing in the arguments.
[00:05:07] So that's all it's gonna do. So it will implement default behavior when a constructor is absent. But if it's a class as we talked about in sugar. I think it's easier to learn for newbs too, though, like the some of us have i really honed our skills on prototypes and I sometimes look at my classes my prototypes and I'm like, that is beautiful.
[00:05:31] I've done a great job today. But I think for for new people for people just learning the language, which hopefully we get a lot more JavaScript learners. I think it's a good thing, I think classes will help.
>> Aaron Frost: I think especially subclassing, specifically, I think it will make it easier for new people.
[00:05:52] You can also subclass built in stuff. Some of the benefits. I think it will make code ports between languages a little bit easier too. Just to compile, I think will make JavaScript a better compiled to language,personally. And I talked about the possible perf games long term if if we got rid of function constructors and only use classes.
[00:06:15] You could see some, I don't know, I think big big performance gains if all functions lost their constructor. But yeah, that's classes, we did it, we good? Any questions, online? No Okay. Let's go ahead and try it out real quick. So go ahead and try and build something in parallel, a prototype and a class.
[00:06:48] Or like for some method implementation from Super, try that. Maybe someone could like, extend array and make it so that calling array.push() returns the instance of the array so that you can like chain your calls to it. You know what I'm saying? So you could do like, kinda like how you can in Lodash, like array.map.filter.blah.
[00:07:14] Well actually you can with those ones but not with like push and pop but, anyway