Object prototypes is another one of those topics in JavaScript that I get excited about. prototypes are the things that make the object oriented approach JavaScript takes so powerful. Now, every JavaScript object has at least a second JavaScript object that it inherits properties from. And this is called the prototype. So if the prototype of an object has certain properties, certain methods, well, the object itself can use those same properties and methods, it inherits those from the prototype. So basically, the way it works is if you call a property of an object, it will first look in its own object definition and see if it can find it.
If not, it will look in it prototype definition. If it can't find it in its immediate prototype, it will look in the next prototype. So that is the whole process of inheritance and the chaining of properties between objects. Let me pull up a diagram to help explain this even further. So prototypal inheritance, that's the term we use in JavaScript to talk about this whole concept. Here we have an object.
User one is the reference we use for it. That's the variable that points to the object. And then we have some properties in there we have username, age, address, city, and then we also have a method the full address method. Now if this object were created, using the object literal, or using the object constructor, either one, its prototype object would be object prototype. And so there would be a link, the object will have a link to its product Type in in this example we're showing here, the prototype actually has a property state. Now, user one object does not have a property state if it did, it would use that if I tried to address state, but since it does not have it, it links to its prototype and searches the prototype for that property.
Now, I also mentioned the property chaining, or the prototype chaining. Its prototype may also be linked to a prototype. And so it would continue to search through the prototypes and until it got to the prototype that is the highest level, which in JavaScript is the object dot prototype that's at the highest level. Now let me open up the console and we'll explore these concepts a little bit more. So I'm going to create an object using the object literal. This is one we used in the previous session.
Now I'm going to display that object by simply typing it in, I can press this down arrow to display the contents of the object, the different properties of the object. Now notice down here at the bottom, this protocol, this is the link to its prototype, this is where it shows that link. If I were to open that up, suddenly I get a whole lot of additional things that I can do with my object simply from the prototype. My object inherits all of these functions, etc, so that I can do more with my object. For example, there is a two string function. So the object I just created the customer object, I can type to string.
Now function there's nothing really exciting about it is simply indicates that this is an object. That's all it does. But I inherited that from its prototype, I did not have to create it. Now, as we've talked about arrays are also objects. And since they're objects, they link to a prototype as well. And so when we were doing things like array dot length, well, where does that length come from?
Well, it is available because of the array prototype, which all arrays link to, has the first prototype. The same thing for functions. When things are available within functions. It's because of the prototype. So if I were to type array dot, and then in our console, we see all the different things I can access from that array object. index.
Some of these, we've learned join, all of those are defined in the array prototype. So prototypes can be very powerful for JavaScript. And in the next movie, we're going to look at how you can specify the prototype of an object using another object. Therefore, you can set up an object with certain properties, certain methods that you want multiple objects to have. And you simply make that object, the prototype of those other objects. So let's move on to the next movie and talk about that.