function Vehicle(make, model, year) {
this.make = make;
this.model = model;
this.year = year;
this.speed = 0;
}
Vehicle.prototype.accelerate = function (amount) {
this.speed += amount;
return this;
};
Vehicle.prototype.describe = function () {
return `${this.year} ${this.make} ${this.model} going ${this.speed}km/h`;
};
function ElectricVehicle(make, model, year, range) {
Vehicle.call(this, make, model, year);
this.range = range;
}
ElectricVehicle.prototype = Object.create(Vehicle.prototype);
ElectricVehicle.prototype.constructor = ElectricVehicle;
ElectricVehicle.prototype.describe = function () {
return Vehicle.prototype.describe.call(this) + ` | Range: ${this.range}km`;
};
const car = new ElectricVehicle("Tesla", "Model 3", 2023, 500);
car.accelerate(60).accelerate(40);
console.log(car.describe());
console.log(car instanceof ElectricVehicle);
console.log(car instanceof Vehicle);
console.log(car.constructor === ElectricVehicle); Post #3117
2.97K
CHALLENGE
- ❤ 2