On class inheritance of Javascript

Source: Internet
Author: User

SpeakingJavascriptOfClass inheritanceThe prototype chain is inevitable, but the inheritance implemented only through the prototype chain has many defects.

No parameter class inheritance issues

Let's take A look at the sample code to implement B inherited from:

 
 
  1. function A() {  
  2. }  
  3. A.prototype.a1 = function() {  
  4. };  
  5. function B() {  
  6. }  
  7. B.prototype = new A();  
  8. B.prototype.b1 = function() {  
  9. };  
  10. var b = new B();  
  11. alert(b.constructor == A); // true  
  12. alert(b.constructor == B); // false 

The main question of this Code is:

1. You need to instantiate A as the prototype of B and then execute the constructor of. However, according to the object-oriented rules, the constructor of B and its parent class A should not be executed before B is instantiated.

2. Changed prototype of B, resulting in B. constructor not B but.

Questions about inheritance of parameters

Assume that A and B both have two string parameters s1 and s2. A calculates the total length of the two strings. B calls A directly with s1 and s2 as the parameters:

 
 
  1. function A(s1, s2)   
  2. {  
  3. this.totalLength = s1.length + s2.length;  
  4. }  
  5. A.prototype.a1 = function()   
  6. {  
  7. };  
  8. function B(s1, s2)   
  9. {  
  10. }  
  11. B.prototype = new A();  
  12. B.prototype.b1 = function()   
  13. {  
  14. };  
  15. new B(“ab”, “123″); 

We can see that there is no way to upload s1 and s2 to A in this Code, but there is no parameter when instantiating A as A prototype of B, so an exception occurs:

 
 
  1. s1 is undefined 

Solution

The s1 and s2 scopes are only within B. to pass them to A, they can only be operated in B. This can be achieved through the apply method of the function:

 
 
  1. function B(s1, s2)   
  2. {  
  3. A.apply(this, arguments);  
  4. alert(this.totalLength);  

The next question is how to add the method to the prototype of B. This is not difficult. Just traverse A. prototype and copy the method to B. prototype. It should be noted that for methods with the same name, the subclass is naturally overloaded), so it cannot be overwritten:

 
 
  1. For (var m in A. prototype)
  2. {
  3. If (! B. prototype [m])
  4. {// The parent class cannot override the subclass method.
  5. B. prototype [m] = A. prototype [m];
  6. }
  7. }

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: 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.