
JavaScript: The Hard Parts Object.prototype
This course has been updated! We now recommend you take the JavaScript: The Hard Parts, v2 course.
Transcript from the "Object.prototype" Lesson
[00:00:16]
>> It makes a bond here. It makes a bond to this user creator.protoype. user creator, this is, when it's an object, just like we could do .andrew.prototype, right? .prototype, which is, wait for it, another object. And login is a function. We're gonna go through this line by line do not fear.
[00:01:02] But, no log, cuz we don't know what each function source is gonna be called. We need a default place that this auto generated object, that gets its auto proto bond up before that object got its proto bond we said. make it to use the function store, when we said Before create an empty object.
[00:01:20] When we run userCreator, go inside, create an empty object. Store properties on it, name which is Will, score which is 3. But when we create an empty object make a bond to any of the functions that we could ever want to use on this object. So when we pass out that object to store in user1.
[00:01:40] And we call User1 it'll increment, even when we don't find the increment function on User1, we just look up to through the _proto_ to userfunction store. But we made that explicitly. We said make a bond to userfunction store. We still gotta do that, with the new key word this is gone, it's automated.
[00:01:58] So, we need a standardized place, cuz we create new function stores and we store those functions there. Didn't we? There it is, use the function stores for the function and it made the bond. Now we don't get to do that, so we need a default place in which to store those functions.
[00:02:13] And Java Shifter says best place to store them. We're gonna give you a lot of objects stored on user creator in its object form. Low object on it with the property prototype and you can store them there. So let's see that in action. New keyword automates a lot of our manual work.
[00:02:32] Gone is the line of creating the empty object with the bond, well, we call it functionStore here, whatever, with the bond to functionStore. No longer can we refer to newUser.name is name. newUser.name is name Will. User.score is Score 3. Can't do that. Now, we refer to it as this.name, this, which is then gonna become the auto-generated object.
[00:02:54] .name is name Will. No longer do we have to return this out, done automatically for us, which, by the way, means, when you've seen this user creator with a new keyword, you've really never realized something's being returned out. That's in there. It's automatically returning out for us, the object that was automatically created.
[00:03:11] But now that inner object, needs to somehow have a guaranteed place it can finds its shared functions. Such that when we return that object out and call this function on user1 and don't find increment, even though we don't have a bound to the function store Somehow we know where to look, for the functionality.
[00:03:32] While the default place, is the name of the function in which the object was created. Not at its function version, but as an object version. On the prototype property of that function that created the object. Is a property called prototype. Which itself has an object on it, and that's where we store those functions.
[00:03:54] And JavaScript is gonna know to make a bond to this guy here.