5 ways JavaScript simulates Java implementation inheritance

Source: Internet
Author: User


1, inherit the first way: Object posing

  functionParent (username) { This. Username =username;  This. Hello =function() {alert ( This. username); }   }   functionChild (Username,password) {//inheritance is implemented by appending the properties and methods of the parent to child in the following 3 rows    //The first step: This.method is as a temporary property and points to the object that the parent points to,    //Step Two: Execute the This.method method, which is the object function that the parent points to    //Step three: Destroy the This.method attribute, which means that now child has all the properties and methods of the parent     This. method =Parent;  This. method (username);//The most critical line    Delete  This. method;  This. Password =password;  This. World =function() {alert ( This. Password); }   }   varParent =NewParent ("Zhangsan"); varChild =NewChild ("Lisi", "123456");   Parent.hello ();   Child.hello (); Child.world (); 


2, Inherit the second way: Call () method mode

The call method is a method in a function class The value of the first parameter of the calling method is assigned to a class (that is, a method) that occurs when the second parameter of the This call method is first assigned to a parameter that is accepted by the class (that is, the method)functionTest (str) {alert ( This. Name + "" +str); }   varObject =NewObject (); Object.name= "Zhangsan"; Test.call (Object,"Langsin");//at this point, the first parameter value, object, is passed to this that appears in the test class (that is, the method), and the second parameter "Langsin" is assigned to the test class (that is, the method) str  functionParent (username) { This. Username =username;  This. Hello =function() {alert ( This. username); }   }   functionChild (Username,password) {Parent.call ( This, username);  This. Password =password;  This. World =function() {alert ( This. Password); }   }   varParent =NewParent ("Zhangsan"); varChild =NewChild ("Lisi", "123456");   Parent.hello ();   Child.hello (); Child.world (); 



3, the third way of inheritance: Apply () method mode
The Apply method accepts 2 parameters,
A, the first parameter is the same as the first parameter of the call method, which is the value that appears in the class (that is, the method).
B, the second parameter is an array type, and each element in the arrays is assigned to a parameter that is accepted by the class (that is, the method)

 functionParent (username) { This. Username =username;  This. Hello =function() {alert ( This. username); }   }   functionChild (Username,password) {parent.apply ( This,NewArray (username));  This. Password =password;  This. World =function() {alert ( This. Password); }   }   varParent =NewParent ("Zhangsan"); varChild =NewChild ("Lisi", "123456");   Parent.hello ();   Child.hello (); Child.world (); 4, inheritance of the fourth way: The prototype chain, that is, the subclass by prototype all the properties and methods appended to the parent class through prototype are appended to child, thus implementing the inheritancefunctionPerson () {} Person.prototype.hello= "Hello"; Person.prototype.sayHello=function() {alert ( This. Hello); }     functionChild () {} Child.prototype=NewPerson ();//The function of this line is to append all attributes and methods appended by prototype to child in the parent, thus implementing the inheritanceChild.prototype.world = "World"; Child.prototype.sayWorld=function() {alert ( This. World); }     varc =NewChild ();   C.sayhello (); C.sayworld (); 
 functionParent (username) { This. Username =username;  This. Hello =function() {alert ( This. username); }   }   functionChild (Username,password) {parent.apply ( This,NewArray (username));  This. Password =password;  This. World =function() {alert ( This. Password); }   }   varParent =NewParent ("Zhangsan"); varChild =NewChild ("Lisi", "123456");   Parent.hello ();   Child.hello (); Child.world (); 4, inheritance of the fourth way: The prototype chain, that is, the subclass by prototype all the properties and methods appended to the parent class through prototype are appended to child, thus implementing the inheritancefunctionPerson () {} Person.prototype.hello= "Hello"; Person.prototype.sayHello=function() {alert ( This. Hello); }     functionChild () {} Child.prototype=NewPerson ();//The function of this line is to append all attributes and methods appended by prototype to child in the parent, thus implementing the inheritanceChild.prototype.world = "World"; Child.prototype.sayWorld=function() {alert ( This. World); }     varc =NewChild ();   C.sayhello (); C.sayworld (); 



5, the fifth way of inheritance: Mixed mode
Hybrid call mode, prototype chain mode

functionParent (hello) { This. Hello =Hello; } Parent.prototype.sayHello=function() {alert ( This. Hello); }   functionChild (Hello,world) {Parent.call ( This, hello);//inherit the properties of the parent class .     This. World = World;//Add some properties} child.prototype=NewParent ();//inherit the methods of the parent class .Child.prototype.sayWorld=function(){//add some new methodsAlert This. World); }   varc =NewChild ("Zhangsan", "Lisi");   C.sayhello (); C.sayworld ();

5 ways JavaScript simulates Java implementation 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.