Without class, make a Dog constructor inherit from Animal using prototypes.
Animal(name) stores name and has a shared speak() method that returns "<name> makes a sound".
Dog(name, breed) reuses Animal's constructor to set name, stores breed, and overrides speak() to return "<name> barks". It also has describe(), which calls Animal's version of speak and adds the breed: "Rex makes a sound (beagle)".
A dog must be instanceof Animal, its constructor must be Dog, and nothing you do for Dog may change how other animals behave.
function Animal(name) {
// your code
}
function Dog(name, breed) {
// your code
}
// const rex = new Dog("Rex", "beagle");
// rex.speak(); // "Rex barks"
// rex.describe(); // "Rex makes a sound (beagle)"
// rex instanceof Animal; // true
The answer
Write it yourself first. Then compare it with a reference solution and the checks it passes.
Checking your sign-in…