The inheritance problem in JS

Source: Internet
Author: User

1, the concept of inheritance: take someone else's to become their own, but not affected by themselves. 2, JS in the most basic inheritance is the prototype inheritance. 3. Prototype inheritance: an instance object that points to the parent constructor by modifying the prototype of the child constructor.     function Animal (name) {        this.name=name;        this.favor=[' eating ', ' sleeping '];   }    cat.prototype=new Animal (' Kitty ');    function Cat (color) {        this.color=color;   }    var cat=new Cat (' Blue '); &nb Sp   Cat.favor.push (' swimming ');    var cat1=new cat (' Pink ');    Console.log (cat.name+cat.color +cat.favor);    Console.log (Cat1.name+cat1.color+cat1.favor);  prototype Inheritance disadvantages: (1) You cannot pass parameters to a parent constructor. Here I'm new n creates an instance function of cat, but the name inside this function is still kitty. (2) data from a reference type in a parent constructor is shared by its own constructor instance. A swimming is added to the cat's favor, but CAT1 also has a swimming 4, which is borrowed from the constructor to inherit the parent's all-amount attribute. function Animal (name) {        this.name=name;        this.favor=[' eating ', ' Sleeping '];   }animal.prototype.say=function () {      &NBsp Console.log (' hello ');   }//This behavior does not inherit     function Cat (color,name) {        Animal.call (this,name)         this.color=color;   }    var cat=new cat (' Blue ', ' Tom ');    cat.favor.push (' swimming ');    var cat1=new cat (' Pink ', ' Candy ');       cat.say ()     Console.log (cat.name+ ' = = = ' +cat.color+ ' = = = ' +cat.favor);    Cat1.say ()     Console.log (cat1.name+ ' = = = ' +cat1.color+ ' = = = ' +cat1.favor); The two drawbacks of prototype inheritance are solved by using constructor inheritance, but there are new problems. Members in the parent constructor prototype cannot be inherited.  5, combination inheritance   function Animal (name) {        this.name=name;        this.favor=[' eating ', ' sleeping '];   }    animal.prototype.say=function () {        Console.log (' hello ');   }    function Cat (color,name) {        Animal.call (this,name)         this.color=color;   }&nbsp   Cat.prototype=new Animal ();    var cat=new Cat (' Blue ', ' Tom ');    cat.favor.push (' swimming ') ;    var cat1=new Cat (' Pink ', ' Candy ');    Cat.say ()     Console.log (cat.name+ ' = = = ' + cat.color+ ' = = = ' +cat.favor);    Cat1.say ()     Console.log (cat1.name+ ' = = = ' +cat1.color+ ' = = = ' + Cat1.favor); Disadvantage: Although this method solves all the problems of appeal, the singular has a small shortcoming. The properties in the parent constructor are redundant (wasting memory).

Inheritance issues in

JS

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.