First, the prototype inheritance
The inheritance model was proposed by Douglas Rockford in 2006 to implement the method of inheritance.
The basic idea of a pattern: a prototype allows you to create new objects based on existing objects without having to create custom types.
The code is as follows:
functionObject (o) {functionF () {//defines an object of type F This. Name= "111"; } F.prototype=o;//causes the prototype object of F to point to the incoming object, that is, F inherits the incoming object, or the prototype object that overrides F with the passed object is equivalent to the following code/*f.prototype={Name: "Zhang San", friends:["John Doe", "Harry"]};*/ return NewF ();//return F Object Note: There is a constructor in the prototype object that points to the F constructor, so this object includes both the F prototype object and the properties inside the F constructor.}varperson={name:"Zhang San", friends:["John Doe", "Harry"]};varperson1=object (person); Person1.name= "Plugin"; Person1.friends.push ("Zhao Liu");varPerson2=object (person); Person2.name= "Kobe"; Person2.friends.push ("Durrant"); alert (person1.friends); //output: John Doe, Harry, Zhao Liu, Durrant
This inheritance pattern is similar to the prototype constructor pattern, except that here the F constructor acts as a base object, and his prototype object is dynamically changing according to the incoming object! So according to this feature, by changing the property values of the incoming object, without the steps to create the constructor, so when we do not have to create a constructor selectmen, just want to keep one object similar to another object, the prototype inheritance is fully capable!
Object-oriented learning of JavaScript nine (prototype inheritance and parasitic inheritance)