Several methods of object inheritance in js

1 prototype chain inheritance
All javascript will inherit a prototype object to inherit the properties and methods on the prototype object.
For example:
The date object inherits properties and methods from the Date.prototype prototype prototype object
The array object inherits properties and methods from the Array.prototype prototype prototype object
To inherit, there must be a parent class ---- the child class inherits the parent class's properties and methods

Features: the instance can inherit the prototype property of the constructor property in its own instance property parent
Disadvantage: cannot inherit prototype properties and methods of subclasses
Too single, only single inheritance, not multiple inheritance
After inheritance, all the properties on the prototype object are shared
Subclass cannot pass parameters directly to parent

function Person() {
        this.name;
        this.sex;
        this.sleep = function () {
            return "Sleep";
        };
        this.eat = function () {
            return "Having dinner"
        }
    }
        //Add prototype properties and methods to parent class
    Person.prototype.job = function () {
        return "work";
    };
    Person.prototype.color = "yellow";
        function Child() {
        this.age = 20;
    }
        Child.prototype = new Person();  //The core makes the prototype of the subclass equal to the instance object of the parent class
         var child = new Child();
         console.log(Child.prototype);//Point to Person object after inheritance
          console.log(child instanceof Child);  ///true
    console.log(child instanceof Person);  //true

2 constructor
Directly using call apply inheritance
Construct inheritance is written directly inside the subclass
Advantages: multiple inheritance can be realized; parameters can be passed to the parent class
Disadvantages: the instance of a subclass is itself not a parent class; construction inheritance can only call apply the construction properties and methods of the parent class object, but cannot copy the prototype properties and methods

 //Parent class
    function Animail(s, a) {
        this.sex = s;
        this.age = a;
        this.sleep = function () {
            return "Sleep";
        }
    }
    //Animal add prototype properties
    Animail.prototype.color = "Flower color";
    //Animal class
    function Type(t) {
        this.type = t;
    }
    //Subclass
    function Cat(n, s, a, t) {
        this.name = n;
        this.eat = function () {
            return "Eat something"
        }
        Animail.call(this, s, a);
        Type.apply(this, [t]);
    }

    //Instantiate subclass object
    var cat = new Cat("kitten", "common", 2, "Feline family");  //Class objects will directly execute their own constructors when instantiated
    console.log(cat);
        /Detect class problems in construction inheritance
    console.log(cat instanceof Cat);//true
    console.log(cat instanceof Animail);//false
    console.log(cat instanceof Type);//false

3 instance inheritance
The principle is to directly construct the instance of the parent class in the subclass
Advantage: you can pass parameters to the parent class in an unlimited way
Disadvantages: can't inherit more; can't get the construction properties and methods of subclasses
An instance inherits an instance of a subclass that is not itself but a parent class

 //Parent class
    function Person(n, s) {
        this.name = n;
        this.sex = s;
        this.sleep = function () {
            console.log(this.name + "Sleep");
        }
    }
    //Subclass
    function Child(n, s) {
        var per = new Person(n, s);
        return per;
    }
    //Instantiate subclass object
    var child = new Child("Zhang San", "female");
         console.log(child instanceof Child);//false
    console.log(child instanceof Person);//true

4 copy inheritance
The principle is to copy the attribute methods in the parent class to the child class
An instance of a subclass is itself not a parent
Subclass passing parameters to parent
Can support multiple inheritance

function Animal(n) {
        this.name = n;
        this.sleep = function () {
            return this.name + "Sleep"
        }
    }
    function Cat(n, a) {
        this.age = a;
        //Copy
        var animal = new Animal(n);
        for (var p in animal) {
            Cat.prototype[p] = animal[p];
        }
        //Continue copying new objects
    }
    /* Cat.prototype.name="";
     Cat.prototype.sleep=function (){

     };*/
    //Instantiation subclass
    var cat = new Cat("Floret", 3);
    console.log(cat);
         console.log(cat instanceof Cat);//true
    console.log(cat instanceof Animal);//false

5 combined inheritance
Construction inheritance + prototype chain inheritance
An instance of a subclass is itself a parent
No sharing of prototype object properties
Implement multiple inheritance
Constructor of parent class called twice

    function Person(n) {
        this.name = n;
        this.sleep = function () {
            return this.name + "Sleep";
        }
    }
    Person.prototype = {
        job: function () {
            return this.name + "job";
        }
    }

    function Child(n, a, s) {
        this.age = a;
        this.sex = s;
        //Structural inheritance
        Person.call(this, n);
    }
    //Prototype chain inheritance
    Child.prototype = new Person();
    var child = new Child("Zhang San", 18, "male");
        console.log(child);
        console.log(child instanceof Child); //true
    console.log(child instanceof Person);//true

6 parasitic combination inheritance
It is a disadvantage of handling combination inheritance to avoid calling the constructor of the parent class twice
The principle is to give the prototype of the parent class to the prototype of an empty object
An instance of a subclass object is itself a parent

function Person(n) {
        this.name = n;
        this.sleep = function () {
            return this.name + "Sleep";
        }
    }
    console.log(Person.prototype);

    function Child(n, a) {
        this.age = a;
        this.eat = function () {
            return this.name + "Having dinner"
        }
        Person.call(this, n);
    }
    //parasitic
    (function () {
        var fn = function () {
        };
        //Give the prototype object of the parent class to an empty object
        fn.prototype = Person.prototype;
        Child.prototype = new fn();
    })();

    var child = new Child("Li Si", 20);
    console.log(child);
        console.log(child instanceof Child); //true
    console.log(child instanceof Person); //true

Keywords: Javascript Attribute

Added by ElectricMessiah on Mon, 09 Mar 2020 10:23:37 +0200