How to implement JS inheritance:
Now that we want to achieve inheritance, we first have to have a parent class, the code is as follows:
function Animal(name) { // attribute this.name = name || 'Xiao Bai'; // Example method this.sleep = function () { console.log(this.name + 'Sleeping in!'); } //Instance Reference Properties this.features = ['11', '22']; } // Prototyping method Animal.prototype.eat = function (food) { console.log(this.name + 'I am eating:' + food); };
1. Inheritance of Prototype Chain
Core: Prototype parent class instances as subclasses
function Cat(name) {} //hold Cat Prototype orientation Animal Cat.prototype = new Animal(); var tom = new Cat('Tom'); var kissy = new Cat('Kissy'); console.log(tom.name); // "Xiao Bai" console.log(kissy.name); // "Xiao Bai" console.log(tom.features); // ["11", "22"] console.log(kissy.features); // ["11", "22"] tom.name = 'Small black'; tom.features.push('33'); //For parent instance value type member changes, no impact console.log(tom.name); // "Small black" console.log(kissy.name); // "Xiao Bai" //Changes in the reference type members for parent instances can affect other subclass instances by affecting other subclass instances console.log(tom.features); // ["11", "22", "33"] console.log(kissy.features); // ["11", "22", "33"]
2. Constructional Inheritance
Core: Using constructors of parent classes to enhance subclass instances is equivalent to copying instance attributes of parent classes to subclasses (prototypes are not used)
function Cat(name) { Animal.call(this); this.name = name || 'Small black'; } var cat = new Cat(); var animl = new Animal(); console.log(cat.name);//Small black cat.sleep();//Xiao Hei is sleeping in. console.log(animl.name);//Xiao Bai animl.name = 'Chinese rhubarb'; console.log(animl.name);//Chinese rhubarb cat.sleep();//Xiao Hei is sleeping in! console.log(cat.name);//Small black console.log(cat instanceof Animal); // false console.log(cat instanceof Cat); // true
3. Instance Inheritance
Core: Adding new features to parent instances and returning them as child instances
function Cat(name) { var instance = new Animal(); instance.name = name || 'Small black'; return instance; } var cat = new Cat(); console.log(cat.name);//Small black cat.sleep();//Xiao Hei is sleeping in! cat.features.push('33') console.log(cat.features);//["11", "22", "33"] console.log(cat instanceof Animal); // true console.log(cat instanceof Cat); // false
4. Copy Inheritance
Core: Copy every object of the parent class through the for loop
//Go through the cycle copy Each item of the parent class function Cat(name) { var animal = new Animal(); for (var p in animal) { Cat.prototype[p] = animal[p]; } Cat.prototype.name = name || 'Small black'; } var cat = new Cat(); console.log(cat.name);//Small black cat.sleep();//Xiao Hei is sleeping in! console.log(cat instanceof Animal); // false console.log(cat instanceof Cat); // true
5. Combinatorial Inheritance
Core: By calling the parent class structure, inheriting the properties of the parent class and retaining the merits of parameter transfer, and then by using the parent class instance as the prototype of the child class, function reuse is realized.
function Person(name, age, sex) { this.name = name; this.age = age; this.sex = sex; } Person.prototype.sayHi = function () { console.log("I am Person Method"); }; function Student(name, age, sex, score) { //Borrowing constructors:The problem of duplication of attribute values Person.call(this, name, age, sex); this.score = score; } //Changing the Orientation of Prototype----inherit Student.prototype = new Person(); //No passing value //Change the orientation of the prototype back to the original Student.prototype.constructor = Student; Student.prototype.eat = function () { console.log("I am Student Method"); }; var stu = new Student("Small black", 20, "male", "100 branch"); console.log(stu.name, stu.age, stu.sex, stu.score);//Xiao Hei 20 Males 100 Points stu.sayHi();//I am Person Method stu.eat();//I am Student Method