Getters and Setters I


#1

The lesson makes a bit of a leap that I’m not sure I understand. For the first step, I just added:

let person = {
_name: ‘Lu Xun’,
_age: 137,

set person.age()
};

But the second step is where I got lost. Where does ‘ageIn’ come from?


#2

There is the issue. Within the object we refer to person as this, and then only inside methods.

A setter method always takes a parameter, the new value to set.

set age(newAge) {
    this._age = newAge;
}

However, there is another small problem, the getter is missing.

get age() {
    return this._age;
}

This should all unfold as you complete the exercise. Consider the following assignment…

person.age = 19;

The above will, with the help of the getter invoke the set age method and pass in the assigned value as the argument. Don’t get too hung up on this. There will be more chances to let this sink in.