The super keyword allows us to call methods on an object's parent. It's often used to call a parent's constructor method when the child object has its own.
class Titan {
constructor(name) {
this.name = name;
}
toString() {
return `Titan - Name: ${this.name}`;
}
}
class BeastTitan extends Titan {
constructor(name, power) {
// call the parent's constructor
super(name);
this.power = power;
}
toString() {
// call the parent's `toString` method
return `${super.toString()}, Power: ${this.power}`;
}
}
const beast = new BeastTitan("Zeke", 9000);
console.log(beast.toString());
// Titan - Name: Zeke, Power: 9000
The Sender class' sendMessage method now handles the full message-sending process.
They've decided that Sender subclasses should only be responsible for formatting the message. Both SMSSender and EmailSender should have a formatMessage method that calls the base Sender's formatMessage method, but appends either " [SMS]" or " [Email]" to the end of the message, depending on the subclass. For example:
To: [email protected], Message: You up? [SMS]