Today we will take a look at the class inheritance in inheritance and the mixture of class inheritance and prototype inheritance. The so-called class inheritance is to use call or apply methods 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);The above is the class inheritance we want to use. With this kind of inheritance, we can access the methods and properties in the class, but we cannot access the methods and properties in the parent class prototype. This method alias is impersonating inheritance. As the name suggests, it is a fake inheritance. Therefore, the fake ones cannot inherit the real prototype. Therefore, the disadvantages of class inheritance are also very obvious. When we use more, it will cause memory waste. Therefore, we have a mixed use 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 using the method of using an empty function as mentioned in the previous chapter.Of course, the most commonly used method we use now is the way to mix the two!
The above is the object inheritance relationship in JavaScript introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!