Use inheritance in JavaScript object-oriented programming (4)

xiaoxiao2021-03-06  38

Use inheritance in JavaScript object-oriented programming (4)

Everyone seems to be very interested in the inheritance of JavaSrip-oriented programming.

There is no discussion. Perhaps everyone can do not have such a complex script to develop Solution, but there is a problem in the future to discuss. After all, the classic tutorial is impossible to include these advanced applications, so I summarized things may have fallacy.

Today, the script is the 'instance inheritance method in object-oriented programming, this method is

In the Classical Forum, introducing the inheritance method used in the article-oriented object-oriented objects. How did it work?

The principle of instance inheritance method:

The key code of the instance inheritance method is its constructor

Function arraylist03 ():

VAR

Base

=

New

CollectionBase ();

//

...

Return

Base;

In fact, an instance of the base class is created in the subclass structure, and then the base class instance is returned as the return value. The so-called inheritance operation at this time is a dynamic read and write to the Object instance (base class is also an example of a derived class of Object). It has not been involved in any associated category, but ultimately The effect is still to make people feel that inheritance.

Example inheritance method of defects:

This inheritance method is still more clear, especially if you have understood the dynamic characteristics of the JavaScript object. However, this method is also coming to the request of class code. Since we created a base class instance in subclass constructors, there is no requirement for the writing of base classes, as long as it is a scripting engine It is possible to think the correct class. However, subclasses cannot be written casually. Since the properties and methods of subclasses are implemented by the dynamic characteristics of the object, the subclass cannot use the prototype attribute (prototype) to implement the import of attributes and methods, but must write in the inline. In the constructor of the subclass, this and

The limitation of the inheritance of the structure method is very similar, but the former is the writing of the base class cannot use the prototype attribute.

Example of instance inheritance method:

Document.write ('instance inheritance method:

<

Br

>

');

VAR

ArrayList31

=

New

ArrayList03 (); ArrayList31.Add ('a'); arraylist31.add ('b'); arraylist31.foo ();

VAR

ArrayList32

=

New

ArrayList323 (); arraylist32.add ('a'); arraylist32.add ('b'); arraylist32.add ('c'); arraylist32.foo ();

Example Run Result:

Instance inheritance: [Class ArrayList03]:

2

: A, b [Class ArrayList03]:

3

: A, b, c

Summary: The instance inheritance method actually steals the taste of the column, because the examples thus, for InstanceOf, is entirely an extension of its base class. The example of the subclass is thrown away because

NEW ARRAYLIST03 () Returns the base class instance (

Return

Base;). This has no other inheritance of the taste, called class extensions.

. The advantage is that there is no special requirements for the preparation of the base class, but it also requires the writing method of the subclass, the subclass cannot use prototype to import prototype method, and create a base class instance in subclass constructive function

VAR

Base

=

New

COLLECTIONBASE (); needs to begin at the beginning of the constructor (that is, any additional properties and methods to base).

Application scenario: There is no classic application scenario, but for the base class is more complicated, the subclasses need to be added with little inheritance, and the instance method is still very clear. Especially those who are very familiar with JScript objects are dynamically expanded. To Be Continue ...

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

New Post(0)