Javascript plays with inheritance (ii)

  • 2020-03-30 02:52:40
  • OfStack

Needless to say, this approach is easy to understand, calling the constructor of the superclass in a subclass. In addition, the biggest advantage of this method is that the construction of inheritance can achieve multiple inheritance, review the code:


function A()
{    }
function B()
{    }
function C()
{
    this.father=A;
    this.father();
    delete this.father;
    this.father=B;
    this.father();
    delete this.father;
}

But this approach has its drawbacks:
For those familiar with object orientation, here's a piece of C# code:


classProgram
{
staticvoid Main(string[] args)
{
B b=newB();
bool temp = (typeof(A)).IsInstanceOfType(b);
Console.WriteLine(temp);
}
}
classA
{
public A()
{ 

}
}
classB : A
{
public B()
{ 

}
}

The result? B is of course an example of A:

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201405/201405081801068.png ">

However, here's how we tested the above piece of Javascript code using construction inheritance:


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 is not what we thought:

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201405/201405081801069.png ">


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();

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201405/2014050818010610.png ">

Test passed. To illustrate that Point2D has inherited methods from its parent class, consider instance.

Alert (p instanceof Point);

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201405/2014050818010611.png ">

Success! Ok, let's analyze prototype inheritance:

I won't go into details about the advantages of prototype inheritance. It's simple, easy to understand, and can instance. However, his shortcomings are also obvious. Do you still remember my last 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(???);

Unable to implement multiple interfaces!

Okay, so that's it for today. In summary, the Prototype inheritance solves some of the problems of constructing inheritance and introduces some new ones. In general, prototype inheritance is the most widely used way of inheritance, and it is also the way that inheritance is really implemented in Javascript grammar!


Related articles: