Prototype chain inheritance

Advantage: Using the parent instance as the prototype for the child Disadvantage: all attributes of the parent class are shared by the subclasses. If you change the attributes of a subclass, other attributes change

function father () {
    this.name = 'xxx'
    this.obj = {
        name:}}function Son () {}

Son.prototype = new father()
var son = new Son()
son.name = 'yyy'
Copy the code

Borrowing constructor

Advantages: Directly use instance attributes of the parent class, and can pass parameters. Disadvantages: Cannot reuse methods, and methods on the parent class cannot be used

function Father (name) {
    this.name = 'fuyu'
}
function son (name) {
    Father.call(this, name)
}
Copy the code

Combination of inheritance

Optimization: retains methods that can pass arguments and preserve the prototype chain, and does not share the reference attributes of the parent class. Disadvantages: Calls the parent class twice, generating two instances

function Father() {
    this.name = 'xxx'
    this.arr = [1]}function Son (name) {
    Father.call(this, name)
}
Son.prototype = new Father()
Son.prototype.constructor = Son
Copy the code

Optimization of 1

function Father() {
    this.name = 'xxx'
}
function Son () {
    Father.call(this)
}
Son.prototype = Father.prototype
Son.prototype.constrcutor = Son
Copy the code

Perfect model

function Father () {
    this.name = 'xxx'
}
function Son () {
    Father.call(this)
}
Son.prototype = Object.create(Father.protptype)
Son.prototype.constrcutor = Son
Copy the code