class Animal {
#sound;
constructor(name, sound) {
this.name = name;
this.#sound = sound;
}
speak() {
return `${this.name} says ${this.#sound}`;
}
static create(name, sound) {
return new this(name, sound);
}
}
class Dog extends Animal {
#tricks = [];
constructor(name) {
super(name, "woof");
}
learn(trick) {
this.#tricks.push(trick);
return this;
}
perform() {
return `${this.name} performs: ${this.#tricks.join(", ")}`;
}
}
const rex = Dog.create("Rex", "bark");
rex.learn("sit").learn("shake").learn("roll over");
console.log(rex.speak());
console.log(rex.perform());
console.log(rex instanceof Animal);
console.log(rex instanceof Dog); Post #3156
2.22K
CHALLENGE