Parasitic inheritance
Parasitic inheritance is a kind of thought that is closely related to prototype inheritance. The parasitic basis is similar to parasitic constructors and factory patterns, creating a function that encapsulates only the inheritance process, which in some way enhances the object, and finally returns the object as if it did all the work itself.
The following code demonstrates the parasitic inheritance pattern.
function Object (o) {
function F () {
}
F.prototype=o;
return new F ();
};
function Createanother (original) { var// Create a new object by calling functions function// enhancement of the object alert (" hi") in some way; return // return this object }
In this example, the Createanother () function receives a parameter, which is the object that will be the basis of the new object. The object (original) is then passed to an object () function and the returned result is assigned to clone. Add a Sayhi method to it, and finally return to the Clone object.
You can use the Createanother () method as follows
var person={ name:"Nicholas", friends:["Shelby", "Court", "Van"] var now =// hi
A new object is returned by Createanother () based on the person object------now; not only has all the methods and properties of the person object, but also has its own Sayhi method;
Parasitic inheritance is also a useful pattern in situations where objects are primarily considered rather than custom types and constructors. The object () function used in the previous demonstration of inheritance mode is not required, and any function that returns a new object applies to this pattern.
Note: using parasitic inheritance to add functions to an object is inefficient due to the inability to achieve function reuse, similar to the constructor pattern.
JavaScript-----------------Parasitic Inheritance