12. Getters and Setters I


#1

<PLEASE USE THIS TEMPLATE TO HELP YOU CREATE A GREAT POST!>

<Below this line, add a link to the EXACT exercise that you are stuck at.>
https://www.codecademy.com/courses/learn-javascript-objects/lessons/objects/exercises/getters-setters?action=lesson_resume&course_redirect=introduction-to-javascript

<Below this line, in what way does your code behave incorrectly? Include ALL error messages.>
I don’t know what i’m doing wrong. To me it’s all correct but the exercise returns me an error.

```

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

set age() {};
};

<do not remove the three backticks above>

#2

Are you stuck on the first task:
Let’s consider a new person object. We want to add a setter method that changes the age of person.
Notice, we already prepended the _age property with an underscore.
Start by adding an empty .age() setter method.

There should be a parameter for the setter method in your code, A setter must have one value:

{set prop(val) { . . . }}

#3

value would imply a primitive type like number, string or boolean. Parameter would be the correct tern since it can be a data structure, as well, which is not a value, per se.

A setter must take exactly one parameter.


#4

Apart from the required parameter already mentioned, a setter must have an accompanying getter which handles the internal lookup of the property.

instance.name = "Lu Z. Xun"

When this command is executed, the getter retrieves the _name property and passes it to the setter (speculative, needs further reading).

get name () {
    return this._name;
}
set name (newName) {
    this._name = newName;
}

Same would apply to age.


#5

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