First, we look at Non-mainstream succession One: instance inheritance method.
I do not say so much nonsense, since it is Non-mainstream inheritance, it must not be used, since not commonly used still exist, there is only one factor, he used for specific occasions. Instance inheritance, which is mainly used for the inheritance of core objects, is the only way to resolve core object inheritance so far.
The core object of inheritance has a certain value, such as the Error object, our company may have to implement an error class to simplify future development, then I will use the instance inheritance method to inherit the error.
The code is as follows:
Copy Code code as follows:
function Extendingerror (MES)
{
var instance=new Error (MES);
Instance. Newerror=function () {
Alert ("Your Error is" +mes);
}
return instance;
}
Okay, under test:
Copy Code code as follows:
var e=new extendingerror ("Your number is less than one");
E.newerror ();
Alert (e.tostring ());
The results satisfied US:
Good, nonsense not much to say, this is a non-mainstream way of inheritance, basically only for the core object of inheritance, remember!
Let's take a look at the Non-mainstream Succession II: Copy Inheritance method.
As the name implies, copy inheritance, is through the copy to achieve the inheritance of objects, copy what? Obviously, is the object's properties and methods, remember JavaScript, the class is actually a hashtable? If you can't remember, go back and review the basics, and I'll probably write a piece of knowledge about JavaScript objects over time.
Understand this is good to do, directly look at the code:
First write a extend method:
Copy Code code as follows:
Function.prototype.extend=function () {
For (Var pro in obj)
{
This is actually copying the properties and methods of the parent class completely.
This.prototype[pro]=obj[pro];
}
}
OK, then write a snippet of code to see how to use:
Copy Code code as follows:
function Animal ()
{ }
function people ()
{ }
People.extend (New Animal ())
{ }
The shortcomings of this method are too obvious at the sight of a discerning eye:
When you copy the object's property method one by one, it is useful for reflection, reflection on efficiency damage I don't have to say more.
As with prototype inheritance, you must initialize the parent class object, when the inheritance relationship is determined, but when the parameters are uncertain, do not go!
In short, this method is generally not necessary.
Well, here's a common thing to say. Mixed inheritance!
This is based on two main ways of inheriting. Comparing two inheritance methods, we can find that the advantages and disadvantages of two inheritance methods are complementary, so it is good to do, mix together!
Copy Code code as follows:
function people (name)
{
This.name=name;
This. Sayname=function () {
Alert ("My name is" +name);
}
}
function Girl (name,age)
{
Construct 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 ();
Well, two ways of mixing, now look, is not the problem solved?
var g=new Girl ("Xuan", 22);
Alert (g instanceof people);
G.sayname ();
G.introduce ();
Test pass!
This is a relatively perfect solution, but it increases the complexity of the code, so the specific solution depends on the choice of practice.
JavaScript to play the way of inheritance on these, you are welcome to continue to pay attention to my other articles.