"JavaScript" inheritance

Source: Internet
Author: User

1. JS is actually a non-object-oriented language, through the object's depth of copy to complete the inheritance

2. Inheritance Methods

There are two ways to inherit

1) prototype prototype mode

As an example,

varAnimal =function () {     This. Type = ' Animal ';  This. tmp = {name: ' hehe '};  This. Eat =function(TMP) {Console.log (' Animal eat ');    };  This. modifytmp =function(TMP) { This. Tmp.name =tmp; }}varCat =function(name) { This. Type = ' Cat ';  This. Name =name;  This. Eat =function() {Console.log (' Cat eat: ' + This. Name); }}cat.prototype=NewAnimal ();varCAT1 =NewCat (' CAT1 '); Cat1.eat (); Cat1.modifytmp (' Lala '); Console.log (cat1.tmp); //Output LalavarCAT2 =NewCat (' Cat2 '); Cat2.eat (); Console.log (cat2.tmp); //Output Lala
View Code

Note: There are pits here!!

Why is the output of Lala inherited from the prototype?

Because the type of TMP at this time is an object or an array, when prototype inheritance is performed, the inheritance is actually done through the object reference, at which point Cat1 Cat2 is pointing to the same animal object. If the TMP is the underlying type (string,int), there is no reference, so there is no need to worry.

TMP can be redefined to an external CAT1 Cat2 object for re-copying, which will point to two different objects, as shown in the following example:

varAnimal =function () {     This. Type = ' Animal ';  This. tmp = {name: ' hehe '};  This. Eat =function(TMP) {Console.log (' Animal eat ');    };  This. modifytmp =function(TMP) { This. Tmp.name =tmp; return  This. tmp; }}varCat =function(name) { This. Type = ' Cat ';  This. Name =name;  This. tmp = {};  This. Eat =function() {Console.log (' Cat eat: ' + This. Name);  This. tmp = This. tmp; }}cat.prototype=NewAnimal ();varCAT1 =NewCat (' CAT1 '); Cat1.eat (); Cat1.tmp= cat1.modifytmp (' Lala '); Console.log (' CAT1 ', CAT1);//LalavarCAT2 =NewCat (' Cat2 '); Cat2.eat (); Cat2.tmp= Cat2.modifytmp (' Miaomiao '); Console.log (' Cat2: ', CAT2);//MiaomiaoConsole.log (' cat1 ', CAT1);//Lala
View Code

2) Call apply method

This is the use of this object bogus.

"JavaScript" 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.