In the previous chapter, we focused on the prototype inheritance relationship between classes in JavaScript: Prototype inheritance is handy for methods in inheriting classes. So let's take a look at the inheritance of class inheritance and the mixing of class inheritance and prototype inheritance, so-called class inheritance, is to use the call or Apply method to impersonate the inheritance:
1 functionDesk (size,height) {2 This. size=size;3 This. height=height;4 }5 functionMjdesk (size,height) {6Desk.call ( This, size,height);//This is called Class inheritance.7 }8 varMJ =NewMjdesk (10,123);
Like the above is the class inheritance we want to use, with this inheritance, we can access the methods and properties in the class, but cannot access the methods and properties in the parent prototype, this method alias pseudo-inheritance, as the name implies, is a false inheritance, so, the false certainly can not inherit the real prototype, so, The disadvantage of class inheritance is also obvious, when we use a lot of time, it will create a waste of memory. Thus, we have a hybrid approach to class inheritance and prototype inheritance:
1 functionDesk (size,height) {2 This. size=size;3 This. height=height;4 }5 functionMjdesk (size,height) {6Desk.call ( This, size,height);//This is called Class inheritance.7 }8Mjdesk.prototype=NewDesk ();//prototype Inheritance9 varMJ =NewMjdesk (12,12);Ten //Of course, the prototype inheritance here is better in the way that we use an empty function to inherit from the previous chapter.
Of course, what we're using most now is the combination of these two ways!
Object inheritance relationships in JavaScript (2)