Use inheritance in JavaScript object-oriented programming (5)

xiaoxiao2021-03-06  38

Use inheritance in JavaScript object-oriented programming (5)

I have to go back to my hometown for the New Year tomorrow, about this

The topic of using inheritance in JavaScript object-oriented programming is still not finished. If you don't get it, you will be dragged it next year.

. So I still have to write it. This year, this year does this year, there is a more important task next year! ~~ Continue to watch the "fennel" word of fennel beans.

The fourth way of writing of this "fennel"

Additional inheritance method, although it is my own, there are some shadows of the three inheritance laws, but this method is undeniable, you can say that the problem of inheriting the inheritance is CUT.

. Let's take a closer to say why it is so martial arts and wisdom?

The principle of additional inheritance method:

The key code of the additional inheritance method is its structure of the function arraylist04 ():

THIS

.base

=

New

CollectionBase ();

for

(

VAR

Key

in

THIS

.base) {

IF

(

!

THIS

[key]) {

THIS

[key]

=

THIS

.base [key];}}

Here is actually given

This additional base is not important, it will not affect our inheritance method. First we see the first sentence in the constructor, we immediately

New has an example of an example, which means that our inheritance is not required for the writing of the base class, with the front

The statement in the instance inheritance method is that as long as the script engine thinks the correct class can be. we know

Why is the constructive inheritance method? It is because it has never created an instance of a base class. and

Although the prototype inheritance method also created a base class instance, it assigns accumulated instances directly to the prototype property of the subclass, so that there is a special requirement for the child writing.

Then one

FOR

IN) Cycle, all attributes and methods with base classes to subclass examples

This is what I put this inheritance method is the reason for the attachment. This step is

The principle of constructing the inheritance method is quite similar, but the constructive inheritance method is used.

A trick of the THIS scope replacement, which makes the additional process to complete the function, but also gives a special requirement for the base class to the construction inheritance method, and its prototype characteristics cannot be used. Of course, the additional method is still there is no such requirement.

Additional inheritance UPDATE:

Object.prototype.extends

=

FUNCTION

BaseClass {

IF

(Arguments.length

> =

6

) {

Throw

New

Error ('Only Cansu Supprot At MOST

5

Parameters. ');

VAR

Base;

IF

(Arguments.length

>

1

) {

VAR

ARG01

=

Arguments [

1

];

VAR

ARG02

=

Arguments [

2

];

VAR

ARG03

=

Arguments [

3

];

VAR

ARG04

=

Arguments [

4

];

=

New

Baseclass (Arg01, Arg02, Arg03, Arg04);

Else

{Base

=

New

Baseclass ();

for

(

VAR

Key

in

Base) {

IF

(

!

THIS

[key]) {

THIS

[key]

=

Base [key];

IF

(

Typeof

(Base [key])

! =

'

FUNCTION

') {

Delete

Base [key];}}}

THIS

.base

=

Base;

//

Base.inherit = THIS;

}

In this way, we will write directly to:

FUNCTION

ArrayList04 () {

THIS

.Extends (CollectionBase);

//

...

}

At the same time, it also provides support for the base class, passing parameters to the base class, such as:

FUNCTION

ListItem () {

THIS

.Extends (ListiteMbase, TEXT, VALUE);

//

...

}

For the base class, will execute

New ListItembase (Text, Value); This action generates an instance of the base class.

Additional inheritance method of defects:

From now on, if you do not use Override technology to override the method, then in the new method to call the base class method (I will talk about this technology, because it does not affect us today discussion The problem of this inheritance mode). The additional method is basically no shortcomings, and it must be said that the words are: use one

FOR

IN) Cycling to the introduction of the base class, the syntax is very ugly :(

An example of an additional inheritance method:

Document.write ('Additional Inheritance Law:

<

Br

>

');

VAR

ArrayList41

=

New

ArrayList04 (); arraylist41.add ('a'); arraylist41.add ('b'); arraylist41.foo ();

VAR

ArrayList42

=

New

ArrayList04 (); arraylist42.add ('a'); arraylist42.add ('b'); arraylist42.add ('c'); arraylist42.foo ();

The example operation results are:

Additional inheritance: [Class ArrayList04]:

2

: A, b [class arraylist04]:

3

: A, b, c

Small knot: Additional inheritance method is the most unlike inheritance, but it is the most used in actual use (PS: this is our BOSS's title of good code). Its OVERRIDE is also very clear, as long as

This.Extends (BaseClass); After the statement, the method of the same name is imported into the subclass, automatically overrides the method imported from the base class to achieve Override effect.

Use scenario: Anywhere, Anytime, Anybody ...

This seems to be big.

The perfect thing must be no, the additional inheritance law is also defective, but this defect does not belong to inheriting this category, but a problem that occurs in the simulation of other OO programming characteristics, will then talk. Tang Yan: The light is inheritance and use,

Additional inheritance law is not any problem.

A variety of research on the inheritance of JScript simulation object-oriented programming is completed.

.

转载请注明原文地址:https://www.9cbs.com/read-57576.html

New Post(0)