Closely related to prototypal inheritance is the concept of parasitic inheritance, another pattern popularized by Crockford. The idea behind parasitic inheritance is similar to that of the parasitic constructor and factory patterns: create a function that does the inheritance, augments the object in some way, and then returns the object as if it did all the work. The basic parasitic inheritance pattern looks like this:
In this code, the createAnother() function accepts a single argument, which is the object to base a new object on. This object, original , is passed into the object() function, and the result is assigned to clone . Next, the clone object is changed to have a property called newProperty . The last step is to return the object. The createAnother() function can be used in the above mentioned way:
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.