Java Reference
In-Depth Information
Inheritance
The examples we've seen so far have all demonstrated inheritance by inheriting properties
and methods from the prototype object. But the prototype object also has its own prototype
object, which has its own prototype object ... and so on, creating a chain of inheritance .
The Prototype Chain
We can see an example of a prototype chain by looking at the prototype of the mike instance
that we created in the last section, using the __proto__ property:
mike.__proto__
<< {"attack": function (){
return this.name + " hits you with his " +
this.weapon;
}, "eat": function () {
return "Mmm, this " + this.food + " tastes great!";
}, "food": "Pizza"}
All objects have constructor functions, so we can look at what is the constructor function of
the prototype object. As you'd expect, it's the Turtle() constructor function:
mike.__proto__.constructor === Turtle;
<< true
The prototype of the prototype is an apparently empty object literal (although it's actually
not empty, as we'll see later):
mike.__proto__.__proto__
<< {}
The constructor for this is the built-in Object() constructor function:
mike.__proto__.constructor
<< function Object() {
Search WWH ::




Custom Search