Use inheritance in Javascript Object-Oriented Programming (3)

Source: Internet
Author: User

Last time I talked about some advantages and disadvantages of using constructor to implement class inheritance in object-oriented programming using JavaScript. Next, let's talk about the disadvantages of the prototype inheritance law. I hope you can give some suggestions and discuss some of these questions.

Prototype is a basis for JavaScript to implement object-oriented programming, but it is not the only method for constructing classes, we can write classes without using prototype (write all the additional attributes and methods in the constructor ). However, in addition to adding new attributes and methods to the object subclass, the prototype can also add prototype attributes and methods to internal objects in the script environment, for example, we usually add a trim method to the internal object string to delete spaces at both ends of the string,CodeIs:

String. Prototype. Trim =   Function ()
{
Return   This . Replace ( / ( ^ \ S * ) | (\ S * $) / G ,'');
}

In this way, we can use the trim method in any string instance and get used to this prototype system. Sometimes we still feel that the traditional OOP is not as good as Kaka. Let's get down to the truth and continue to talk about our prototype inheritance law.

Principles of prototype inheritance:

The key code of the prototype inheritance method is the first sentence in its constructor function arraylist02:

Arraylist02.prototype =   New Collectionbase ();
Arraylist02.prototype. constructor = arraylist02;

AE...Overwrite all prototypes into an instance of the base class. How can prototype be used for subclasses? Don't worry about it. Anyway, JavaScript Object instances can dynamically add and delete attributes and Methods. Isn't the base class instance as prototype exactly equal to extends's collectionbase? Then, use XXX. Prototype. xxx = function () to continue to obtain the object with the newly added attributes and methods.Note that arraylist02.prototype is initialized to the base class instance outside the constructor of arraylist02..

Let's take a look at the second sentence. Why should we assign arraylist02 to the constructor of the new prototype? If you do not assign this value, when we get from the arraylist02 instance ???. Prototype. constructor, but its constructor will obtain the collectionbase. This is not our intention, and it is the use of instanceof key comparison object instances and objects will also make errors.

Defects of prototype Inheritance Law:

The prototype inheritance method has two defects. The first one is that the prototype of the class is actually an object instance, it cannot be instantiated again (its initialization has been completed during Script Loading ). What does that mean? We know that when creating an object instance, we use the statement new arraylist02 (). At this time, we can regard it as a Javascript script engine that uses a shortest copy of prototype as this to return to the instance of the class (in this caseNoOnly useShortest copyIf the class does not have any prototype attributes and prototype Methods attached, a new object () instance is returned. This is where the problem occurs. Because New executes a shortest copy on prototype, if prototype has an object type attribute, this will cause shared object instances (similar to modifying attributes using static modifiers in traditional OOP class definitions ). The following is an example of this defect. To avoid this, do not define attributes of the object type in the base class, such as array, object, and date.
The second defect is similar to the defect of the "Construction Inheritance Method" mentioned last time. It is also about the statement sequence of subclass definition. That is to say, the Code: arraylist02.prototype = new collectionbase (); must be executed before all prototypes are defined. It is very easy to execute this statement after the prototype attributes and methods are imported, it is scheduled to overwrite all the previous imports :(. The solution is to write in the order I gave the article (1). Is it depressing? Kaka

Prototype Inheritance Method example:

< Script Language = "JavaScript" >
Document. Write ('original inheritance: < BR > ');
VaR Arraylist21 =   New Arraylist02 ();
Arraylist21.add ('A ');
Arraylist21.add ('B ');
Arraylist21.foo ();
VaR Arraylist22 =   New Arraylist02 ();
Arraylist22.add ('A ');
Arraylist22.add ('B ');
Arraylist22.add ('C ');
Arraylist22.foo ();
</ Script >

The running result is as follows:

Original inheritance law:
[Class arraylist02]: 2: a, B
[Class arraylist02]: 3: A, B, C, A, B

Have you found any problems? The Foo () of the Instance arraylist22 Outputs A, B, C, A, B @ _ @... this is the problem caused by the shortest copy of the prototype object mentioned above. But why is the set counter in arraylist22 still 3? This is because this. m_count is an integer type, which is also called the value type (the same as the value type and object type concept in C ). The value type does not have the problem of shallow copy and deep copy. It can be considered as a deep copy.

Summary: The prototype Inheritance Method of JavaScript uses many inheritance methods, although it has a bug that is so serious as a prototype shallow copy. Because we seldom define attributes in the base class, let alone the attributes of the object type, this error may not be very likely, but it is a potential risk :(. As for the second defect, it is also a potential bug. As long as you can clearly define the class, you will not make mistakes. 'Reload' is also relatively simple. If arraylist02.prototype = new collectionbase (); has the attributes or methods with the same name, it will automatically overwrite the attributes or methods in the base class, just as if they were overloaded.

Application Scenario: The base class has no attributes, at least the attributes of the object type. The advantage of this inheritance is that it maintains the integrity of the sub-class constructor. You can add any Code related to the inheritance in it. All the inheritance and overload operations are based on the 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.