First, let’s look at non-mainstream inheritance one: instance inheritance method.
I don’t say so much nonsense. Since it is inherited by non-mainstream, it must not be used frequently. Since it is not used frequently and still exists, there is only one factor that it is used in specific occasions. The instance inheritance method is mainly used for the inheritance of core objects, and is also the only way to solve the inheritance of core objects so far.
The inheritance of core objects has certain value. For example, the Error object. Our company may have to implement an Error class to simplify future development. At this time, I have to use the instance inheritance method to inherit the Error.
The code is as follows:
The code copy is as follows:
function ExtendingError(mes)
{
var instance=new Error(mes);
instance.NewError=function(){
alert("Your Error is "+mes);
}
return instance;
}
OK, test it:
The code copy is as follows:
var e=new ExtendingError("Your number is less than one");
e.NewError();
alert(e.toString());
The results satisfy us:
OK, without saying much nonsense, this is a non-mainstream inheritance method, which is basically only used for the inheritance of core objects, just remember it!
Next, let’s take a look at the second non-mainstream inheritance: copy inheritance method.
As the name suggests, copy inheritance means the inheritance of objects through copying. What should I copy? It is obvious that it is the properties and methods of the object. Do you still remember that in Javascript, the class is actually a Hashtable? If you can't remember, go back and review the basics. I may write an article about Javascript objects in a while.
It will be easier to understand this, just look at the code:
First write an Extend method:
The code copy is as follows:
Function.prototype.Extend=function(){
for(var pro in obj)
{
//This is actually a complete copy of the properties and methods of the parent class
this.prototype[pro]=obj[pro];
}
}
OK, write a piece of code to see how to use it:
The code copy is as follows:
function Animal()
{ }
function People()
{ }
People.Extend(new Animal())
{ }
A discerning person can see at a glance that the disadvantages of this method are too obvious:
When copying the attribute methods of an object one by one, reflection is actually used. I won’t say much about the damage to efficiency of reflection.
Like prototype inheritance, the parent object must be initialized. When the inheritance relationship is determined, but the parameters are still uncertain, it cannot be played!
In short, this method is not generally used.
Okay, let’s talk about something commonly used. Mixed inheritance!
This is based on two mainstream inheritance methods. Comparing the two inheritance methods, we can find that the advantages and disadvantages of the two inheritance methods are complementary, so it's easy to mix them together!
The code copy is as follows:
function People(name)
{
this.name=name;
this.SayName=function(){
alert("My name is "+name);
}
}
function Girl(name,age)
{
//Construction 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();
Okay, a mixture of the two methods, now let’s see if the problem has been solved?
var g=new Girl("Xuan",22);
alert(g instanceof People);
g.SayName();
g.Introduce();
Test passed!
This is a relatively perfect solution, but it increases the complexity of the code, so the specific solution depends on everyone's choice in practice.
This is all the ways to play with Javascript. Everyone is welcome to continue to follow my other articles.