Use inheritance in JavaScript object-oriented programming (2)
Yesterday, I threw a bunch of JavaScript class 'inheritance', which is actually not all of them. It is not that I don't have to write inheritance class, but these methods themselves have their own advantages and disadvantages. Below I say their principles and precautions.
The principle of constructing inheritance method:
Constructing the key code is
Function arraylist01 ():
THIS
.base
=
CollectionBase;
THIS
.base ();
The Base here is not the Base in the C # derived class, which is completely an arbitrary JavaScript variable name. transfer
THIS.BASE (); actually the collectionBase ();
Not
New collectionBase (); Oh! No
How do you get the methods and properties in CollectionBase? Here used here
A Hack in the action domain, 'deceived' has a script engine. When we call from the constructor of class arraylist01
THIS.BASE (); time, in the base class CollectionBase
This is an instance of ArrayList01, so that the constructor of CollectionBase is executed, the member and method attach of the base class and method Attach are dynamically in the ArrayList01 instance. The problem of constructing is also generated here, pay attention.
Defects of constructing inheritance method:
The first question, the key is on the above two code, because there is no
NEW base class. The problem brings to the instance of the prototype attribute and method Attach in the base class CollectionBase, Attach, this is, is this inherited ?! So
In the last article, in order not to change the CollectionBase class, an Add () method is separately implemented (only for unified sample code). The way to solve this defect is also very simple, that is, the base class cannot use prototype to import attributes and methods, and write all attributes and methods to the constructor, respectively, respectively:
this.attribute = ...; and
this.method =
Function () {...}; this form.
The second question is
THIS.BASE = CollectionBase; and
THIS.BASE (); must be written at the beginning of the derived class constructor (not necessarily the first line and the second line, but the front of them can't have
This.xxx is defined for any attribute or method imported by arrayList01) because
THIS.BASE ();
Injects the properties and methods of the base class in this (an instance of ArrayList01), if there is and
The imported properties and methods are renamed in this this, the method in the fall is automatically overwritten.
The third issue is that subclasses cannot use prototype to import attributes and methods, just like the rendering of the question II, because prototype imported attributes and methods in subclavab
When New is generated, there is also a potential error threat that is overwritten with the base class. Solution, do not use Prototype, and use Prototype, and to define the code in the import inheritance of the subclass inheritance.
THIS.BASE = CollectionBase;
This.base ();).
Example for constructing inheritance method:
<
Script
Language
= "JavaScript"
>
Document.write ('Constructor:
); 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 example operation results are:
Construction inheritance: [Class ArrayList01]:
2
: A, b [Class ArrayList01]:
3
: A, b, c
Summary: The construction of the JavaScript does actually looks more intuitive, because there is a call to the base class constructor in the subclass constructor, which seems to be easily accepted in grammar. But because there is no
The New Base class brings a defect that cannot get the properties and methods imported by Prototype. Although the method of solving this defect is not difficult, it can be written to the base class to limit a rule that is over the JavaScript syntax specification, and operability is not very good. Subclasses cannot use the Prototype characteristics to import attributes and methods, and there is potential renowning override problems between the base classes. Therefore, for complex base classes, this inheritance method is not recommended because the class is complex, using prototype can standardize class code, so that the definition of the class looks more comfortable
.
Application Scene: Inheritance, base classes and subclasses of small-scale classes are 5-8. There is also the properties and methods imported in the constructor to import classes, without using Prototype to import the habit.
To Be Continue ...