Combination Inheritance of JS inheritance (combining prototype chain inheritance and borrowing constructor inheritance)

Source: Internet
Author: User

Review the disadvantages of borrowing constructor inheritance

Let's look at the last code we used in borrowing constructor inheritance:

//Parent class: Peoplefunction Person () { This. Head ='Head';  This. Emotion = ['Hi','Anger','Grief','le'];//all people have emotions.       This. Eat =function () {Console.log (' Eat'); }       This. Sleep =function () {Console.log ('Sleep'); }       This. Run =function () {Console.log ('Run'); }    }    //Subclass: Student, inherit the category of "man"function Student (studentid) { This. StudentID =StudentID; Person.call ( This); }        //student.prototype = new Person ();    varSTU1 =NewStudent (1001); Console.log (stu1.emotion); //[' Hi ', ' anger ', ' grief ', ' le ']Stu1.emotion.push ('Sorrow'); Console.log (stu1.emotion); //["Hi", "anger", "sorrow", "Joy", "Sorrow")        varSTU2 =NewStudent (1002); Console.log (stu2.emotion); //["Hi", "anger", "sorrow", "le")

In this code, we use the constructor inheritance to ensure that both STU1 and STU2 have their own copy of the Parent property, so that their respective emotion are not affected. However, the problem is that both STU1 and STU2 copy all the properties and methods in the person class, and in the person class, such methods as Eat (), sleep (), run () should be common, without adding to each instance to increase memory, especially this When the class method is more.

Ii. combining two types of inheritance models

So we thought, can we put these methods on the prototype object of the parent class, implement the method reuse, then the subclass inherits through the prototype chain, can call these methods? ~

//Parent class: Peoplefunction Person () { This. Head ='Head';  This. Emotion = ['Hi','Anger','Grief','le'];//all people have emotions.    }    //put the method that needs to be shared in the person class into the prototype, and implement the ReusePerson.prototype.eat =function () {Console.log (' Eat'); } Person.prototype.sleep=function () {Console.log ('Sleep'); } Person.prototype.run=function () {Console.log ('Run'); }    //Subclass: Student, inherit the category of "man"function Student (studentid) { This. StudentID =StudentID; Person.call ( This); } Student.prototype=NewPerson ();//at this point the constructor in Student.prototype is rewritten, causing stu1.constructor = = = PersonStudent.prototype.constructor = Student;//Student The constructor pointer of the prototype object back to the Student itself    varSTU1 =NewStudent (1001); Console.log (stu1.emotion); //[' Hi ', ' anger ', ' grief ', ' le ']Stu1.emotion.push ('Sorrow'); Console.log (stu1.emotion); //["Hi", "anger", "sorrow", "Joy", "Sorrow")        varSTU2 =NewStudent (1002); Console.log (stu2.emotion); //["Hi", "anger", "sorrow", "le")stu1.eat ();// EatStu2.run ();//RunConsole.log (Stu1.constructor);//Student

First, we extract the method that needs to be reused in the person class into Person.prototype, and then set the Student prototype object as an instance of the person class so that STU1 can access the properties and methods on the person prototype object. Second, in order to ensure that STU1 and STU2 have their own copy of the Parent property, we use the Person.call (this) method in the Student constructor. In this way, the combination of the prototype chain inheritance and the borrowing of constructor inheritance, it is a perfect solution to the previous two of the shortcomings of each.

Combination Inheritance of JS inheritance (combining prototype chain inheritance and borrowing constructor 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.