// Prototype interface
class Animal {
constructor(name, species) {
this.name = name;
this.species = species;
}
clone() {
return null;
}
}
// Concrete prototypes
class Dog extends Animal {
constructor(name) {
super(name, "Dog");
}
clone() {
return new Dog(this.name);
}
}
class Cat extends Animal {
constructor(name) {
super(name, "Cat");
}
clone() {
return new Cat(this.name);
}
}
// Client
const originalDog = new Dog("Buddy");
const clonedDog = originalDog.clone();
console.log(originalDog); // Dog { name: 'Buddy', species: 'Dog' }
console.log(clonedDog); // Dog { name: 'Buddy', species: 'Dog' }
No comments:
Post a Comment