Create a JavaScript class called "Vehicle" with the following properties and methods: 1.…
- User
Create a JavaScript class called "Vehicle" with the following properties and methods:
- brand: a string that represents the brand of the vehicle.
- model: a string that represents the model of the vehicle.
- year: an integer that represents the manufacturing year of the vehicle.
- getAge(): a method that returns the age of the vehicle in years, calculated based on the current year and the manufacturing year.
- startEngine(): a method that simulates starting the engine of the vehicle.
- stopEngine(): a method that simulates stopping the engine of the vehicle.
Note: The getAge() method should use the Date object to get the current year.
Please provide the implementation of the Vehicle class.
- Assistant
class Vehicle { constructor(brand, model, year) { this.brand = brand; this.model = model; this.year = year; }
getAge() { const currentYear = new Date().getFullYear(); return currentYear - this.year; }
startEngine() { console.log(
The engine of the ${this.brand} ${this.model} is starting.); }stopEngine() { console.log(
The engine of the ${this.brand} ${this.model} is stopping.); } }// Example usage const myCar = new Vehicle("Toyota", "Camry", 2010); console.log(myCar.getAge()); // Output: 11 (assuming the current year is 2021) myCar.startEngine(); // Output: The engine of the Toyota Camry is starting. myCar.stopEngine(); // Output: The engine of the Toyota Camry is stopping.