You can get something for nothing

Inheritance: Let the child have all the attributes and methods of the parent

Carry it forward: Child objects have their own properties and methods based on parent objects

JS features: Born to inherit

Such as:

lets = { }; s.toString(); // Why toString? Retrieved from __proto__Copy the code
letA = [1,2] a.link, a.port // retrieved from __proto__Copy the code

Next, there are six types of inheritance in JS

Here’s the prototype:

// B is the prototype of A, A is the constructor of B. Prototype = BCopy the code

Prototype chain inheritance

  • The basic idea

Inheritance is implemented using a stereotype chain, with an instance of the parent class as the stereotype of the subclass

  • The specific implementation
function Parent(yourname){
    this.name = yourname;
}
Parent.prototype.say = function(){
    console.log(this.name)
}

functionSon(yourname){ this.name = yourname; } / / let the Son to inherit to the say method on the Parent Son. The prototype = Parent. The prototype / / modify the constructor to Son. The prototype. The constructor = Son;let p = new Parent("Hello")
p.say();

let s = new Son("world")
s.say()
Copy the code
  • The advantages and disadvantages

1. Advantages:

  • Simple and clear
  • An instance is an instance of a subclass and, in effect, an instance of a superclass
  • New stereotype methods/attributes in the parent class that are accessible to all subclasses

2. Disadvantages:

  • The stereotypes of all subclass instances share the properties and methods of the same superclass instance
  • Multiple inheritance cannot be implemented

Tectonic inheritance

  • The basic idea

By using the call, apply methods, you can perform constructors on newly created objects and add instances of subclasses using the parent class’s constructor

  • The specific implementation
function Parent(yourname) {
	this.name = yourname;
}
Parent.prototype.say = function() {console.log(this.name)} // let Son inherit the name attribute and the say methodfunctionParent.call(this,yourname)} Son(yourname) {// Parent.call(this,yourname)} Son.prototype.constructor = Son;Copy the code
  • The advantages and disadvantages

1. Advantages:

  • It inherits the attributes and methods of the parent constructor directly

2. Disadvantages:

  • Unable to inherit properties and methods on the stereotype chain

Combination of inheritance

  • The basic idea

Use structural inheritance and prototype chain composition

  • The specific implementation
function Parent(yourname) {
	this.name = yourname;
}
Parent.prototype.say = function() {console.log(this.name)} // let Son inherit the name attribute and the say methodfunctionSon(yourname) {// Inherit attribute parent.call (this, // Son. Prototype = Parent. Prototype; // Son. // Son.prototype.constructor = Son; // Parent. Prototype loop an object to usefor in
for(let i in Parent.prototype){
	Son.prototype[i] = Parent.prototype[i]
}
Son.prototype.constructor = Son;
Copy the code
  • The advantages and disadvantages

1. Advantages:

  • Two problems of structure inheritance and prototype chain inheritance are solved

2. Disadvantages:

  • In fact, a subclass will have two copies of the parent class’s attributes, but the attributes of the subclass override the attributes of the parent class

Primary inheritance

  • The basic idea

You don’t need to define a class, pass in the argument obj, and generate an object that inherits the obj object

  • The specific implementation
function object(o){
 function F(){}
 F.prototype = o;
 return new F();
} 


var person = {
 name: "Nicholas",
 friends: ["Shelby"."Court"."Van"]}; var anotherPerson = object(person); anotherPerson.name ="Greg";
anotherPerson.friends.push("Rob");

var yetAnotherPerson = object(person);
yetAnotherPerson.name = "Linda";
yetAnotherPerson.friends.push("Barbie");

alert(person.friends); //"Shelby,Court,Van,Rob,Barbie" 
Copy the code
  • The advantages and disadvantages

1. Advantages:

  • Generate an object that inherits from an object directly from an object

2. Disadvantages:

  • It’s not class inheritance, it’s prototypal foundation, it’s missing the concept of class

Parasitic inheritance

  • The basic idea

Create a function that simply encapsulates the inheritance process, then internally enhance the object in some way, and finally return the object

  • The specific implementation
function objectCreate(obj){
  function F(){}
  F.prototype = obj
  return new F()
}
function createAnother(original) {
	var clone = object(original);
	clone.sayHi = function() {
		alert("hi");
	};
	return clone;
}
Copy the code
  • The advantages and disadvantages

1. Advantages:

  • An extension of the original type inheritance

2. Disadvantages:

  • There is still no concept of classes

Parasitic combinatorial inheritance

  • The basic idea

The combination of parasitic inheritance and combined inheritance perfectly realizes the inheritance without two superclass attributes

  • The specific implementation
function object(o) {
	function F(){}
	F.prototype = o;
	return new F();
}

function inheritPrototype(superType, subType) {
	var prototype = object(superType.prototype);
	prototype.constructor = subType;
	subType.prototype = prototype;
}

function SuperType(name) {
	this.name = name;
	this.colors = ["red"."blue"."green"];
}

SuperType.prototype.sayName = function() {
	alert(this.name);
};

functionSubType(name, age) { SuperType.call(this, name); this.age = age; } inheritPrototype(SuperType, SubType); Subtype.prototype = new SuperType() subtype.prototype.sayage =function() {
	alert(this.age);
};
Copy the code
  • The advantages and disadvantages

1. Advantages:

  • Perfect implementation of inheritance, to solve the combined inheritance with two attributes

2. Disadvantages:

  • Too tedious, so it is better to combine inheritance

The above is JS inheritance of six ways, need their own practice to master oh!


^ _ <