Needless to say, this method is relatively easy to understand, calling the constructor of the parent class in the subclass. In addition, one of the biggest advantages of this method is that constructing inheritance can achieve multiple inheritance. Review this code:
The code copy is 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 this method also has this and that disadvantage:
If you are familiar with object-oriented, let’s look at this C# code:
The code copy is 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 is of course an example of A:
However, we do this test on the above section of Javascript code inherited using constructs:
The code copy is 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 are not what we imagined:
In fact, it is easy to explain: constructed inheritance is just a property of the parent class copied by calling the construction method of the parent class. No other searches are done, so many materials do not call this inheritance method inheritance.
While seeing the shortcomings, remember the advantages: support more inheritance.
When we look at the inheritance of C#, we found that there are two most typical differences from this inheritance: C# does not support multiple inheritance, and the disadvantages of constructive inheritance I mentioned above. So a new inheritance method was created, and we became prototype inheritance.
When you see the name, you can roughly understand that prototype inheritance is to use the characteristics of prototypes to achieve inheritance. This is the most popular way of inheritance in Javascript. If you don’t understand the prototype, please pay attention to another article of mine: "Playing with the prototype - prototype".
Let’s look at the code first. Here, I will draw on a piece of code from "The Return of the King of Javascript":
The code copy is as follows:
function Point(dimensional)
{
this.dimensional=dimensional;
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();
The test passed. This shows that Point2D has inherited the parent class method, and then look at the instance.
alert(p instanceof Point);
success! OK, let’s analyze prototype inheritance:
I won’t talk about the advantages of prototype inheritance. The structure is simple, easy to understand, and it can be instanced. But his shortcomings are also significant. Do you remember my previous example about Animal, People, and Girl? We use prototype inheritance to implement the following:
The code copy 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(???);
Please pay attention to the line of code in my red bold part. People are the prototype of Girl. So when we initialize People, we should pass in the name parameter, but the name of each Girl is different, so there is no use of prototype inheritance: in the prototype inheritance stage, you cannot determine which parameters to use to initialize the parent class object. Occasion 2: It's very simple. Each class can only have 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 believed to be incompatible with object-oriented
Multiple interfaces cannot be implemented!
Okay, that's all I have written about today. To summarize, Prototype inheritance solves some problems of construct inheritance and introduces some new problems. Overall, prototype inheritance is the most widely used inheritance method, and it is also the way to truly implement inheritance in Javascript grammar!