课程: JavaScript: Five Advanced Challenges and Concepts
免费学习该课程!
今天就开通帐号,24,700 门业界名师课程任您挑!
Extending a class with new capabilities - JavaScript教程
课程: JavaScript: Five Advanced Challenges and Concepts
Extending a class with new capabilities
- [Instructor] A cool feature of classes is we can extend methods within the classes as well by overriding the methods. In the previous example, we captured unique properties for the Car and Bike classes, but the output from the getDescription method for the Vehicle class doesn't reflect this data. We get My Car has 4 wheels, but not My Car has 4 wheels and 4 doors. We get My Bike has 2 wheels, but not My Bike has 2 wheels and is an offroad type bike. So let's extend the functionality of our subclasses to output this information as well. To do that, I'll go into the Car class first and I'll say getDescription. So that's the same method as the one we defined up here, meaning I'm now overriding this method. But because we are in a subclass, an extended class, I can say getDescription and then return, and then use my template literal to say super, meaning go up into the parent, getDescription, that gives us the full output…