2. Borrowing constructors
To solve the problem with the reference type value contained in the prototype, we use a technique called a borrowing constructor (constructor stealing) (also known as a forged object or classic inheritance).
The basic idea of this technique is to call the superclass constructor inside the subclass constructor.
You can perform constructors on newly created child class objects by using the Apply () and call () methods.
function supertype () {
this.colors = ["Red", "Blue", "green"];
}
Function subtype () {
//Inherits Supertype
supertype.apply (this);
}
var Instance1 = new subtype ();
Instance1.colors.push ("Black");
alert (instance1.colors); Red,blue,green,black
var instance2 = new subtype ();
alert (instance2.colors); Red,blue,green
In the above example, the Supertype constructor is actually called in the context of the newly created subtype instance (Instance1 instance2). As a result, all object initialization code defined in the Supertype () function is executed on the new subtype object. So each instance of subtype will have its own copy of the colors attribute.
Passing parameters
For a prototype chain, the advantage of borrowing a constructor is that it is possible to want the superclass constructor to pass parameters in the subtype constructor.
function Supertype (name) {
this.name = name;
}
Function subtype () {
Supertype.call (this, "Bob");
This.age =;
}
var Instance1 = new subtype ();
alert (instance1.age);
alert (instance1.name); Bob
To borrow a constructor question:
Methods are defined in the constructor, so the function reuse is not discussed. Also, the methods defined in the superclass stereotype are not visible to the subtype.
3. Combined inheritance
Combinatorial inheritance (combination inheritance), sometimes called the classic inheritance of perjury, refers to the combination of the prototype chain and the technology that borrows the constructor. In order to give play to both the length of an inheritance model.
Using the prototype chain to implement the inheritance of the prototype properties and methods;
Inheritance of instance properties is implemented by borrowing constructors.
In this way, both functions are reused by defining methods on the prototype, and each instance has its own attributes.
function Supertype (name) {
this.name = name;
This.colors = ["Red", "Blue", "green"];
}
SuperType.prototype.sayName = function () {
alert (this.name);
}
Function subtype (name, age) {
//Inheritance Property
Supertype.call (this, name);
This.age = age;
}
Inheritance method
Subtype.prototype = new Supertype ();
SubType.prototype.sayAge = function () {
alert (this.age);
}
var Instance1 = new Subtype ("Bob");
Instance1.colors.push ("Black");
alert (instance1.colors); Red,blue,green,black
instance1.sayname (); Bob
instance1.sayage ();
var instance2 = new Subtype ("Alice");
alert (instance2.colors); Red,blue,green
instance2.sayname (); Alice
instance2.sayage (); 21st
In this example, the Supertype constructor defines two properties: name and Colors. Supertype's prototype defines a method Sayname ().
The subtype constructor passed the name parameter when it called the Supertype constructor, defining his own attribute age. The supertype instance is then assigned to the subtype prototype. A method Sayage () is defined on the prototype.
This allows two different subtype instances to have their own properties-including the colors attribute-and the same method.
The above article on JS inheritance _ Borrow constructor & Modular inheritance is small to share all the content of everyone, hope to give you a reference, but also hope that we support the cloud-dwelling community.