JavaScript implements Multi-Inheritance Method Analysis and javascript inheritance

Source: Internet
Author: User

JavaScript implements Multi-Inheritance Method Analysis and javascript inheritance

This example describes how to implement multiple inheritance in JavaScript. We will share this with you for your reference. The details are as follows:

1. Define an empty parent class constructor and define attributes and methods for the parent class in prototype mode.

2. Define an empty subclass constructor, bind the subclass prototype to the parent class instance, and then bind the parent class of the subclass prototype to the parent class instance. You can use prototype to set attributes and methods for sub-classes.

3. Define an empty sun class constructor, bind the sun class prototype to the subclass instance, and then bind the parent class of the Sun class prototype to the subclass instance. Define attributes and methods for Sun class in prototype mode.

4. instantiate a sun-Class Object. by calling this instance object and calling its own method, you can also call the sun-class parent class, that is, the sub-class method in the text, you can also directly call the maximum parent class, that is, the method of the parent class, or add attributes and methods for the current object.

Function Person () {} Person. prototype. name = "Person"; // create a name attribute Person for humans. prototype. say = function (content) {// create a speaking Method for humans if (! This. name) {// if the object does not have the name attribute, use the name of the prototype chain this. name = this. _ proto __. name;} console. log ("I am" + this. name + ", I want to say" + content) ;}; function Parent () {} Parent. prototype = new Person (); // set the Parent class to inherit the Person class Parent. prototype. superClass = new Person (); // sets the superClass to save the method attribute Parent of the Parent class Person. prototype. name = "Parent class"; // set the name attribute of the Parent class. prototype. say = function () {// set the say method console of the Parent class. log ("I am the s of the Parent class Ay method! ") ;}; Function Child () {} Child. prototype = new Parent (); // set the Child class to inherit from the Child class. prototype. superClass = new Parent (); // sets the superClass to save the method attribute Child of the Parent class Parent. prototype. say = function () {// set the say method console of the Child class. log ("I am the say method of the Child class! ");} Var c = new Child (); // instantiate a Child object c. say (); // call the say method of the prototype. Output: I am a say method of the Child class! C. superClass. say (); // call the say method of the Parent class. Output: I am the say method of the Parent class! C. superClass. superClass. say ("Haha"); // directly call the say method of the largest parent class Person (this in the method points to Person), output: I am a Person, 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 the name attribute, so this. name uses the name of Parent) c. superClass. superClass. say. call (c, ""); // output: I am a parent class. name = "subclass instance"; // Add the name attribute to the current object // or call the say method of the maximum parent class Person using call (the name attribute already exists in the c object ); c. superClass. superClass. say. call (c, "I am a subclass's instantiated object"); // output: I am a subclass instance, and I want to say that I am a subclass's instantiated object

Ps:Multi-inheritance allows you to add a property on the prototype object to save the objects and attributes of the parent class. When the subclass calls the property, superClass points out the parent class method, in this way, the parent class and the subclass method have the same name. After the subclass inherits the parent class, it will overwrite the parent class method.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.