** Explanation Needed ** Use getters and setters to Control Access to an Object

class Book {
  constructor(author) {
    this._author = author;
  }
  // getter
  get writer() {
    return this._author;
  }
  // setter
  set writer(updatedAuthor) {
    this._author = updatedAuthor;
  }
}
const novel = new Book('anonymous');
console.log(novel.writer);
novel.writer = 'newAuthor';
console.log(novel.writer);

This is the example code from the challenge, which I don’t quite understand.

I understand the usage of getter and setter, but in this example, where are we getting the “writer()” from?

JS is different than some languages. In some languages, you would create setters and getters as methods like “setWriter” and “getWriter”, and that is how you would access them, like:

const writer = myBook.getWriter();

But JS doesn’t do it that way. You define the methods with the “get” and “set” keywords (as you’ve done above) and you just access the setter and getter as you would a property:

const writer = myBook.writer;
myBook.writer = 'Someone Else';

and JS just knows to use the getter for the first one and the setter for the second one, because of context. The code above doesn’t know that it is using a getter and setter, and it doesn’t care.

I understand the usage of getter and setter, but in this example, where are we getting the “writer()” from?

In short, that name “writer” is the name of the “pseudo property” you will be accessing in the code. Again, we just access myBook.writer and JS knows whether it is a standard property, and if not, it knows to use the setter or getter.

1 Like

Could you also please help me understand the challenge?

Here’s the link:

Here’s the solution:

// Only change code below this line
class Thermostat {
  constructor(fahrenheit) {
    this.fahrenheit = fahrenheit;
  }
  
  get temperature() {
    return (5 / 9) * (this.fahrenheit - 32);
  }
  
  set temperature(celsius) {
    this.fahrenheit = (celsius * 9.0) / 5 + 32;
  }
}
// Only change code above this line

const thermos = new Thermostat(76); // Setting in Fahrenheit scale
let temp = thermos.temperature; // 24.44 in Celsius
thermos.temperature = 26;
temp = thermos.temperature; // 26 in Celsius

The challenge states:

" In the class, create a `getter` to obtain the temperature in Celsius and a `setter` to set the temperature in Celsius."

If we are already going to obtain the temperature in Celsius, why do we still need to set the temperature in Celsius?

Perhaps I don’t understand what it’s asking. Please clarify, thanks!

You aren’t the only one who finds this challenge a little confusing :slight_smile: Don’t overthink it. This challenge is merely exposing you to getters and setters in JS. The challenge itself doesn’t have to make perfect sense. You just need to understand what getters and setters are and how they work. You may never use them yourself (they are not mandatory and some people think they make the language more confusing and don’t really help much). But there is a chance that you will run into them so you need to at least know what they are.

Do you understand what getters/setters are and how you use them?

1 Like

That does make me feel slightly better.

And yes, I suppose getters and setters are pretty much being used for manipulating variables within objects?

If we are already going to obtain the temperature in Celsius, why do we still need to set the temperature in Celsius?

Yeah, that doesn’t make practical sense. It’s just a silly little example, don’t worry about it making practical sense.

I suppose getters and setters are pretty much being used for manipulating variables within objects?

I like to think of it as controlling access. You can control how certain properties are accessed and changed.

1 Like

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