Mastering JavaScript 5: Critical Fundamentals of Objects | Steven Hancock | Skillshare

Playback Speed


1.0x


  • 0.5x
  • 0.75x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 1.75x
  • 2x

Mastering JavaScript 5: Critical Fundamentals of Objects

teacher avatar Steven Hancock, Founder All Things JavaScript

Watch this class and thousands more

Get unlimited access to every class
Taught by industry leaders & working professionals
Topics include illustration, design, photography, and more

Watch this class and thousands more

Get unlimited access to every class
Taught by industry leaders & working professionals
Topics include illustration, design, photography, and more

Lessons in This Class

    • 1.

      Introduction

      1:14

    • 2.

      The Nature of Objects

      17:29

    • 3.

      What is a Prototype and Why?

      4:27

    • 4.

      Prototype Objects are Everywhere

      8:20

    • 5.

      Prototype Chains

      10:02

    • 6.

      Object Properties have Precedence

      6:13

    • 7.

      What's Next?

      1:59

  • --
  • Beginner level
  • Intermediate level
  • Advanced level
  • All levels

Community Generated

The level is determined by a majority opinion of students who have reviewed this class. The teacher's recommendation is shown until at least 5 student responses are collected.

12

Students

--

Project

About This Class

This course is the fifth section in Mastering JavaScript. In this section we cover critical fundamentals of objects. This section includes the nature of objects and dives into prototypes.

In this course we cover the following topics:

  • Nature of Objects
  • Prototypes
  • Prototype Chains
  • How the Prototype is Used with Object Methods and Properties

Spend the time necessary to understand these very important concepts. Make sure to complete the exercises and post your solutions.

Prerequisites and Set Up: You need to understand the basics of JavaScript to do well in this course. If you have completed the first 4 sections, you should be ready to dive into this section.

The setup is pretty easy. We aren't using any libraries or anything like that, so all you will need is a text editor and a browser. The majority of the JavaScript code that we write we will be run in a browser. 

For a code editor, I will be using Visual Studio Code. This is a free, cross platform editor that is pretty popular, so if you aren't currently using visual studio code and would like to during this course, you can download it here.

Meet Your Teacher

Teacher Profile Image

Steven Hancock

Founder All Things JavaScript

Teacher

I have 20+ years experience in training and product development and 15+ years using JavaScript. I started learning JavaScript when it was a new language used for minor affects on web pages. The growth and ubiquitous nature of JavaScript both excites and inspires me.

Currently I am the President and Lead Trainer of All Things JavaScript, a resource for anyone and everyone that hopes to increase their JavaScript skills. Our goal is to assist in the journey from JavaScript novice to expert.

I have been the co-owner and President of Rapid Intake, an eLearning firm. The company was an ideal place to put my training and development skills to work. While there I managed all development and professional service related activities. I was heavily involved in the initial development ... See full profile

Level: All Levels

Class Ratings

Expectations Met?
    Exceeded!
  • 0%
  • Yes
  • 0%
  • Somewhat
  • 0%
  • Not really
  • 0%

Why Join Skillshare?

Take award-winning Skillshare Original Classes

Each class has short lessons, hands-on projects

Your membership supports Skillshare teachers

Learn From Anywhere

Take classes on the go with the Skillshare app. Stream or download to watch on the plane, the subway, or wherever you learn best.

Transcripts

