Why does logging a getter method call to the console print an error?

I am not that experienced with ES6+ and as I am learning React, I have now gotten into the “Learn Intermediate JavaScript” tutorial on Classes. When I call a getter method from my Surgeon Class instance, surgeonRomero here, the exercise accepts my code but the console prints a big, long error. Any ideas why this happens?

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

const surgeonRomero = new Surgeon('Francisco Romero', 'Cardiovascular');
const surgeonJackson = new Surgeon('Ruth Jackson', 'Orthopedics');

console.log(surgeonRomero.name()); // ERROR

surgeonRomero.takeVacationDays(3);

console.log(surgeonRomero.remainingVacationDays()); // ERROR

It says “TypeError: surgeonRomero.name is not a function,” among other things, even though it is a function! Hmm…

With a getter you can print the name like a property, not a function. Just remove the parenthesis behind „name“.

1 Like