Javascript play-and-play inheritance (2)

Source: Internet
Author: User

In 《JavascriptPlay-and-play inheritance(I)", I mainly introduced object disguise to implement inheritance. Here we will first discuss the advantages and disadvantages of this method.

Undoubtedly, this method is easy to understand and calls the constructor of the parent class in the subclass. In addition, the biggest advantage of this method is that constructor inheritance can implement multiple inheritance. Review thisCode:

Function ()

{}

Function B ()

{}

Function C ()

{

This. Father =;

This. Father ();

Delete this. Father;

This. Father = B;

This. Father ();

Delete this. Father;

}

However, this method also has the following Disadvantages:

If you are familiar with object-oriented, let's look at this section of C # code:


Class
Program

{


Static
VoidMain (String[] ARGs)

{


BB =New
B();


BoolTemp = (Typeof(A). Isinstanceoftype (B );


Console. Writeline (temp );

}

}


Class
A

{


PublicA ()

{

 

}

}


Class
B:A

{


PublicB ()

{

 

}

}

What are the results? B is of course an instance of:

However, we did this test on the above section using the JavaScript code that constructs the inheritance:

Function ()

{}

Function B ()

{}

Function C ()

{

This. Father =;

This. Father ();

Delete this. Father;

This. Father = B;

This. Father ();

Delete this. Father;

}

VaR c = new C ();

Alert (C instanceof );

However, the result is not what we think:

In fact, it is easy to explain that constructing inheritance only copies the attributes of the parent class by calling the constructor of the parent class, therefore, many documents do not refer to this inheritance method as inheritance.

When you see the disadvantages, remember the advantages: support for multi-inheritance.

Let's look at the inheritance of C # And find that there are two most typical differences with this inheritance: C # does not support multi-inheritance and the disadvantages of constructor inheritance I mentioned above. As a result, a new Inheritance Method is created, and we become prototype inheritance.

The name can be roughly understood. Prototype inheritance uses prototype features to implement inheritance. This is one of the most popular inheritance methods in JavaScript. If you do not understand the prototype, follow my other articleArticle: Prototype.

Let's take a look at the code. Here, I refer to a piece of code in "Return of the Javascript King:

Function Point (dimension)

{

This. Dimension = dimension;

This. test = function (){

Alert ("success ");

}

}

Function point2d (x, y)

{

This. x = X;

This. Y = y;

}

Point2d. Prototype = new point (2 );

VaR P = new point2d (3, 4 );

P. Test ();

Test passed. It indicates that point2d has inherited the method of the parent class and then looks at the instance.

Alert (P instanceof point );

Successful! Well, let's analyze prototype inheritance:

I will not talk about the advantages of prototype inheritance. The structure is simple, easy to understand, and the instance can be used. However, his shortcomings are equally significant. Do you still remember the previous example about animal, people, and girl? We use prototype inheritance to implement the following:

Function animal ()

 

{

 

This. Run = function () {alert ("I can run ");};

 

}

 

Function people (name)

 

{

 

This. Say = function () {alert ("My name is" + this. Name );}

 

}

 

People. Prototype = new animal ();

 

Function girl (name, age)

 

{

 

This. Age = age;

 

This. Introduce = function () {alert ("My name is" + this. Name + ". I am" + this. Age );};

 

}

 

Girl. Prototype = new people (???);

 

Everyone should pay attention to the line of code in the red and bold parts. People is the prototype of girl, so we should input the name parameter when initializing people, but the name of each girl is different, therefore, prototype inheritance does not use the combination of fields: In the prototype inheritance stage, you cannot determine which parameters are used to initialize the parent class object. Scenario 2: Very simple. Each class can have only one prototype. That is to say, prototype inheritance cannot be used for multi-inheritance. This is a good thing and a bad thing. Because:

    1. In object-oriented languages such as Java and C #, multi-inheritance is not supported, and it is considered that multi-inheritance is not in line with object-oriented
    2. Multiple Interfaces cannot be implemented!

 

Now, I will write this article today. In summary, prototype inheritance solves some problems of constructor inheritance and introduces some new problems. In general, prototype inheritance is the most widely used method of inheritance, and it is also the method of realizing inheritance in Javascript grammar!

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.