1. Introduction: We have already completed a section on critical fundamentals. However, in this section I want to cover critical fundamental specific to objects. In this section, following a quick cursory look at the nature of objects in JavaScript, we will mainly deal with the concept of prototypes. Understanding prototypes in JavaScript, it is absolutely critical. Some talk about prototypes as a pillar of JavaScript and really that makes a lot of sense. Prototypes are associated with objects. And as we discussed earlier, objects are everywhere in JavaScript. That makes prototypes everywhere as well. As we get into prototypes, we will first explore what they are and how they affect objects. We will cover advantage of prototypes, the prototype chain, and ways we can work with properties and methods provided by prototypes. I don't think I can overstate the importance of understanding prototypes in JavaScript. So even if you feel you have a good grasp on this subject, I would still encourage you to go through this section is review. Alright, that's enough introductory information. Let's jump in and get started. 2. The Nature of Objects: If you already have a thorough understanding of the nature of objects in JavaScript, you can skip this topic. I've included it because before I start talking about prototypes, I want to make sure the nature of objects is understood. This is especially important for those that may be coming to JavaScript from another language. We've used objects numerous times in this course already. So it may seem a bit strange to cover the nature of objects. Now. However, there are many that work with objects in JavaScript without understanding them completely. Sometimes it is better to have some experience with objects before diving into the true nature of objects, which is what we're doing in this section. So put simply, an object is a composite value. It brings multiple data pieces, primitives, or other objects together under a single reference. An object is an unordered collection of properties, and each property has a name and a value. The name of the property is usually a string, but it can also be assemble. The value can be any datatype available in JavaScript. So we speak about properties as having name-value pairs. Now, before we go any further, let me start creating an object. Some declare the variable. Then I'm going to define that object using an object literal. So first F name, I'm just gonna put information about myself in here in this object. So basically right now what we have are two properties and F name property and an alanine property. And notice that each of these properties have a name and a value. In the case of these two, the values are strings. Now when you're defining an object, each property must be separated by a comma, which I've done here. Now, the name is referred sometimes as a key. So we talk about properties as a key value pair as well. Now to throw in a different datatype. Here is a property that stores a number. So the name is score, the value is 90. And a property that stores a Boolean. Now we said that the value of a property can be any datatype that's available in JavaScript. So we can do other object type, e.g. an array. So if I do quizzes here and then set that up as an array, and it just contains some quiz information, basically quizzes, the quizzes that have been taken crap, something like that. There I have an array. Another type of object could be any of the built-in objects that are available in JavaScript. One of those course, to date object. So this object is created on I just create a new date object and that becomes the value of that property. Then of course we can just have a standard object, say have an address in here. And that is an object and it contains properties inside it. Let's see, 45 main and then city. So there we have several different datatypes associated with properties. Now the one thing we haven't added yet is we haven't added a function to a property. And when the value is a function, we call that a method. So basically an object property that points to a function as a method. Now, methods can be set up in two different ways. Let's say I wanted a fullName property and this is going to be a method this points to a function. So here's one way to set it up. This is, this is the traditional way. We just define a function here. Now I'm using this to refer back to the object. I'm grabbing fname and I'm just concatenating that with Helen name. So I use this again. Like I said, there's two ways to define a method to attach a function to a property. This is the traditional way. But in a recent release of Ekman script, they've simplified this so I can define the same method like that. Much simpler to do. Now, here we've created an object using an object literal. That's what we call this. But there are multiple ways to create objects in JavaScript. E.g. every time we create a function, an object is created using the function constructor. And that's just a special type of object. Every time we create an array, an object is created using the array constructor, which is a special type of object. So a lot of different ways to create objects. But even when we're creating a standard object like I've done here. There are multiple ways to do that. And we'll dive into a lot of those different ways of doing that when we look at the section on structuring code using objects and object oriented principles as they're applied to JavaScript. So that's what we will cover some of these different ways creating objects, because those become patterns. If you're structuring your code using objects. Now, let's go ahead and save this object and take a look at it. This in a bit and open up the console here. And just do OBJ. And there we can see the object and all of its different properties, the name value pairs, which we see inside of this object. Now, in order to access a value that is associated with a property, we traditionally use the dot syntax. So I can access F9 like that and it displays the value for that particular property. But we're not limited to just dot syntax. We can use square brackets as well. And this becomes very useful when we need to do certain things programmatically. So e.g. if I pass in a string address inside of square brackets, it's going to look up OBJ dot address, and it's going to return the value for that property, as we can see there. Now, this can also be used when assigning values to an object. So let me jump back the Visual Studio Code here. And up here, I'm going to first define a couple of variables. One is data name. I'm going to set that equal to a string final. And then the other one. Let's just go Data Val. And let's set that equal to 80. Now we're going to use these values to create another property on the OBJ object. Now we could do that inside of this object literal. We could do it like this. I put square brackets and then the data name, that's what I want. I want final to be the name of the name value pair. And so I put square brackets around it. Then I put the colon like that. And then I can just associate the value with it. Now let's go ahead and save that and take a look at the object. Make sure that we're actually seeing that. So if I open up this object and we have final and it's 80, and so that worked for us. Now, another way could have been to assign it here. I could do OBJ. Then inside the square brackets data name. You can see right now it's retrieving the value so we can use it to retrieve or we can use it to set. If I set it to 90. What happens? Case-sensitive. There we go. The score is now showing 90. Now something else that shows us is that objects are mutable, meaning they can be changed. Now, even if we had defined this object with const keyword, that would make it so that we couldn't assign another value to this variable. That's all that would happen. We could still change all the properties inside the object because objects are mutable. So using const is not going to prevent us from changing properties inside of an object. Now, there are some commands to help us make objects so they are more immutable so we can't change them. In order to introduce that, we have to be aware that each property contains attributes. Attributes are associated with each property on an object, and these attributes determine what we can do with that property. So let me just go through the three main attributes. One is writable. So the writable attribute determines if the value can be changed or not. So if writable a set defaults on F name, I can't change that value. It won't allow me to change it. Alright, there's also a, an attribute configurable. And the configurable attribute determines two things. One, it determines whether or not we can delete the property from the object. So if it's false, we can't delete it. The other thing that determines is if we can change the attributes for that property. So once attributes are set, if we set configurable to false, we can't change those attributes. That's what it's going to prevent us from doing. Now there is one caveat to that. If we set configurable to faults, we can change writable to false. Javascript engine allows that, but that is the only scenario where we could actually do a change of configurable were false. Now the last attribute I need to mention is enumerable. Now the enumerable attribute determines that the property is returned by certain actions. E.g. a. For-in loop will not show a property with an attribute of innumerable set defaults, it just won't show up. Now by default, for this object we've created here, by default, all of these are set to true. And we can see that with a simple JavaScript command. Let me just save what I've done there. This is a static method of object constructor. It's get owned property descriptor, it's pretty long one. So if we pass in OBJ and then we pass in the property that we want to see its attributes for. Let's do F name. It will return the information on that. So the value is Steve shows us and then writeable is set to true, innumerable is set to true and configurable is set to true, so we can see what they are. And by default, that's what is assigned to every property, true, true, and true. Now, how could you change that? Well, let's go ahead and define a new property. Let me go ahead and do that inside of Visual Studio Code so that you'll have a record of it. Defined property. And then once again, this is static method, so object dot defined property. And then we pass in the object. And then we pass in the property. We want to act on. I'm going to create a new one. So I'm gonna do BD for birthday. Now, the third parameter is an object. And this object has the attributes in it that we want to define. One is value. I'm going to set that to me 25. Then we can do the writable, configurable, and innumerable properties we've talked about. So let's do writable. And let's set that to false. I'm going to set these defaults on all of these, since this is since true is the default. Alright, let's go ahead and save that. And now let's see what we get. So we take a look at OBJ. We can open it up and we can see there is now a B date property and it's set to May 25th. Can we display it? Obj dot? Sure. We can display it. Can we change it? Well, let's try. It. Will be j dot v. Let's set that equal to June 25th. It looks like it changes. And this is what I think is a problem with this. It looks like it changes. But when we display OBJ again and open it up, or when we take a look at the BD property of OBJ is still set to its original value, so it doesn't allow us to change that. Now what about the configurable property? Let's try to change that same property again, I'm going to copy in the same command. Let's try to set it up again. And let's set writeable to true. Now, if I press Return, notice we get an error type or cannot define property, it won't allow us to do that. Now one more thing I want to show just a for in loop. So let's do let i. And then we'll just console.log. I like that. So there's a for-in loop to loop through all of the properties of the object. Now if we look through that, we don't see a BD because it's not enumerable. Let's try one more thing. Here's another static method. And this will retrieve the keys. The keys of the key value pairs. The key of the property. We also call that name, but I mentioned that it was called keys as well. So if we display that, basically what that does is that creates an array. But notice B day is missing and that's because the enumerable attribute is set to false. Now there's a similar command, object dot values, that retrieves the values and puts them in an array like this. So some nice static methods to be aware of. Basically using these different commands to show you the nature of objects, what objects are alike in JavaScript. And with that grounding, we're ready to begin talking about prototypes, a very important concept when it comes to objects in JavaScript. So let's move on. 3. What is a Prototype and Why?: As we dive into prototypes, we first need to talk about what a prototype is and why it is important. So first, what is a prototype? Now, almost every JavaScript object has a second object associated with it. That second object is called the prototype object. And the JavaScript object can borrow properties from that prototype object. Now, before we dive into this more detail by looking at examples in actual code, Let's think about the power of this concept. Let's say we had four user objects, as we're showing right here. User one, user, two, user, three, user for they each have an F name property, N and L name property. And they have different values for those properties as we can see, because their names are different. But let's say that each of these objects need a full name method. A full name method that will return the firstName concatenated with the last name. So we can go ahead and define a phony method on each of these objects. But that doesn't seem very efficient. Putting on an each object that uses more memory and we're repeating ourselves. There's a couple of things that just make that inefficient. So the ideal way is to place that method on the prototype object. Now with the method on the prototype object, then each user object can borrow that method whenever they choose to, whenever they need it. And it's only in one place. If we then need to make a change to it, we go make a change out on the prototype object that changes available for all of the objects that use that object as It's prototype object. So a lot of advantages there. Now in this example, I'm showing the borrowing of a method, a function that's associated with a property. So a method, if I enter user one dot fullname and invoke that in my code. When the code execute, the JavaScript engine is going to look first on the user one object. It's going to look for a property of full name that it can invoke, use as a method. When it doesn't find it. It will look on the object's prototype. The first place it will look. And if it finds it there, it will grab it and it will invoke it. So that is how objects are borrowing properties from the prototype object. The JavaScript engine will first look on the object itself, can't find it. It will then look on the prototype. But what if the prototype had a property up here in our prototype object? What if I had a property of f, fname and lname? What would happen then? Would the object use its own afternoon property and L name property? And the answer is yes, of course it would, based upon what we described, the JavaScript engine is going to find that property on the use of one object. It won't need to pull it from the prototype object. So this is another powerful feature of prototypes. We can have properties on the prototype object and we can override them on the individual local objects. So this provides flexibility that we're looking for. Now, I said that almost every JavaScript object has a prototype object from which it borrows properties. So there are situations where we can have an object without a prototype object. For that to happen though, you as a developer would have to force it. So any object created through regular means, we'll link to a prototype object. This means arrays, dates, functions, regular JavaScript objects, these all have prototype objects. And in the next topic we're going to start looking at some of these and looking at the actual prototypes and what they provide to those objects. So let's move on. 4. Prototype Objects are Everywhere: Since almost every object has a prototype, Let's go ahead and look at some objects. Take a look at the prototype and what the object gains from that prototype. Now, let's go ahead and start with an array. Now I'll be doing all of this at the console because these will just be some simple commands and I'm gonna be entering. So we'll be doing it here. So let me first start by creating an array. So very simple array, just going to have three numbers in it like this. I'm going to go ahead and press Return. Now, let me take a look at that array here at the console. We can see the array elements. But notice down here we have this what looks like a property, underscore, underscore. Underscore, underscore will. This is a link to the prototype object. And so if we open that up, we can see what the prototype provides for arrays. Now, with arrays, with that specific object type, there are a number of things provided from the prototype. You can see all of the methods that we get that can be used with an array are contained here. So e.g. let's say we wanted to add a value to this array. So array it up, push. Let's go ahead and add a four to that array. Now when we look at the rate, we now have another item there. Now where did that push method come from? How come we're able to use that? Well, you undoubtedly see where this is going. It comes from the prototype objects. So if we scroll down here far enough, we can see push. So that's why we're able to use push. And so every array that is created had access to this method. And each array doesn't have to use up memory storing all of these methods. They're all stored on the prototype object, and then they get access to them. They can borrow them when needed. Alright, now let's look at some other ways to display the prototype. I've just been using what's provided with the Chrome browser and the way it displays things. And I'm able to open it and then see the prototype object. But we could also do this. That thing that looks like a property that I was talking about. Two underscores proto, two underscores that is available to display the prototype as well. And we can see that looks similar. What I was looking at before. Now, before you go crazy with this underscore, underscore, underscore, underscore. Let me just mention this particular way of accessing. The prototype shouldn't be used. I'm just showing it here because of different ways you can take a look at a prototype, but it shouldn't be used. It was never a part of the initial Ekman script specification. It was just added by browsers and eventually got popular over the years. And then was added to the specification later. But it was still discouraged. Use it, so it's more advisable to use object dot get prototype of which we're going to take a look at. And then later when we look at setting the prototype, we'll look at soon commands to do that. So with object dot get prototype of, we can also look at the prototypes. So let's do that. Inside of the parentheses. This is a static method. So object die gap prototype inside the parentheses. We put the object, in this case, the array that we created. And we open that up and we can see the same type of things we were seeing before when you're looking at the prototype. So that's another way to see the prototype of an object. And notice all the features we get added to an array because of the prototype. Okay, let's do a different one. Let's do a date. So I'm going to declare a date variable and then just create a new date for today's date. Now, because of the way dates are set up, I just can't enter a date like that and be able to see the prototype that basically just to use the toString method to display the date in a string format. That's what that does for me. But I can do a console dot DIR. And there I can open up and see more about this. So here is our proto that points to the prototype object. And you can see all of the different methods is that dates have as well. There are a bunch of them. So it makes sense that those would be on the prototype object and not on each individual date. So because of the prototype, once again, because of that prototype object, we can do date, dot, get date, and basically get the day of the month. We can use any of those methods that are available. So all of the built in JavaScript objects that are part of JavaScript, they all have prototypes with them. We've looked at only two, but they all have prototyping. You can experiment with that, you can try that. And I encourage you to spend some time create some of the different built-in objects that are available and just open them up. Take a look at the prototype, see what's available on the prototype. Now, what about plain JavaScript objects? That's what I want to look at next. So let me create just a plain JavaScript objects. I'm going to have one property name. Just barely created this. And it's not a built in JavaScript objects, so does it come with a prototype when I create it like this? Well, let's find out. Let's open that up. We see the property and sure enough, it has a prototype as well. And if I open that up, there are some things that I get as a part of a plain JavaScript object. Notice there is a method is prototype up. That's a way to check if some object is a prototype of another object. So there are some things we get with the prototype object that comes with a plain JavaScript object. In one of those is value of. There, we get the value. Now let's just use that has own property here. And let's see what is returned for value of. This is math that we just use. It's false, so it's not an own property of OBJ. That's another way to indicate that something we're using is not, is not a part of the prototype object. Because this method or this property that is a method is not an own property of OBJ. It's getting it from the prototype. Now one more thing I want to show before we move on here. If I look at the array again, I come down here to the prototype object. I opened that up and I scroll down all the way down the bottom. Look, there's another one here. And that's object. This is array, which we would expect. This one is object. So what's going on here? Well, this is the prototype chain. So this is what we're going to talk about in the next topic. Prototype objects can also have prototypes. So let's move on and we'll talk about that. 5. Prototype Chains: We've established the fact that objects canned and most times do have a prototype object from which that object can borrow or inherent properties. Now, since a prototype object is also an object, it makes sense that it can also have a prototype object. This creates what we call a prototype chain. Objects linked together in a chain like fashion. Let's first look at this in code and then we'll look at a graphic representation to make sure that we understand how these objects all eat together and a prototype chain. Alright, for this current document, I've created an array, simple array and also an object to have both of these so that we can look at them in the console. And then I'll do some more things here in the code file and just see what happens. So first thing we're going to do is open up that console so we can take a look at these. Okay, so let's first take a look at the array. Now, if I open that up, as we've done in the past, we see that there is a prototype attached to that, and that happens to come from the array constructor. However, if we further open this and then scroll down, so now we're looking at the object that is the prototype to that array. And we go further down within that object, we can see all the methods that are available for arrays. But further down we also have a link to another object, which is the prototype of the prototype. So this is our prototype chain that we're talking about here. So we can see that represented here in the browser. Let's use the get prototype of two also take a look at that. So if I do object, get prototype of an enter, the array. This is the object that is the prototype. All of those methods should look familiar. There's methods. Those are methods that are available on arrays. Alright, now let's go one step further. So I'm going to do object dot get prototype of. And then inside of that, I'm going to put object I get prototype of again. So we will first get the prototype here of the array, and then we'll get the prototype of that prototype object. It should show us the third one we were looking at. So if I open that up, notice what we have here constructor has on property is property value of all of that. So if we were to go down and down, and then one more time, That's basically what we're seeing here. So it is the prototype of the prototype that creates what is called a prototype chain. Now, in that code, I'd also created an object. So I want to show that really quick. So if I enter OBJ and then I open that up, the prototype of that is exactly like the prototype of the array prototype. With an array. When we create an array, it receives a prototype that comes from the array constructor. Well, that prototype receives a prototype, it comes from the object constructor, and that's the same object we're seeing here. So eventually, as you go up the prototype chain, all objects will go to the same end, prototype. And that's the prototype that is attached to the object constructor. And so all objects will have access to the value of method e.g. I. Can type ARR dot value of. And it works. It looks first for that method on the array, can find it there. So it looks for it on the prototype object. Can't find it there. So it looks for it on the prototype of the prototype object and eventually finds it there. Now, if it can't find it on the final prototype object that is in the prototype chain, then it would return undefined or it would give an error saying that's not a method, it can't do anything with it. Alright, now let's go a little bit further here. Let me go ahead and create another object. I'm going to call it OBJ to. This object is going to have a single method on it. It's just going to be full. Name is the method. What is this method going to do? It's going to return basically this dot F name and concatenate that with a space. And then this dot L name. That's what it will returned. Okay? Now what we're gonna do is we're gonna make this a prototype of another object. Because I don't want to take a look and see what happens when we set the prototype of an object. See if we still have that prototype chain that we talked about. So let's go ahead and set up will be J3. Here is a command used to create an object can at the same time attach a prototype is called Object.create. It's a static method on the object constructor. And what we do is inside a parentheses here we pass in the object that we want as the prototype object for this object we're creating. So OBJ three will have this as its immediate prototype. So OBJ to here. Now, when I save that and I look at OBJ three on the console, there's nothing in the object, but it does have a prototype. Notice what's in the prototype, the full name method. And then that keeps going up the prototype chain as well. Now let's add something to OBJ three. Let's add an F name and an L name property. So OBJ three dot F name. There's the firstName, LBJ, three L name, and this is going to be our last name. There we go. Now we have both of those in. So now let's save that. Let's jump to the console again. Now this time if I enter OBJ three, full name, what's going to happen? It will look for it on OBJ three. It can't find it there. So it looks for on its prototype object, it finds it there, does what's needed to and returns the full name for us. Now we just talked about value of which is found on the final prototype object when we're moving up a prototype chain that's available here for us in this self-defined JavaScript object. And basically it prints out the value of it has to go ask to move up the prototype chain in order to find that. So it can't find it OBJ three, it can't find it on its prototype object, which is OBJ to this one here. I can't find it there. Where it finally finds it is on the object dot prototype, prototype. That's where it finally finds it, the prototype of the object constructor. So we've talked about that from a code perspective. Let's look at how that is represented using an image just so I can make sure that this is concrete in your mind. This is an important concept to grasp. So let's take a look at a graphic that will illustrate that. So here we have represented OBJ three has the fname and lname property. And then we have its prototype object represented OB J2. And then we have the prototype of that, which is the prototype assigned to the object constructor, object dot prototype. That is up here, that's where value is found. So when I enter OBJ three dot value of it first looks in the OBJ object, can't find it there. So moves to its prototype, can't find it there. Moves to its prototype, finds it there, and it would continue up that prototype chain. This happens to be the end of the prototype chain. So if it didn't find it here, then it would give an error indicating that it could not find that function or that value of is not a function. That's how the prototype chain is used by the JavaScript engine to borrow properties from an object's prototype. And this illustrates that it's not just the initial object prototype, but it can be the prototype of the prototype. It can go up that prototype chain to find and borrow those properties. Alright, let's move on to the next topic. 6. Object Properties have Precedence: Before we leave the topic of prototypes and begin using them, I want to emphasize a point which you may have already figured out, but I want to make sure it is known. That is a method or property of an object will always override the method or property of the same name on a prototype object. In other words, when the JavaScript engine is looking for a method or a property, it looks first on the object. If the object has it, it uses that version and ignores anything that might find in the prototype chain. Let's look at a quick example that might also be helpful for other reasons. So I'm going to create a very simple object here. Obj is what we'll call it. And I'm going to have an F name property. Or hey, we'll make the F name and then I'm going to put an age of 45. That's all we'll have right now in this particular object. Let me go ahead and save that. Going to just resize this a bit. And we're gonna go to the console here. And first thing I want to do, There's our object. We can see that pretty easily. And what happens if I do value of it shows me the object as an object. So I can navigate through that. I can see the prototype. Where does that value of property come from, or that value of method it comes from the prototype. We can see that right here. Alright? So we can also verify that it doesn't belong to the object by doing has own property. And then inside of that doing value of like this, and that comes back false. So we know that the value of method is not a property on an object. That's pretty obvious at this point. But we also know that we can access it because it finds it on the prototype chain and it prints it out this way. Now, let's do something really quick. Let's do a five plus OBJ. Notice what gets printed out. Where does that come from? Well, that comes from the two string method. Generally, when an object needs to be converted to a primitive value, it uses the value of method to do that. But in the case of a string, a string situation, it will use two string to do that conversion. If value of returns, something that could be coerced into a string, then it will use value up. But right now what value of is returning cannot be coerced into a string. So we use the toString method. So let's just do a couple of changes here. Let's go back to that object. And I'm going to first do a toString method. And we'll have it return this dot F name. And That's what we're going to have. The two string method return. Whoops, I put a period there. There we go. Now let's save that. And let's jump out and do that same command again. And notice what we get because we overrode the toString method that is in the prototype chain, that is on the object's prototype. We now get the value that is designated by the toString method on the object itself. Alright, now let's add a value of method. And this can be very helpful if you need an object to be able to convert to a number. Let's just go ahead and do value of. And for this I'm going to have it returned a number which is the age. Now, let's save this. Let's jump out. Let's do that same thing again. And now look what happens. Now because this is a number. It's going to try to get a primitive value from the object using value of first, since value of is returning a number, then it's treated like a number and basically adds those two things together. Now what would happen if we had a string? Let's, let's do something like this. Object plus OBJ. Well, it accesses the value of method. It always tries to access that first if it needs to convert it to a primitive value. If it can coerce to a string, it will do that. 45 can be coerced to a string, and so it does, and it returns a string here. But since by default it is a number. When we use it with the number, it will return the number. It will act like a number, like we've seen here. So there we've overridden the properties, the methods that are on the prototype of an object, of an object we create. It's a part of the object constructor. And that is toString and value of we overrode those. So now this object uses the ones that are local to it instead of the ones on the prototype chain. Now, we've covered a lot about prototypes. And with this knowledge, we can begin applying them without the mystery of prototypes, too many people jump into working with objects in JavaScript and don't really understand what's going on behind the scenes. It is so much better to understand why things are working in a particular way and why prototypes do the things they do in JavaScript. And that's important to know because prototypes are used throughout JavaScript. Alright, let's move on. 7. What's Next?: You've come to the end of the critical fundamentals and concepts sections of mastering JavaScript. So what does next? Well, the next mastering JavaScript section will be on modules. But I would recommend you tackle some other topics first or at the same time that you're tackling modules. These other topics are in some of my other courses and so I want to point them out. First off, I think a critical concept, a critic and critical pattern in JavaScript is object oriented design. And that can be found in JavaScript advanced topics in that course. It's one of the parts of that course. And so I'd recommend if you haven't already to go through that and learn those different patterns that are available for object oriented programming. Then also functional programming has become an integral part of JavaScript lately. And so that's something else that I feel is critical. That's another pattern that I think is important to understand. I have an entire course on that, functional programming in JavaScript, a Practical Guide. This is really a practical approach to functional programming. So even if you don't use functional programming paradigm all the time, you can apply the concepts that are taught there. And I think they're critical to understand. And then finally, very critical is understanding asynchronous patterns in JavaScript. And that's an entire course as well. That's in asynchronous JavaScript deep dive. So I would recommend tackling those at the same time or before you tackle modules. Alright, keep learning and thanks for going through this course with me.