This article describes the method of implementing multiple inheritance in JavaScript. Share it for your reference, as follows:
1. Define an empty parent class constructor, and then define properties and methods for the parent class through prototype
2. Define an empty subclass constructor, then bind the subclass prototype to the instance of the parent class, and then bind the parent class of the subclass prototype to the instance of the parent class. Set your own properties and methods for subclasses through prototype.
3. Define an empty grand class constructor, then bind the grand class prototype to the instance of the subclass, and then bind the parent class of the grand class prototype to the instance of the subclass. Define your own properties and methods for the grandson class through prototype method.
4. Instantiate a grandchild object, by calling the instance object and calling its own methods, you can also call the parent class of the grandchild class, that is, the subclass in the text, or directly call the largest parent class, that is, the methods of the parent class here, or add properties and methods to the current object.
function Person(){} = "people";// Create a name attribute for humans = function(content){// Create a way to speak for humans if(!){ // If the object does not have a name attribute, use the name of the prototype chain = this.__proto__.name; } ("I am" + + ", I want to say"+content); }; function Parent(){} = new Person(); // Set Parent class to inherit Person class = new Person();// Set the method properties of superClass to save the parent class Person = "Parents";// Set the name attribute of Parent class = function(){// Set the Parent class's own say method ("I'm the Parent class says method!"); }; function Child(){} = new Parent();// Set Child class to inherit Parent class = new Parent();// Set the method properties of superClass to save the parent class Parent = function(){ //Set the Child class's own say method ("I'm the Child class saying method!"); } var c = new Child();// Instantiate a Child object(); // Call the says method of its own prototype, output: I am the says method of the Child class!(); // Call the say method of the parent class Parent, output: I am the say method of the Parent class!("Ha ha");// Directly call the say method of the largest parent class Person (this in the method points to Person), output: I am a human, I want to say haha"// Use call to call the say method of the largest parent class Person (this in the method points to the instantiated object c, but at this time c does not have a name attribute, so Parent's name is used)(c,"whee"); // Output: I am a parent, I want to say hee = "Subclass instance";// Add name attribute to the current object// Or use call to call the say method of the largest parent class Person (at this time there is a name attribute in the c object);(c,"I am an instantiated object of a subclass"); // Output: I am a subclass instance, I want to say I am an instantiated object of the subclass
ps:Multiple inheritance can add a new attribute to the prototype object to save the parent class object and attribute. When the child class calls, use superClass to point out the parent class method. This solves the problem that the parent class and the child class method have the same name, and the child class will overwrite the parent class method after the child class inherits the parent class.
For more information about JavaScript, readers who are interested in reading this site's special topic:JavaScript object-oriented tutorial》、《Summary of JavaScript Errors and Debugging Skills》、《Summary of JavaScript data structure and algorithm techniques》、《JavaScript traversal algorithm and skills summary"and"Summary of JavaScript mathematical operations usage》
I hope this article will be helpful to everyone's JavaScript programming.