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.html
that loads and runs amain
function frommain.js
.
- Create a
main.js
file that defines a new class namedRocket
. Add a constructor that takes a constructor argumentname
and assigns it to an instance property. Then, define a simpleprint
method:
// main.js class Rocket { constructor(name) { this.name = name; } print() { console.log(this.name + ' is...