I’m confused. Aren’t getters and setters functions? Shouldn’t I be able to use the setter like a function and pass in the new value I want as a parameter of the setter like so: `person.age(22)`?


#1

Question

I’m confused. Aren’t getters and setters functions? Shouldn’t I be able to use the setter like a function and pass in the new value I want as a parameter of the setter like so: person.age(22)?

Answers

Yes, they are functions, but person.age don’t refer to the function itself so person.age() won’t call it. The getter and setter functions only invoke when the corresponding property is assigned.