Use inheritance in JavaScript object-oriented programming (3)

xiaoxiao2021-03-06  38

Use inheritance in JavaScript object-oriented programming (3)

Last talked about the use of JavaScript for object-oriented programming, use a construction method to implement some advantages and disadvantages of the class inheritance. Below we will follow the shortcomings of the 'prototype inheritance law', I hope everyone can actively advise and explore some of the problems. Prototype is a basis for the JavaScript implementation of object-oriented programming, but it is not the only way to construct classes, we can fully implement classes without using Prototype (add all the properties and methods to write in the constructor. . However, in addition to adding new properties and methods to Object's subclasses, it can also continue to add prototype properties and methods for internal objects in the script environment, such as our most commonly used TRIM methods to add a TRIM method to internal object string. Spaces on both ends of the string, the code is:

String.Prototype.Trim

=

FUNCTION

() {

Return

THIS

.replace

/

(

^

/ s

*

)

|

(/ s

*

$)

/

g, '');

In this way we can use the Trim method in any String instance. It is used to this prototype system. Sometimes it still feels the traditional OOP without it, kaka. The words retired and continued to say our prototype inheritance. Principle inheritance method: The key code of the prototype inheritance method is the first sentence under the functional arraylist02 ():

ArrayList02.Prototype

=

New

CollectionBase ();

AE ... all the prototype covers an example of the base class, how do you use prototype? Here, don't worry, anyway, JavaScript's object instance can be dynamically increasing and delete attributes and methods, and the base class instance is not just equal to EXTENDS for PROTOTYPE? Then use XXX.Prototype.xxx = function (), and you can continue to get an object that has added properties and methods. Note ArrayList02.Prototype is an instance of an example to be based on the constructor of ArrayList02. The deficiencies of the prototype inheritance: the prototype inheritance method has two flaws, the first is due to the prototype "actually an instance of an object, it cannot be instantiated again (its initialization has been executed when the script is loaded ). What do you mean? We know that we use statement new arraylist02 (), then we can see a shallow copy of the JavaScript script engine as this returned to the class (here, there is no copy, just use shallow copy this Concept to help understand), if the class does not attach any prototype attributes and prototype methods, it is equal to returning a new Object () instance. The problem is here, because the new copy of the prototype is a shallow copy, if there is an object type attribute in prototype's prototype properties, then the problem of shared object instances (similar to using the traditional OOP class definition) The Static modifier is modified). This defect will have an example demonstration, and the approval is not to define the properties of the object type in the base class, such as Array, Object, and Date. The second defect and the defects of the last "Construct Inheritance Law" are similar, and the subclass definition is the order of statement. That is, code: arraylist02.prototype = new collectionBase (); must be executed before all prototype definitions, very simple, if the prototype attribute and method is imported, then execute this statement, it is scheduled to cover the previous imports all overwhelmed The solution is to write, depressed, depressed? Kaka prototype inheritance method: example:

Script

Language

= "JavaScript"

>

Document.write ('original inheritance:
); var arraylist21 = new arrayList02 (); arraylist21.add (' a '); arraylist21.add (' b '); arraylist21.foo (); var arraylist22 = New arraylist02 (); arraylist22.add ('a'); arraylist2.add ('b'); arraylist2.add ('c'); arraylist22.foo ();

Script

>

The example operation results are:

Original inheritance: [Class ArrayList02]: 2: A, B [Class ArrayList02]: 3: A, B, C, A, B

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

New Post(0)