Полиморфизм
class Animal {
speak() {
return "Some sound";
}
}
Animal.prototype.greet = function () {
return "Не ожидал, что я разговариваю?!";
};
let a = new Animal();
console.log(a.greet());
class Dog extends Animal {
speak() {
return "Bark";
}
}
class Cat extends Animal {
speak() {
return "Meow";
}
}
function makeSound(animal) {
return animal.speak();
}
const dog = new Dog();
const cat = new Cat();
console.log(makeSound(dog)); // Bark
console.log(makeSound(cat)); // Meow