Calling super methods
Overriding methods is awesome for extending behavior. However, we sometimes want to continue to use behavior from a parent class. This is possible by using the super keyboard to access parent class methods.
In this recipe, we'll see how to use this keyword to access those methods.
Getting ready
This recipe assumes you already have a workspace that allows you to create and run ES modules in your browser. If you don't, please see the first two chapters.
How to do it...
- Open your command line application and navigate to your workspace.
- Create a new folder named
08-05-getters-read-only. - Copy or create an
index.htmlthat loads and runs amainfunction frommain.js.
- Create a
main.jsfile that defines a new class namedRocket. Add a constructor that takes a constructor argumentnameand assigns it to an instance property. Then, define a simpleprintmethod:
// main.js
class Rocket {
constructor(name) {
this.name = name;
}
print() {
console.log(this.name + ' is...