Object-oriented encapsulation , inheritance , described how to "encapsulate" the data and methods, and how to generate an instance from the prototype object, today to look into the object-oriented constructor inheritance ;
Today's introduction is about five ways to " inherit " between objects.
1. First we define two constructors,
// For example, there is a cat's constructor function; function Animal () { this. Species = "Animal";} // There's also a cat's constructor function Cat (name,color) { this. Name = name; this. color = color;}
2. The first method of call apply method inheritance;
function Animal () { this . Species = "Animal" ;} // implement inheritance, using the call apply method function Cat (name,color) {animal.call ( this " Span style= "color: #000000;" >,arguments); this . Name = name; color;} var cat1 = new Cat (' barley ', ' black '
Ps: Change this point by the call method, thus inheriting the attributes of animal;
3. The second method of prototype mode
functionAnimal () { This. Species = "animals";}functionCat (name,color) { This. Name =name; This. color =color;}//prototype ModeCat.prototype =NewAnimal ();//here point to become animal;//point the pointer back to cat;Cat.prototype.constructor =Cat; Cat.prototype.run=function() {alert ( This. Name)}varCAT1 =NewCat (' barley ', ' black '); Console.log (cat1.species);
Ps: It is noteworthy that constructor point, must let him re-point himself;
4. Third Direct prototype inheritance
The third method is the improvement of the second method. Because of the animal object, the invariant property can be written directly to Animal.prototype. So, we can also let cat () skip Animal () and inherit Animal.prototype directly.
functionCat (name,color) { This. Name =name; This. color =color;}functionAnimal () {}; Animal.prototype.species= ' animals '; Cat.prototype= Animal.prototype;//The disadvantage is that the two point to an object at the same time, when modifying any one will affect the other; //It is noteworthy that this piece of pointing, must let him again point to himself;Cat.prototype.constructor =Cat; varCAT1 =NewCat (' da Mao ', ' black '); Console.log (cat1.species);//Animal
PS: The disadvantage is that both points to an object, when modifying any one of the time will affect the other;
5. The fourth method uses the empty object as the medium
Since the "Direct inheritance prototype" has the disadvantages mentioned above, there is a fourth method, using an empty object as the intermediary.
functionAnimal () {}; Animal.prototype.species= ' animals ';functionCat (name,color) { This. Name =name; This. color =color;}//f is an empty object, so it hardly accounts for memory. At this point, modifying the cat's prototype object does not affect the animal prototype object. varF =function (){}; F.prototype=Animal.prototype; Cat.prototype=NewF (); Cat.prototype.constructor=Cat; varCAT1 =NewCat (); Console.log (cat1.species);
When used, we can encapsulate the above;
//we encapsulate the above into a function;functionExtend (child,parent) {varF =function (){}; F.prototype=Parent.prototype; Child.prototype=NewF (); Child.prototype.constructor=Child ;//It means setting an Uber property for the sub-object, which points directly to the parent object's prototype property. (Uber is a German word that means "up", "up".) This is equivalent to opening a channel on a child object that can call the parent object's method directly. This line is put here, just to achieve the completeness of inheritance, is purely an alternative nature. Child.uber =Parent.prototype;} Extend (cat,animal);varCAT1 =NewCat (); Console.log (cat1.species);
6, the last method copy inheritance
functionAnimal () {}; Animal.prototype.species= ' animals ';functionCat (name,color) { This. Name =name; This. color =color;}functionExtend2 (child,parent) {varp =Parent.prototype; varc =Child.prototype; for(varIinchp) {C[i]=P[i]} c.uber=p;} Extend2 (cat,animal);varCAT1 =NewCat (); Console.log (cat1.species)//Animal
Object-oriented--inheritance of constructor function