Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
After this, I have this playerFred variable that I can use, and I can add new properties freely to him. Properties are variables that belong to this particular object, and I can even add methods, which are functions that belong to this object, and then call the methods of that object. But there is nothing that would stop me, for example, from creating another object which looks like it should have a similar structure, but in this case would have high score instead of score and have level instead of rank. It just doesn't match the general structure.
Now, there is nothing in this constructor function right now, so we might want to add a little data to the object. Well, I could put the phrase this.name=fred. If you remember, "this" is a word that we can use to refer to whatever the current object is, but I don't want to make all my objects have a name of fred, so well, here's where we use the benefit that this is a function. So what we can do instead is make this constructor function take a parameter, in this case we will call it n, and then when we call it we will actually create a new player passing in the name Fred.
What I can do is call Player.prototype. and then use this to attach to tie new functions to this player object. So I might decide that I want this object to have a method called logInfo. Now, this is completely up to me what I call it. I'm going to set that equal to and give it a function body here, and all I will do with this one is make it write out of console.log that writes out the message "I am" then a comma and then this.name, again, using this to say whatever the name property of the current object is, because we might call this with 500 different objects.
I want to make sure it writes out the right one for each one. I could add another one in here. Keeping the same general format, I am going to say Player.prototype. Well, let's give this one a promote method and when promote is called it's going to say this.rank++. Add 1 to whatever the rank property of the current object is, and then we will write out a message that says, "My new rank is: this.rank." And using prototype I am attaching these functions to that constructor so it means they'll automatically be available on any object that was created using that new keyword.
So down here after I create this new variable called Fred, which is a new player, I can say fred.logInfo, fred.promote. Save that, jump over and open up the page that's using this script. If I have Firefox open--yup I do-- I can see in the Console here "I am: Fred," "My new rank is: 6." Now it seems like an awful lot of trouble if I was just going to create one thing, but of course the whole benefit of this is I can now create a new variable called Bob which is a new player, and I will pass in three pieces of information--the name, the score and he rank--and I don't have to do anything else to make this object have the logInfo method.
It will automatically have it. Same way it will have the promote method. I can just start calling those. Save that, jump over into the browser, refresh that, and we are getting information about the different object. Create a third one, create a fourth one, create a fifth, and so on. Each of them will act independently of each other. They will know about their own properties and they will base everything that they can do on both this constructor and any of these methods that I've attached using the .prototype property, without having to separately attached them for each and every object.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 92826 Viewers
82 Video lessons · 104226 Viewers
71 Video lessons · 75922 Viewers
56 Video lessons · 107279 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.