Using the call method to implement inheritance in JS

Source: Internet
Author: User

Tags: style blog http java color IO re c

Explanations are written in the comments inside, there is no place please shoot bricks, thank you!

<HTMLxmlns= ""><Head>    <title>JS implements inheritance by call method</title></Head><Body>    <Scripttype= "Text/javascript">        /*There are many implementations of the inheritance in JS, and today we discuss the inheritance through the call method.        This method is relatively simple and easy to understand compared with the prototype chain inheritance.        So, let's start with a simple explanation of the call method: The calling method can invoke a method with one object instead of another. This sentence is very difficult to understand, or to see the sample code bar, more intuitive and clear:*/        functionA () { This. Name= "I'm a .";  This. ShowName= function() {alert ( This. Name); }        }        functionB () { This. Name= "I'm a b ."; }        var_a= NewA (); var_b= Newb (); //the following line of code means: use _b instead of _a to execute _a showname method.         //so the execution object is changed from _a to _b, and this at this point in the ShowName method refers to B, not a.        //so the output is: I'm b (_b); /*Ah, the language did not learn, always feel the expression is not clear, do not know whether you can understand.        Well, to get to the point, let's look at the implementation of inheritance. */        /*first we need to define 4 classes: Animals (animal), people (person), Chinese (Chinese), Japanese (Japanese). The code is as follows:*/        functionanimal () { This. Eat= function() {alert ("animals have to eat."); }        }        functionPerson () { This. Say= function() {alert ("people can talk."); }        }//function Chinese () {// = function () {//alert ("I am Chinese");//            }//        }//function Japanese () {//This.ja = function () {//alert ("I am Japanese");//            }//        }        /*the relationship between the 4 classes is then implemented as follows: The Chinese inherit from the people the Japanese inherit from animals and people description:            In JS, you can implement multiple inheritance of a class.                        So Japanese can inherit from both animal and person at the same time, unlike the C # and Java languages, which support multiple inheritance of interfaces, and do not support multiple inheritance of classes. Modify the above Chinese and Japanese as follows:*/        functionChinese () { ( This); //inherit from person class (replace person with Chinese)             This. Ch= function() {alert ("I am a Chinese"); }        }        functionJapanese () { ( This); //inherit from Animal ( This); //inherit from Person class             This. Ja= function() {alert ("I'm a Japanese ."); }        }        /*we know that when inheritance is implemented, subclasses can have public fields and methods of the parent class.            And the parent class does not have any of the subclasses. Description: The fields in JS, methods are also public and private points.                       Subclasses can access only the public fields and methods of the parent class.            About the definition of public, private, global, static, etc. in JS I'll write another blog post to discuss with you. Take a look at the following code:*/        //instantiating an object        varC= NewChinese (); varJ= NewJapanese (); varP= NewPerson (); C.say (); //The Say method that owns the parent class (); //the Ch method of its (); //Eat method with parent class animalJ.say (); //The Say method that owns the parent class personJ.ja (); //your own Ja methodP.say (); //self-say method        /*The following two sentences will be an error, because the parent class person does not have anything of the subclass, so p does not have ch and JA methods.   ();        P.ja (); */    </Script></Body></HTML>


Related Article

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: and provide relevant evidence. A staff member will contact you within 5 working days.

Tags Index: