Javascript plays with inheritance (3)

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

First, let's look at alternative inheritance: the method of instance inheritance.

I won't say that much nonsense, since the non-mainstream inheritance, it must not be used, since the non-mainstream still exist, there is only one factor, he used in a particular situation. Instance inheritance, which is mainly used for inheritance of core objects, is the only way to solve the inheritance of core objects.

The inheritance of core objects has certain value, for example, the Error object, our company may need to implement an Error class to simplify the future development, then I will use the instance inheritance method to inherit errors.

The code is as follows:


function ExtendingError(mes)
{
    var instance=new Error(mes);
    instance.NewError=function(){
        alert("Your Error is "+mes);    
    }
    return instance;
}

Okay, let's test it out:


var e=new ExtendingError("Your number is less than one");
e.NewError();
alert(e.toString());

The results satisfied us:
< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201405/2014050818081912.png ">

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

Well, without further ado, this is a non-mainstream inheritance method that is basically only used for inheritance of core objects, just remember!
Next up is alternative inheritance two: copy inheritance.

Copy inheritance, as the name implies, is the inheritance of an object by copying. Copy what? Obviously, the properties and methods of the object, remember in Javascript, the class is just a Hashtable? If I can't remember, I'll go back and review the basics, and I'll probably write about Javascript objects later.
This is easy to understand, directly look at the code:
Start by writing a Extend method:


Function.prototype.Extend=function(){
    for(var pro in obj)
    {
        //This is a complete copy of the parent class's properties and methods
        this.prototype[pro]=obj[pro];
}
}

Okay, let's write another piece of code and see how it works:


function Animal()
{    }
function People()
{    }
People.Extend(new Animal())
{    }

The disadvantages of this method are obvious to the discerning eye:
When you copy an object's property methods one by one, you actually use reflection, and I won't say much more about the effect of reflection on efficiency.
As with prototype inheritance, the parent object must be initialized, and when the inheritance relationship is determined, but the parameters are uncertain, the game is over!

Anyway, this method is not usually used.

Okay, here's a common thing. Hybrid inheritance!
This is based on two main inheritance approaches. Comparing the two inheritance methods, we can find that the advantages and disadvantages of the two inheritance methods are complementary.


function People(name)
{
    this.name=name;
    this.SayName=function(){
        alert("My name is "+name);
}
}
function Girl(name,age)
{
    //Tectonic inheritance
    this.father=People;
    this.father(name);
    delete this.father;
    this.Introduce=function(){
        alert("My name is "+name+".I am"+age);
}
}
//Prototype inheritance
Girl.prototype=new People();
 Ok, a mixture of the two, now let's see, is that the problem solved? 
var g=new Girl("Xuan",22);
alert(g instanceof People);
g.SayName();
g.Introduce();

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

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

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

Test passed!

This is a relatively perfect solution, but it increases the complexity of the code, so the specific solution is up to you to choose in practice.

That's all Javascript does with inheritance, and I welcome you to stay tuned for the rest of my article.  


Related articles: