Hi, new to learning code - JavaScript

Hey, so i’m on part twelve of the Objects lesson, and I don’t understand getters and setters.
I’ve been learning pretty fast and this is the first time i’ve been stumped. I don’t really comprehend how to create setters and how they fit in with the rest of the code. If anyone can give me a detailed, but somewhat noob-friendly advice on getters and setters that would be amazing, thanks :slight_smile:

Getters and Setters are special methods of the object (or class). In JS attributes that have getters, or getters and setters, are specially prefixed.

this._age = age; (in a constructor)

_age: 19,  (in an object literal)

The instance does not access the special property, though, on purpose. It can be accessed, but we wouldn’t write it that way.

instance._age

would instead be written,

instance.age
person = {
  _name: "Wee Gillis",
  _age: 19,
  get name () {
    return this._name;
  },
  get age () {
    return this._age;
  },
  set age (newAge) {
    this._age = newAge;
  }
}

Since this is an object literal, it is its own instance.

   person.name  =>  Wee Gillis   // invokes the getter
   person.age   =>  19           // invokes the getter
   person.age = 20               // invokes the getter, which invokes the setter
   person.age   =>  20
1 Like

Hello mtf, thank you for this haha, I spent about 10 minutes studying this code along with looking at the MDN on getters and setters and it helped alot. I’ve now become proficient with getters, but setters are still making me stumble a little. I’ll get it down eventually, thanks for the help!

1 Like

Because setters take inputs, usually from the user, it is necessary to validate, either before calling them (the user input phase) or inside. We obviously want a positive integer for the newAge so would need to verify that before changing the _age property.

1 Like

They are first and foremost, methods, so they have context (this) but hidden in their wiring is the actual code we might have had to write to facilitate them. ECMAScript2015 is when this first became implemented, if I’m not mistaken. Previous to that we had to write our own (as if there was widespread usage), and developers would have drawn upon the wisdom of John Resig…

John Resig - JavaScript Getters and Setters

It’s with the contributions of experts like him (there are many, but finite) who put forth the understanding and insight that helped make it happen that today’s world has a very brisk, very robust, and very astute script language built into every browser.

Anyway, getting off track here… Getters are parameterless. Their one job is to return what’s to be got. There is one tiny hitch though. The arguments object. When we assign a getter call a value, it triggers the call to the setter. That’s how, person.age = 20 works. It is first off, a call to the getter, person.age, but in lieu of the assignment that value is placed in the argument list, and given to the setter.

It’s the getter that invokes the setter, which is why a setter needs a getter. Remove the getter and see if your setter still works.

1 Like