Back to questions
JuniorTheoryCommon
What does the program print?
class Animal {
speak() {
return "Hi";
}
}
class Dog extends Animal {
speak() {
return super.speak() + " Woof";
}
}
const d = new Dog();
console.log(d.speak());Hi Woof
- Expecting
Hi: a method inDogoverrides the one inAnimal, sod.speak()runs theDogversion, which adds" Woof"to the parent's result. - Expecting
super.speak()to call thespeakofDogagain:superlooks in the parent class, so it runs thespeakofAnimal, which returns"Hi".
- What does the program print if
Dogreturns"Woof"without callingsuper.speak()? - What does
d instanceof Animalevaluate to, and why?