Use inheritance in Javascript Object-Oriented Programming (4)

Source: Internet
Author: User
It seems that you are not very interested in the inheritance of object-oriented programming in javasript. There is no discussion at all. It may be that you have not encountered such a complicated Script Development Solution for the moment, but you are welcome to discuss some issues later. After all, the classic tutorial cannot include these advanced applications, so the summary may be incorrect.

Today, let's talk about the 'instance inheritance method' in script object-oriented programming. This method is introduced in the classic forum to introduce JScript object-oriented programming.Article. How does it work?

Principles of the instance inheritance law:

Key to the instance inheritance lawCodeIn its constructor function arraylist03:

  VaR Base =   New Collectionbase ();
  // ...
  Return Base;

In fact, an instance of the base class is created during subclass construction, and then the base class instance is returned as the return value. At this time, the so-called inheritance operation is to dynamically read and write the object instance (the base class is an instance of the object's derived class), add attributes and methods for it, etc, there is no scope related to inheritance at all, but the final effect is that inheritance is realized.

Defects of the instance inheritance law:

This Inheritance Method looks clear, especially if you have understood the dynamic characteristics of JavaScript objects. However, the biggest drawback of this method is the requirement for writing class code. Because we have created a base class instance in the subclass constructor, therefore, there is no requirement for writing the base class, as long as it is a class that the script engine deems correct. However, the subclass cannot be written at will. Because the attributes and methods of the subclass are implemented through the dynamic characteristics of the object, the subclass cannot use prototype to import attributes and methods, however, it must be written in the constructor of the subclass using the inline method. This is similar to the constructor's restriction on inheritance. However, the prototype attribute cannot be used for writing a base class.

Example of the instance inheritance law:

Document. Write ('instance Inheritance Method: < BR > ');
  VaR Arraylist31 =   New Arraylist03 ();
Arraylist31.add ('A ');
Arraylist31.add ('B ');
Arraylist31.foo ();
  VaR Arraylist32 =   New Arraylist03 ();
Arraylist32.add ('A ');
Arraylist32.add ('B ');
Arraylist32.add ('C ');
Arraylist32.foo ();

Sample running result:

Instance inheritance law:
[Class arraylist03]: 2 : A, B
[Class arraylist03]: 3 : A, B, C

Summary: The instance inheritance method is actually a bit of a leader, because the Instance obtained in this way, for instanceof, is completely an extension of its base class. The subclass instance is thrown away because new arraylist03 () returns the base class instance (ReturnBase ;). This does not have any inheritance taste, and class extension is more appropriate. The advantage is that there are no special requirements for writing the base class, but you also need to specify the Writing Method of the subclass. The subclass cannot use prototype to import the prototype method and create a base class instance in the subclass constructor.VaRBase= NewCollectionbase (); must begin with the constructor (that is, before any attribute or method is added to the base ).

Application Scenario: there are no classic application scenarios, but the basic classes are complicated, and the attribute methods to be added to subclass are rarely inherited, so the instance method is quite clear. Especially those who are familiar with dynamic expansion of JScript objects are more explicit.

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.