JSFiddle - React, Tailwind, and code Playground
JavaScript
class Animal {
constructor(name, species, sound = null) {
this.name = name;
this.species = species;
this.sound = sound;
}
getName() {
return this.name;
}
speak() {
if (this.sound) {
return this.sound;
} else {
return "What sound does a giraffe make?"
}
}
}
class Dog extends Animal {
constructor(name) {
super(name, "Dog", "Woof")
}
fetch(){
return "Stick";
}
}
let cat = new Animal("Bob", "Cat", "Meow");
console.log(cat.getName() + " is a " + cat.species + ": " + cat.speak());
let dog = new Dog("Tara");
console.log(`${dog.getName()} is a ${dog.species}: ${dog.speak()}`);