Why getters and setters?


#1

Why do we need getters and setters? I try removing get and set methods and outcome was the same. I do not get it. I neex explanation on simple, basic level.



class HospitalEmployee {
  constructor(name) {
    this._name = name;
    this._remainingVacationDays = 20;
  }
  
  get name() {
    return this._name;
  }
  
  get remainingVacationDays() {
    return this._remainingVacationDays;
  }
  
  takeVacationDays(daysOff) {
    this._remainingVacationDays -= daysOff;
  }
}

class Nurse extends HospitalEmployee {
 constructor(name, certifications) {
   super(name);
   this._certifications = certifications;
 } 
}

const nurseOlynyk = new Nurse('Olynyk', ['Trauma','Pediatrics']);
nurseOlynyk.takeVacationDays(5);
console.log(nurseOlynyk)
console.log(nurseOlynyk.remainingVacationDays);



#2

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.