Parasitic Combination Inheritance


Combination inheritance is the most often - used pattern for inheritance in JavaScript, though it is not without its inefficiencies. The most inefficient part of the pattern is that the supertype constructor is always called twice: once to create the subtype's prototype, and once inside the subtype constructor. Essentially, the subtype property ends up with all of the instance properties of a supertype object, only to have it overwritten when the subtype constructor executes. Consider the combination inheritance example again:

			
						
			
			

executed. When this code is executed, SubType.prototype ends up with two properties: name and colors. These are instance properties for SuperType, but they are now on the SubType's prototype. When the SubType constructor is called, the SuperType constructor is also called, which creates instance properties name and colors on the new object that mask the properties on the prototype.

The code in this example returns a new object based on person. The anotherPerson object has all of the properties and methods of person but adds a new method called sayHi() .Parasitic inheritance is another pattern to use when you are concerned primarily with objects and not with custom types and constructors. The object() method is not required for parasitic inheritance; any function that returns a new object fits the pattern.