JavaScript implements multiple inheritance

Source: Internet
Author: User

1. Define an empty parent class constructor, and then define the properties and methods for the parent class by prototype

2. Define a constructor for an empty subclass, then bind the prototype of the subclass to an instance of the parent class, and then bind the parent class of the child class prototype to the instance of the parent class. Set your own properties and methods for subclasses in a prototype way.

3. Define an empty grandchild class constructor, then bind the prototype of the grandchild class to an instance of the subclass, and then bind the parent class of the grandchild prototype to an instance of the child class. Define your own properties and methods for the grandchild class in prototype way.

4. Instantiate a grandchild object by invoking the instance object, calling its own method, or calling the parent class of the grandchild class as a method of the subclass, or calling the maximum parent class, the method of the parent class here, or adding properties and methods to the current object.

function person () {}person.prototype.name = "human";//create a name attribute for humans Person.prototype.say = function (content) {//    Create a speaking method for humans if (!this.name) {//If the object does not exist with the name attribute, the name this.name = This.__proto__.name of the prototype chain is used; } 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 ();//Set Superclass Save the parent class person's Method property Parent.prototype.name = "Parental class";//Set the parent class's Name property Parent.prototype.say = function () {//Set the parent class's own say method Console.log ("I am the say method of the parent class! ");}; function Child () {}child.prototype = new parent ();//Set child class to inherit parent class Child.prototype.superClass = new parent ();// Set superclass to save the parent's Method Property Child.prototype.say = function () {//Set the child class's own say method Console.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 its own prototype, output: I am the child class say Method! C.superclass.say (); Call the parent class say method, output: I am the say method of the parent class! C.superclass.superclass.say ("haha");//The Say method that calls the largest parent class person directly (this in the method points to person), output: I am human, I want to say haha "//Call the say method of the largest parent class person by calling (this in the method points to instantiating object C, but at this point C does not have a Name property, so THIS.name uses the name of the parent) C.superclass.superclass.say.call (c, "hehe"); Output: I am the father of the class, I want to say hee-c.name = "subclass instance";//Add the Name property to the current object//or call the Say method that calls the maximum parent class person (the name attribute already appears in the C object); C.superclass.superclass.say.call (c, "I am the instantiated object of a subclass"); Output: I am a subclass instance and I want to say I am a subclass of instantiated objects

  

PS: Multiple inheritance can add a property on the prototype object to hold the parent class's objects and properties, and when the subclass calls, use superclass to point out the parent class method, which solves the problem that the parent class and the subclass method have the same name, and the subclass inherits the parent class and overrides the parent class method.

Reference:

http://blog.csdn.net/zhy416011189/article/details/50980016

http://blog.csdn.net/turnhead/article/details/32333771

JavaScript implements multiple inheritance

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.