Combination Inheritance


Combination inheritance (sometimes also called pseudoclassical inheritance) combines prototype chaining and constructor stealing to get the best of each approach. The basic idea is to use prototype chaining to inherit properties and methods on the prototype, and to use constructor stealing to inherit instance properties. This allows function reuse by defining methods on the prototype and allows each instance to have its own properties. Consider the following:

Implementing prototype chaining involves the following code pattern:

			
						
			
			

In this example, the SuperType constructor defines two properties, name and colors , and the SuperType prototype has a single method called sayName() . The SubType constructor calls the SuperType constructor, passing in the name argument, and defines its own property called age . Additionally, the SubType prototype is assigned to be an instance of SuperType , and then a new method called sayAge() is defined. With this code, it � s then possible to create two separate instances of SubType that have their own properties, including the colors property, but all use the same methods. Addressing the downsides of both prototype chaining and constructor stealing, combination inheritance is the most frequently used inheritance pattern in JavaScript. It also preserves the behavior of instanceof and isPrototypeOf() for identifying the composition of objects.