So let’s look at an illustrative view of what this really means:
foo on either end of the chain. When the
Dog object is created, it has a property called prototype; this property is just like any other property you might give to a dogs, such as
numberOfLegs (another thing to adjust to when coming from Ruby is the convention of writing in camelcase!).
protoype of this instance of
Dog lists all of the properties that the instance of
Dog (e.g., a specific dog or maybe a type of dog, such as Bichon) will have once it is constructed. Now, once we create instances of
Dog, otherwise known as prototypes of
Dog, they will all have the properties that are defined in the
prototype property (for example, if we say that when we construct an instance of
Dog called Woofie, he should have all the properties and methods of
Dog but also specific those of the prototype of dog, such as a
Take a look at the diagram with the prototypes of
Now, when we look at the object of
Dog, it will have a property called
__proto__; this basically is a reference back to the
prototype object that created it. So, two key things to remember:
- The “`__proto__“` property is part of any object that is the prototype of another object (it was created using the prototype property of another object). This property holds the reference and points to the “`prototype“` that created it.
- The “`prototype“` property is part of the object that is being used to construct the prototype. The information contained in this property is what is used as a template to create the prototype object (e.g., “`woofie“`). This property also holds a reference of “`constructor“` that points back to the object that created it. For example, the protoype of “`woofie“` (remember, the prototype of “`Dog“`) would hold a reference to “`Dog“` as its “`constructor“`.
Now, the object
Dog was created the same way from the
Animal as we created
woofie from the
__proto__ property holds a reference to
null rather than to another prototype.
Enough talking, let’s look at some code! Let’s start by looking at one end of the chain at the object “foo”.
As you can see here, when you look at the constructor of the foo instance, you see that it is Foo since it was the prototype of Foo that created the instance of foo. Keep in mind that the constructor property here isn’t actually part of the foo object; it is part of the prototype object that the
In the example above, you basically traverse back to the object that created Foo by:
- Calling the prototype property of Foo
- Calling reference (“`__proto___“`) to the prototype that made Foo
- Looking at the constructor of that prototype, which is Bichon
Now, just for fun, lets take a look how we can traverse from the
foo object to the
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
For those who may be more visually inclined, here’s an illustration of this code that I put together of this traversal tht we’re doing:
1 2 3 4 5 6 7 8
Now, let’s add some other behaviors the instances of Dog using the prototype property. You call this just like you would call any other property of Dog.
1 2 3
Finally, let’s create Woofie! We use the Dog constructor to create Woofie.
Since Woofie has a
__proto__ reference to the Dog prototype, it will also, naturally inherit the ability to bark that we gave all the prototypes of Dog.