Home  >  Article  >  Web Front-end  >  Javascript playing with inheritance (2)_javascript skills

Javascript playing with inheritance (2)_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:48:59977browse

Needless to say, this method is easier to understand, calling the constructor of the parent class in the subclass. In addition, one of the biggest advantages of this method is that structural inheritance can achieve multiple inheritance. Review this code:

Copy code The code 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 shortcomings of this and that:
For those who are familiar with object-oriented, let’s take a look at this piece of C# code:

Copy code The code 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 instance of A:

However, let’s do this test on the above Javascript code that uses structural inheritance:

Copy code The code 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 result was not what we imagined:

In fact, it is easy to explain: structural inheritance only copies the attributes of the parent class by calling the constructor method of the parent class, and does not do anything else, so many materials do not call this inheritance method inheritance.

While seeing the disadvantages, also remember the advantages: supports multiple inheritance.

Let’s look at inheritance in C# and find that there are two most typical differences with this inheritance: C# does not support multiple inheritance, and the shortcomings of structural inheritance I mentioned above. So a new inheritance method emerged, which we called prototypal inheritance.

Seeing the name, you can roughly understand that prototypal inheritance uses the characteristics of prototype to achieve inheritance. This is the most popular inheritance method in Javascript. If you don’t understand prototypes, please follow my other article: "Playing with Prototypes - Prototype".
Let’s look at the code first. Here, I borrow a piece of code from "The Return of the Javascript King":

Copy code The 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 shows that Point2D has inherited the methods of the parent class. Let’s look at instance again.

alert(p instanceof Point);

Success! Okay, let’s analyze prototypal inheritance:

I won’t go into details about the advantages of prototypal inheritance. The structure is simple, easy to understand, and it can be instanced. But his shortcomings are equally obvious. Do you still remember the example of Animal, People, Girl in my last article? We use prototypal inheritance to achieve this:

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

Please pay attention to the line of code in my red bold part. People is the prototype of Girl, so we should pass in the name parameter when initializing People, but the name of each Girl is different, so prototype inheritance Occasion 1 when not used: In the prototype inheritance stage, you cannot determine what parameters to use to initialize the parent class object. Occasion 2: Very simple, each class can only have one prototype, which means that prototypal inheritance cannot be used for multiple inheritance. This is a good thing and a bad thing. Because:
Object-oriented languages ​​such as Java and C# do not originally support multiple inheritance, and multiple inheritance is considered to be inconsistent with object-oriented

Cannot implement multiple interfaces!

Okay, that’s all for today. To summarize, Prototype inheritance solves some problems of structural inheritance and introduces some new problems. Generally speaking, prototypal inheritance is the most widely used inheritance method, and it is also the true way to implement inheritance in Javascript grammar!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn