This course has been updated! We now recommend you take the JavaScript: The Hard Parts, v2 course.
Transcript from the "__proto__" Lesson
[00:00:00]
>> So we realize, this approach that produces our objects towards one in user1, one in user2, it creates a big issue. Our functions are yeah, hooray, are available right on our objects, success. But that she got copies of them, they're all the same function And if you've got a thousand functions, you've got a copy of each of those functions on every single user object.
[00:00:27] This is gonna take millions of lines of memory, when we don't need. We just have a single version of that function. What would be our solution? Store our increment function In just one object as imagined we could sort of store it in just one object let's call it I don't know store or function let's call it function store and in that one object supposed there we were to.
[00:00:57] Store the increment functionality. The increment functionality, and somehow if I didn't and then not store it on each of these objects. Keep these object clear. And somehow if I were to find On my user tool, my user1 object, the increment function, somehow JavaScript would know to go and look instead in this function store where I have my single copy of that function.
[00:01:30] Store the increment function in just one object and have the interpreter When we do something like User 2 dot increment, look up at User 2, there's no increment function on it. We don't want a unique increment function on User 2 objects. But instead look up to this object shared to check if increments there.
[00:01:53] It is. And run that functionality. That's going to be our, what about that as an approach? Approach, all together if all you believe good approach?
>> Yes.
>> Controlling personality. All right. Good but how can we make that link? What's that famous word? People like to shout out words and mine might not be right Or how might we make that link?
[00:02:17]
>> Bond.
>> Bond, okay, is having a bond, what sort of bond?
>> A reference.
>> Reference, what's the special word?
>> [INAUDIBLE]
>> No
>> [LAUGH]
>> It's got as many words as you like.
>> Back pack.
>> Back pack no, back pack what's that favorite word you heard about in JavaScript?
[00:02:33]
>> Prototype.
>> Prototype
>> Exactly, good job. We're gonna see that all play out, though. Using the prototypal nature of JavaScript solution to in full, we are gonna somehow use a. I said that we've seen all the features of JavaScript. There's one more. It's called its prototypal nature, which is that, when I call a method on an object I first go look at that object if I don't find on that object the method i don't panic I use the prototypal nature of java script to look up to the next object up to the prototype chain the next object that I bonded this object to here we call it function so and I can use the functionality that's stored there.
[00:03:15] The single version. We're going to walk through this line by line. See it all play out in full. But know that's fundamentally what we're doing. We're going to somehow make a bond to a shared function store in which all the functions that we'd want to have available on user two.
[00:03:31] We want to be able to do this dot increment. That's always our goal. But we don't want to have the increment function on user two. So we're going to use a special prototypal nature, a special bonus feature of Java. I think this is the last one. This is the last true feature of Java Script that lets us, when we access user two and don't find increments.
[00:03:52] Go look at this function store through this special hidden bond. You see how this hidden bond works, but first, let's walk through this code line by line.