What is inheritance?

Inheritance is a concept in object-oriented software technology. It is one of the three basic characteristics of object-oriented software along with polymorphism and encapsulation. Inheritance can cause a child class to have properties and methods of its parent class or redefine, append properties and methods, etc.

Prototype chain inheritance

Implement: child.prototype = new Father(

Stereotype inheritance inherits only stereotype properties first define a function (as a parent)

Function Father (name) {enclosing name = 'a'} / / add the stereotype properties Father. The prototype. Location = 'tianjin' const f1 = new Father () console.log(f1)Copy the code

Defining a child class (inheriting from a parent)

Function Child(school) {this.school = school this.name = 'liu'}Copy the code

Use prototype chain inheritance

Const s1 = new Child(' prototype ') console.log(s1)Copy the code

This completes the prototype chain inheritance

Tectonic inheritance

Features: Private only

Define the parent class

function Father (name,age){
  this.name=name
  his.age=age 
}
Copy the code

Define subclasses

Function Child(name,age,school){Father. Call (this,name,age) this. School =school Log (s1) Child(' 1 ',23,' 1 ') console.log(s1)Copy the code

Composite inheritance: prototype chain inheritance + construct inheritance

Stereotype chain inheritance: take the private property and stereotype property of the parent class as the stereotype property of the child class and construct the property: take the private property of the parent class as the private property of the child class

function Father(name, age) { this.name = name this.age = age } Father.prototype.say = function () { console.log('hello world') } function Child(name, age, school) {// construct a parent. call(this, name, school) Age). This school = school} / / prototype inheritance Child. The prototype = new Father ()/group/repair the constructor to the Child. The prototype. The constructor = Const c1 = new Child(' span ', 23, 'Hua hua ') console.log(c1)Copy the code