JavaScript play Inheritance (ii) _javascript tips

Source: Internet
Author: User

There is no doubt that this approach is relatively easy to understand, calling the constructor of the parent class in subclasses. In addition, the biggest advantage of this method is that the construction of inheritance can achieve multiple inheritance, review this code:

Copy Code code as follows:

function A ()
{    }
function B ()
{    }
function C ()
{
This.father=a;
This.father ();
Delete This.father;
This.father=b;
This.father ();
Delete This.father;
}

But there are drawbacks to this approach:
Familiar with object-oriented we look at such a section of C # code:

Copy Code code as follows:

Classprogram
{
Staticvoid Main (string[] args)
{
B B=NEWB ();
BOOL temp = (typeof (A)). IsInstanceOfType (b);
Console.WriteLine (temp);
}
}
ClassA
{
Public A ()
{

}
}
Classb:a
{
Public B ()
{

}
}

What's the result? B Of course is an example of a:

However, we do this test on the above section using the constructed JavaScript code for inheritance:

Copy Code code as follows:

function A ()
{    }
function B ()
{    }
function C ()
{
This.father=a;
This.father ();
Delete This.father;
This.father=b;
This.father ();
Delete This.father;
}
var c=new c ();
Alert (c instanceof A);

But the results were not what we had imagined:

In fact, it is easy to explain: the construction of inheritance is only through the constructor of the parent class to replicate the properties of the parent class, the other search did not do, so many data does not refer to this inheritance as inheritance.

See the disadvantages while also remembering the advantages: support multiple inheritance.

We look at the inheritance of C # and find that there are two of the most typical differences from this inheritance: C # does not support multiple inheritance, and the disadvantages of the structural inheritance I mentioned above. So we have a new way of inheriting, and we become archetypal inheritance.

By looking at the name, you can roughly understand that prototype inheritance uses the attributes of the stereotype (prototype) to implement inheritance. This is one of the most popular forms of inheritance in JavaScript. If you do not understand the prototype, please pay attention to my another article: "Play the prototype--prototype."
Let's look at the code first, and here I draw on a piece of code from "The Return of the JavaScript King":

Copy Code code 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. Explain that POINT2D has inherited the method of the parent class, and then look at the instance.

Alert (P instanceof point);

Success! Okay, let's analyze the prototype inheritance:

The advantages of prototype inheritance I don't say much more, the structure is simple, easy to understand, and can instance. But his flaws are equally significant, remember my last example of Animal,people,girl? We use the prototype inheritance to achieve the following:

Copy Code code 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 (???);

Attention to my red bold section of the line of code, people is girl prototype, then we initialize the people should be passed in the name parameter, but each girl name is not the same, So the prototype inherits no use of the occasion one: in the prototype inheritance phase you cannot determine what parameters are used to initialize the parent class object. Occasion two: It is very simple that each class can have only one prototype, so that is, prototype inheritance cannot be used for multiple inheritance. This is a good thing and a bad thing. Because:
In object-oriented languages such as Java and C #, multiple inheritance is not supported, and multiple inheritance is considered to be incompatible with object-oriented

Cannot implement multiple interfaces!

Well, today is written here, in conclusion, prototype inheritance solves some problems of structural inheritance, and introduces some new problems. In general, prototype inheritance is the most widely used inheritance method, and it is also the way to implement 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.