Check out a free preview of the full The Hard Parts of Object Oriented JavaScript course
The "Subclass Review" Lesson is part of the full, The Hard Parts of Object Oriented JavaScript course featured in this preview video. Here's what you'd learn in this lesson:
Will reviews what was learned up until now in the course, and looks towards the call and apply functions that will be introduced to control the assignment of the this keyword.
Transcript from the "Subclass Review" Lesson
[00:00:00]
>> Will Sentance: One more interlude before we see these other approaches to sub-classing. That interlude is this, we need to know that we can control how we call functions using other techniques besides just grabbing the function's name and sticking parentheses on the end of it, which calls the function. We can actually use a couple of built-in tools to call a function and take control inside of the function execution context of the vis assignment.
[00:00:32]
Now, we saw that sort of with an error function. But we weren't taking control, we were just saying, don't have the vis be defined by where the function's being called. Happened to be defined the way the function was first stored and saved. I was gonna say controlled, it was just being different.
[00:00:47]
This call and apply approach to executing a function allows us to manually control what inside of that function the vis will refer to. All right, folks, here we go. Subclassing in Solution two. We've just seen this. Let's remind ourselves briefly, what this did was give us an object, paid user 1, that represents someone who's so like a user, but they're still got bonus properties and bonus functionality.
[00:01:23]
They're very similar to a user. They have name and score, they have increment, they have same name of the function, but they also have an account balance. They also have an increased balance functionality. We did that by having our paid user one's proto link to paid user functions.
[00:01:44]
Remember, we did that when we kind of made that object inside of paidUserCreator, we said its prototype to paidUser functions. Well, sorry, we ran the set prototype function, which sets the proto to paid user functions. And then we said, but that means we'll lose access to the user functions.
[00:02:03]
Don't worry, we set paid user functions, own proto to user functions. Where did we do that? Set prototype paid user functions to user functions. Paid user functions to user functions. This approach, people, is I think the most sophisticated, most sort of fancy thing you could do. If someone in the interview said to you, tell me how the new keywords working under the hood with subclassing.
[00:02:32]
If some monstrous interviewer asked you this, [LAUGH].
>> Will Sentance: You have the exemplary answer. But, in reality in your day to day engineering. You're gonna be automating a bunch of this work. First with the help of new, that's our solution three and then with the help of class that is our solution four.
[00:02:59]
To understand those, we're gonna need to learn this other approach to executing a function. Interlude, we have another way of running a function that allows us to control the assignment of this. And even a warning, even right now, I think it's worth kind of seeing this ahead. What's gonna happen, is that in our new keyword version of this, folk, we are again, in creating our paid user, Alyssa 825, we're going to use I userCreator function to do so.
[00:03:33]
We're gonna use I userCreator function to do so, but unlike calling the userCreator function with a new keyword which is gonna create this, which we can attach our properties to. We're gonna use user creator.call, that's gonna call user creator, but we're gonna, and this is the most brutal piece, that's why I'm telling you this early.
[00:03:53]
This is the most brutal line of code I think in all of hotpads, or in the top three. There's quite a few brutal lines of code in hotpads, in the top three most brutal lines of code in all of JavaScript hotpads various talks. This line here, we are going to run our userCreate function to stick our name and score properties on an object.
[00:04:12]
But that object is not gonna be the auto-created one, but the new keyword God is creating with user creator, but instead one that we're going to make and pass in using this cool method, whose first argument, first input Is always whatever this is going to be inside of that function.
[00:04:33]
Don't worry, we're gonna build up to it, we're gonna build up to it. Starting off with this understanding how call and apply work. So that we can be use them in the following exam. So I want to give you a preview, we gonna use this to make our new solution three, and new key word version of sub classing actually work.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops