Check out a free preview of the full The Hard Parts of Object Oriented JavaScript course

The "Prototype Lookup" 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 concludes this solution by diagramming a prototype lookup by an object that was previously instantiated.


Transcript from the "Prototype Lookup" Lesson

>> Will Sentance: Here's how we're gonna important checks. So we're gonna do it over here. Does our paidUser1 have access to its new or the functionality that's specific to a paid user? Let's have a look. Let's have a look. Elliot, you're up man. paidUser1.increaseBalance(), give me the lookup journey of JavaScript.

I'm gonna go on this side and kind of crouch down so you can see. So paidUser1, where do we look for it?
>> Elliot: So we'll look to the right.
>> Will Sentance: Well, we look for paidUser1 in global memory.
>> Elliot: And if it doesn't find it there
>> Will Sentance: Doesn't find what?

>> Elliot: The increase.
>> Will Sentance: Doesn't find increased balance method is not there. You've got name, score, account, balance. Don't see any increased balance method. We wouldn't want to, would we, because it means it would be a copy on user one, paidUser1, and then another copy on paidUser2, and another copy on paidUser3.

We don't want that. We want to have a single place. How do we get to that single place, Elliott?
>> Elliot: Through proto?
>> Will Sentance: Through proto. Up to
>> Elliot: Paid user functions.
>> Will Sentance: Do we find increased balance there?
>> Elliot: Yes.
>> Will Sentance: We do. We grab it. We execute it.

Fantastic. By the way, would user one have access to paid user functions? User one dot increase balance. Is it on user one? No. Looking proto. User functions. Is there increased balance there? No, because we don't want our user one to have a increased balance functionality, that'd make no sense.

So we kind of parcelled off functions. We get to choose which ones we want and pass them, not pass them down but have them available. But also have objects be generated at a level in that chain where they don't get access to things further down. Very nice, it's very very nice.

Okay. This is the prototype for design of a language. It's not classical OP. It's a prototypal language structure, which is I think very interesting. Okay. But now let's check whether you can still give paidUser1 access to that sayName function that is definitely not on its set of functions.

Let's go through the journey. Seth, give me the lookup journey.
>> Seth: PaidUser1 sayName is gonna look in the paidUser1 in global memory. It's not gonna find it.
>> Will Sentance: One second, it's not gonna find it. Yep.
>> Seth: It's going to freak out briefly and then look in proto.
>> Will Sentance: Where it's also-

>> Seth: Not gonna find it. And then it's gonna look in that proto, where it will find it.
>> Will Sentance: Where it will find it and execute it. Very nice Seth, excellent. All right, folk. That's, this is solution two, done. This is the most fine grain, this is the senior dev question, tell me how subclassing works under the hood, because this is it.

Now, we are going to mask this with a new keyword implementation that still requires, honestly, a little bit too much work to do in subclassing. Which is why it was reasonable for solution four to emerge as the new standard, where this whole approach is handled in a cleaner fashion through classes and through the extends and the super keywords.

And that's what we will see in a moment.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now