mozilla

Revision 117528 of No Multiple Inheritance

  • Revision slug: JavaScript/Guide/Obsolete_Pages/Property_Inheritance_Revisited/No_Multiple_Inheritance
  • Revision title: No Multiple Inheritance
  • Revision id: 117528
  • Created:
  • Creator: Bkimman
  • Is current revision? No
  • Comment

Revision Content

No Multiple Inheritance

Some object-oriented languages allow multiple inheritance. That is, an object can inherit the properties and values from unrelated parent objects. JavaScript does not support multiple inheritance.

Inheritance of property values occurs at run time by JavaScript searching the prototype chain of an object to find a value. Because an object has a single associated prototype, JavaScript cannot dynamically inherit from more than one prototype chain.

In JavaScript, you can have a constructor function call more than one other constructor function within it. This gives the illusion of multiple inheritance. For example, consider the following statements:

function Hobbyist (hobby) {
   this.hobby = hobby || "scuba";
}

function Engineer (name, projs, mach, hobby) {
   this.base1 = WorkerBee;
   this.base1(name, "engineering", projs);
   this.base2 = Hobbyist;
   this.base2(hobby);
   this.machine = mach || "";
}
Engineer.prototype = new WorkerBee;

dennis = new Engineer("Doe, Dennis", ["collabra"], "hugo")

Further assume that the definition of WorkerBee is as used earlier in this chapter. In this case, the dennis object has these properties:

dennis.name == "Doe, Dennis"
dennis.dept == "engineering"
dennis.projects == ["collabra"]
dennis.machine == "hugo"
dennis.hobby == "scuba"

So dennis does get the hobby property from the Hobbyist constructor. However, assume you then add a property to the Hobbyist constructor's prototype:

Hobbyist.prototype.equipment = ["mask", "fins", "regulator", "bcd"]

The dennis object does not inherit this new property.

{{template.PreviousNext("Core_JavaScript_1.5_Guide:Property_Inheritance_Revisited:Global_Information_in_Constructors", "Core_JavaScript_1.5_Guide:LiveConnect_Overview:Working_with_Wrappers")}}

Revision Source

<p>
</p>
<h3 name="No_Multiple_Inheritance"> No Multiple Inheritance </h3>
<p>Some object-oriented languages allow multiple inheritance. That is, an object can inherit the properties and values from unrelated parent objects. JavaScript does not support multiple inheritance.
</p><p>Inheritance of property values occurs at run time by JavaScript searching the prototype chain of an object to find a value. Because an object has a single associated prototype, JavaScript cannot dynamically inherit from more than one prototype chain.
</p><p>In JavaScript, you can have a constructor function call more than one other constructor function within it. This gives the illusion of multiple inheritance. For example, consider the following statements:
</p>
<pre>function Hobbyist (hobby) {
   this.hobby = hobby || "scuba";
}

function Engineer (name, projs, mach, hobby) {
   this.base1 = WorkerBee;
   this.base1(name, "engineering", projs);
   this.base2 = Hobbyist;
   this.base2(hobby);
   this.machine = mach || "";
}
Engineer.prototype = new WorkerBee;

dennis = new Engineer("Doe, Dennis", ["collabra"], "hugo")
</pre>
<p>Further assume that the definition of <code>WorkerBee</code> is as used earlier in this chapter. In this case, the dennis object has these properties:
</p>
<pre>dennis.name == "Doe, Dennis"
dennis.dept == "engineering"
dennis.projects == ["collabra"]
dennis.machine == "hugo"
dennis.hobby == "scuba"
</pre>
<p>So dennis does get the <code>hobby</code> property from the <code>Hobbyist</code> constructor. However, assume you then add a property to the <code>Hobbyist</code> constructor's prototype:
</p>
<pre>Hobbyist.prototype.equipment = ["mask", "fins", "regulator", "bcd"]
</pre>
<p>The <code>dennis</code> object does not inherit this new property.
</p><p>{{template.PreviousNext("Core_JavaScript_1.5_Guide:Property_Inheritance_Revisited:Global_Information_in_Constructors", "Core_JavaScript_1.5_Guide:LiveConnect_Overview:Working_with_Wrappers")}}
</p>
Revert to this revision