Overriding parent class instance methods
Ideally, classes contain more than just properties. Well-designed classes also define behavior. Thus, sub-classes should also be extending behavior, not just adding additional properties.
In this recipe, we'll see how to override methods from a parent class.
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-03-defining-methods
. - 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 a Rocket...