Please enable JavaScript to use CodeHS

Advanced Objects (Pt 2) in JavaScript

In this tutorial, you will learn about the object prototype, how to customize one for your constructor, and how to link prototypes to create a chain of inheritance.

By Ryan Hart

At the bottom it says "Prototype," and if we expand it, we see: <img src=https://codehs.com/tutorial/ryan/"https://codehs.com/uploads/03baaa9c69490ea02848ab39f7359b48"> Low and behold, if you look closely at the 2nd line under Prototype, you see the method `hasOwnProperty()` listed! Our `animal` object was able to call this method because it was in its prototype! This prototype is the default Object prototype. <br> <h2> The Communal Pantry: a Shared Prototype </h2> If the case for using an object constructor for multiple objects isn't already convincing enough, then add a "shared object prototype" to that pro list. When you create multiple objects via the same constructor, all of those objects are linked to the same prototype. That's because all functions have a `prototype` property, and when the function is called as a constructor, the value becomes the prototype for the newly created object. Think of the prototype as a communal pantry in an apartment building -- if one of the residents doesn't have their own spice (property or method), they can look to the communal pantry (prototype) and use what's there instead. All of the residents (individual objects), *inherit* supplies (properties and methods) from communal space (prototype). <img src=https://codehs.com/tutorial/ryan/"https://codehs.com/uploads/65652667d161fe0c2630e56b913a2b75" > This functionality is quite valuable, which we'll talk about why in a moment. Let's first learn how you can customize the prototype created by the constructor. <br> <h2> Customizing the Shared Constructor Prototype </h2> As mentioned, the object constructor has a `.prototype` property that we can use to configure the prototype of the objects it creates. To add methods or properties to this prototype, it looks like: `constructorName.prototype.keyName = value`. Here's a simple example to demonstrate the setup: ```javascript function Animal(firstName, animalType) { this.name = firstName; this.type = animalType; } Animal.prototype.speak = function() { console.log("Hi! My name is " + this.name + ". I am a " + this.type + "."); } let tracy = new Animal("Tracy", "turtle"); tracy.speak(); // prints "Hi! My name is Tracy. I am a turtle." ``` <br> Notice that the `turtle` object does NOT have the speak method itself, but it is able to call the method because it was added to the `Animal` prototype. Just like defining the values in the constructor, you can use the `this` keyword to refer to the individual object. You have to be a little careful though, the method call (`speak()`) has to come after the line that adds the method to the prototype. <hr> Try running this example in the editor below. Can you add another method to the Animal prototype?">
This chain of prototypes is really a chain of inheritance. This will become clearer with an example: along with the Animal constructor, let's add a Dog constructor. The Dog constructor will use the Animal object as its prototype, so that if you create a dog object, its prototype will be an Animal. Therefore every dog will have access to its own methods, then its prototype's methods (ie Animal's methods), and finally its prototype's prototype's methods (ie Animal's prototype's methods). <img src=https://codehs.com/tutorial/ryan/"https://codehs.com/uploads/6cbb9ad8d6c5d7a91fe4646d7b949d23"> Let's visualize this with some pseudocode: ```linenums function Animal(parameters) { properties and methods for all animals } Animal.prototype.method = function for Animal's prototype function Dog(parameters) { properties and methods for just dogs } Dog.prototype = Animal object let karel = new Dog(arguments); ``` <br> Here we can see that `karel` is a dog object and is going to be given Dog properties and methods from its constructor (line 8). Since its prototype is set to the the Animal object on line 11, it will inherit all of the properties and methods in the Animal constructor (line 2); it will also inherit all of the properties and methods from Animal's prototype (line 5). Not all animals are dogs, so they only have access to the Animal properties and methods (and not those from Dog), but all dogs are animals, so they inherit the Animal properties and methods. <hr> Here is this actual code in the live editor for you to run.">
<br> **Tracy (animal):** <img src=https://codehs.com/tutorial/ryan/"https://codehs.com/uploads/6e65296c0a60044e98f3f044f17d3ffe">">