The notebook demo grouped state and a method on one object. Now make a car object that can change its own speed. Run script.js and inspect the Console.
Object-oriented thinking starts with objects that hold both data and behavior. A car that knows its own speed is a simple way to introduce that pattern.
const thermostat = {
temperature: 20,
raise(degrees) {
this.temperature += degrees;
}
};
thermostat.raise(2);
console.log(thermostat.temperature);
car a brand of Roadster and a speed of 80. The starter logs its brand.accelerate(amount) method that increases this.speed. The starter calls it with 20 and logs the updated speed.Roadster | 100 on its own Console line.Store both data and a method inside the car object.
Inside accelerate, update this.speed by adding the amount.