Original: JS in the call method to achieve inheritance
Explanations are written in the comments inside, there is no place please shoot bricks, thank you!
<HTMLxmlns= "http://www.w3.org/1999/xhtml"><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 ._a.showname.call (_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 () {//this.ch = 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 () {Person.call ( This); //inherit from person class (replace person with Chinese) This. Ch= function() {alert ("I am a Chinese"); } } functionJapanese () {Animal.call ( This); //inherit from Animal classPerson.call ( 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 personc.ch (); //the Ch method of its ownj.eat (); //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.ch (); P.ja (); */ </Script></Body></HTML>
JS through the call method to implement inheritance