Home>

Let's take a look at class inheritance in inheritance and the mix of class inheritance and prototype inheritance today.The so-called class inheritance,It is using the call or apply method to impersonate inheritance:

function desk (size, height) {
this.size=size;
this.height=height;
}
function mjdesk (size, height) {
desk.call (this, size, height);//This is called class inheritance.
}
var mj=new mjdesk (10,123);

Like the above, this is the class inheritance we want to use.With this inheritance,We can access methods and properties in the class,But can't access methods and properties in the parent class prototype,This method aliases inheritance,As the name suggests,Is a false inheritance,So, of course, the fake can't inherit the real prototype,Therefore, the disadvantages of class inheritance are also obvious.When we use more,Will cause waste of memory.From this, we have a mix of class inheritance and prototype inheritance:

function desk (size, height) {
this.size=size;
this.height=height;
}
function mjdesk (size, height) {
desk.call (this, size, height);//This is called class inheritance.
}
mjdesk.prototype=new desk ();//Prototype inheritance
var mj=new mjdesk (12,12);
//Of course, the prototype inheritance here is better than the way we used an empty function to inherit in the previous chapter.

Of course, what we use the most now is this way of mixing the two!

  • Previous Ajax implements intelligent prompt search function
  • Next Summary of commonly used annotations for spring boot