Use inheritance in Javascript Object-Oriented Programming (2)

Source: Internet
Author: User
Yesterday, I threw a bunch of JavaScript class 'inherited' Code Not all of these codes can be executed normally. It's not an inheritance class code that I don't mean to write, but these methods have their own advantages and disadvantages. Next I will explain their principles and precautions.

Principles of constructing Inheritance Law:

The key code for constructing the inheritance method is in function arraylist01:

This . Base = Collectionbase;
This . Base ();

The base here is not the concept of base in the C # derived class, it is completely an arbitrary Javascript variable name. Calling this. Base (); is actually the execution of collectionbase ();,NoNew collectionbase (); oh! Without the new base class, how can we get the methods and attributes in collectionbase? Here we use an hack in this scope, which 'foo' the script engine. When we call this from the constructor of the arraylist01 class. in the base class collectionbase, this is an instance of arraylist01. Therefore, the constructor of collectionbase is executed, attach the Member and method of the base class to the arraylist01 instance dynamically. The constructor problem is generated here. Pay attention to it.

Defects in Construction of inheritance law:

The first problem lies in the above two sections of code, because there is no new base class. The problem is that you cannot attach the prototype attributes and methods in the base class collectionbase to the arraylist01 instance. What is the inheritance ?! In the previous article, I implemented an add () method for the collectionbase class without modifying it (just to unify the sample code ). The solution to this defect is actually very simple, that is, the base class should not use prototype to import attributes and Methods. Instead, all attributes and methods should be written to the constructor, respectively: this. attribute = ...; and this. method = function (){...}; this form.
The second problem is this. base = collectionbase; and this. base (); must be written at the beginning of the constructor of the derived class (not necessarily the first and second rows, but not the front of them. XXX is defined as any property or method imported by arraylist01), because this. base (); will inject the attributes and methods of the base class to this (an arraylist01 instance). If the base class has the same names as the imported attributes and methods in this, the method in the subclass is automatically overwritten.
The third problem is that the subclass cannot use prototype to import attributes and methods. This is the same as the duplicate name overwrite principle in question 2, because prototype import attributes and methods are generated when subclass 1 is new, there are also potential error threats that are overwritten by duplicate names of the base class. Solution: Do not use prototype like the basic class writing rules, and place the subclass attributes and method definition code in the imported inherited code (this. base = collectionbase; this. base.

Example of constructing Inheritance Law:

< Script Language = "JavaScript" >
Document. Write ('Structure Inheritance Method: < BR > ');
VaR Arraylist11 =   New Arraylist01 ();
Arraylist11.add ('A ');
Arraylist11.add ('B ');
Arraylist11.foo ();
VaR Arraylist12 =   New Arraylist01 ();
Arraylist12.add ('A ');
Arraylist12.add ('B ');
Arraylist12.add ('C ');
Arraylist12.foo ();
</ Script >

The running result is as follows:

Construct the Inheritance Law:
[Class arraylist01]: 2 : A, B
[Class arraylist01]: 3 : A, B, C

Summary: The Construction Inheritance Method of JavaScript looks more intuitive, because the subclass constructor calls the base class constructor, and it seems that it is more acceptable in syntax. However, the absence of a new base class leads to the defect that prototype import properties and methods cannot be obtained. Although it is not difficult to solve this defect, it is not feasible to restrict the writing of the base class to a rule that prevails over the Javascript syntax specification. The subclass cannot use the prototype feature to import attributes and methods. There may be duplicate name overwriting problems between the subclass and the base class. Therefore, this inheritance method is not recommended for complex base classes. Because classes are complicated, prototype can be used to compile class code to make the class definition more comfortable.

Application Scenario: inheritance between small-scale classes. The attribute methods of the base class and subclass are 5-8. In addition, when the attributes and methods of the class are imported by assigning values in the constructor, instead of the writing habits of the class imported by prototype.

To be continued...

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.