Javascript play-and-play inheritance (2)

Source: Internet
Author: User

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 this Code:

Copy codeThe Code is as follows:
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:

Copy codeThe Code is as follows:
ClassProgram
{
Staticvoid Main (string [] args)
{
B B = newB ();
Bool temp = (typeof (A). IsInstanceOfType (B );
Console. WriteLine (temp );
}
}
ClassA
{
Public ()
{

}
}
ClassB:
{
Public B ()
{

}
}

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:

Copy codeThe Code is as follows:
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 another article I wrote: Play prototype.
Let's take a look at the code. Here, I refer to a piece of code in "Return of the Javascript King:

Copy codeThe Code is as follows:
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:

Copy codeThe Code is as follows:
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:
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

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!

Related Article

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.