getters&setters

Getters and setters are special methods used to access and modify object properties with custom behavior. Getters are used to retrieve the value of a property, and setters are used to assign a value to a property. They allow you to control how properties are accessed and modified, enabling data validation, computations, or side effects. Getters and setters are particularly useful for maintaining data integrity and encapsulation in objects.


class Car {
  constructor(power, gas) {
    this._power = power;
    this._gas = gas;
  }
  //when Car.power is accessed 'get' binds it to the following function:
  get power() {
    return `${this._power}hp`;
  }
  //when Car.gas is accessed 'get' binds it to the following function:
  get gas() {
    return `${this._gas}L (${(this._gas / 50) * 100}% GAS)`;
  }
  //when Car.value is changed 'set' binds it to the following function:
  set gas(value) {
    if (value > 50) {
      value = 50;
    } else if (value < 0) {
      value = 0;
    }
    this._gas = value;
  }
}

let car1 = new Car(200, 10);
car1.gas = 2;
console.log(car1.power);
console.log(car1.gas);