// Parent constructor
function Person(name){
this.name = name;
this.sum = function(){
alert(this.name)
}
}
// Attributes and methods on the prototype
Person.prototype.age = 10;
Copy the code
Prototype chain inheritance (function prototype points to an instance of another function)
- You can inherit constructor properties and methods,
- You can also inherit properties and methods from the stereotype chain
- An instantiated child class cannot pass arguments to its parent class
// Prototype chain inheritance
function Per(){
}
Per.prototype = new Person();// Can pass parameters, not practical
var per1 = new Per()// Cannot pass the parameter to Person
Copy the code
Constructor inheritance (object impersonation implementation inheritance)
- Can only inherit properties and methods from constructors (not from prototypes)
- Instantiating a child class can pass arguments to its parent class
// borrow constructor inheritance
function Con(){
// Only the properties and methods in Person can be retrieved
// There is no instance of new Person(), so methods on the prototype cannot be inherited
Person.call(this."jer");
this.age = 12;
}
var con1 = new Con();
console.log(con1.name);//jer
console.log(con1.age);/ / 12
console.log(con1 instanceof Person);//false
Copy the code
3. Combinatorial inheritance (combinatorial prototype chain inheritance and borrowed constructor inheritance)
- You can inherit properties and methods of constructors as well as properties and methods on the stereotype chain.
- Instantiating a child class can pass arguments to its parent class
- The superclass constructor is called twice, consuming memory
// Combine prototype chain and constructor inheritance
function SubType(name){
Person.call(this,name);// borrow constructor mode}
//SubType.prototype = Person.prototype ; // Prototype chain inheritance
SubType.prototype = new Person();// Prototype chain inheritance
var sub = new SubType("gar"); // Pass parameters
console.log(sub.name);//gar inherits constructor attributes
console.log(sub.age);//10 inherits the attributes of the superclass prototype
Copy the code
Iv. Inheritance of the original type
- All instances inherit properties from the stereotype
- Reuse cannot be achieved. (New instance attributes are added later)
// Encapsulate a function container that outputs objects and holds inherited stereotypes
function content(obj){
function F(){};
F.prototype = obj;// Inherits the parameters passed in
return new F();
}
var sup = new Person();// Get an instance of the parent class
var sup1 = content(sup);
console.log(sup1.age);// 10 inherits the attributes of the parent function
Copy the code
Parasitic inheritance
- Formal inheritance has a shell around the prototype
- Can’t reuse
function content(obj){
function F(){};
F.prototype = obj;// Inherits the parameters passed in
return new F();// Return the function object
}
var sup = new Person();
// The prototype is inherited and a shell is passed to the prototype
function subobject(obj){
var sub = content(obj);
sub.name = "gar";
return sub;
}
var sup2 = subobject(sup);
// This function is declared to be an object to which attributes can be added
console.log(typeof subobject);//function
console.log(typeof sup2);//object
console.log(sup2.name);//gar returns a sub object that inherits the properties of sub
Copy the code
Vi. Parasitic Combination Inheritance (common)
/ / the parasitic
function content(obj){
function F(){};
F.prototype = obj;
return new F();
}
// The prototype of the con instance (F instance) inherits the prototype of the superclass function
var con = content(Person.prototype);
// Inherits attributes and methods from the parent constructor
function Sub(){
Person.call(this);
}
// Inherits the con instance
Sub.prototype = con;
// add the con constructor to Sub
con.constructor = Sub;
// An instance of sub inherits the constructor property from its parent class, con
var sub1 = new Sub();
console.log(sub1.age);/ / 10;
Copy the code
// The above and following examples have the same effect
function F(){};
F.prototype = Person.prototype;
function Sub(){
Person.call(this);
}
Sub.prototype = new F();
var sub2 = new Sub();
console.log(sub2.age);
Copy the code