6. Prototype pattern
by petrussola
JavaScript
class Car {
constructor(make, model, year) {
this.make = make;
this.model = model;
this.year = year;
}
drive () {
console.log(`I am a ${this.make} ${this.model} from ${this.year} and I drive`)
}
break() {
console.log('Time to break!')
}
}
const car1 = new Car('Toyota', 'Corolla', 1996)
const car2 = new Car('VW', 'ID5', 2022)
const car3 = new Car('Toyota', 'Prius', 2010)
car1.drive()
car2.drive()
car3.drive()
car1.break()
car2.break()
car3.break()