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);
        alert("Your Error is "+mes);    
    return instance;

Okay, let's test it out:

var e=new ExtendingError("Your number is less than one");

The results satisfied us:
< img Alt = "" border = 0 SRC =" / / ">

< img Alt = "" border = 0 SRC =" / / ">

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:

    for(var pro in obj)
        //This is a complete copy of the parent class's properties and methods

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)
        alert("My name is "+name);
function Girl(name,age)
    //Tectonic inheritance
    delete this.father;
        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);

< img Alt = "" border = 0 SRC =" / / ">

< img Alt = "" border = 0 SRC =" / / ">

< img Alt = "" border = 0 SRC =" / / ">

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: