Understanding getters and setters - private variables

I have trouble understanding this exercise
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);

The instruction said:

Getter functions are meant to simply return (get) the value of an object’s private variable to the user without the user directly accessing the private variable.

Why can’t I “directly accessing the private variable”? Can’t I simply use

console.log(novel._author) 

to get the object property? Why do I have to use the getter

console.log(novel.writer);

for the same thing?
Besides, I also can’t tell

  • When is a variable “private”? I haven’t seen an explanation anywhere in the lesson.
  • What is “the user” in this scenerio?

fCC keeps refering to these terms “the user”, APIs and “abstracting implementation details” and expect new learners to figure all of these out on our own. I feel like even if I manage to complete the course, there’s gonna be so many weak spots in my understanding.

There is an entire series of follow up certificates that are about APIs. Using the correct language now helps prepare you for the API certificates.

The challenge for this lesson is a bit contrived bit shows exactly the point. In the challenge, you are creating an object that holds a temperature value. But you get to decide how you store that temperature, in C or F. The internal behavior of the object is determined by the person writing the code.

However, the external behavior of the object needs to match how the object will be used in your project. If you always need to get and set temperatures in C, then the getter and setter provides a way for the internal representation of the temperature to be completely separate from the externally reported format.

Ultimately, objects help you break the parts of your project down into more managable pieces, but if you allow direct access to internal object data, then you may end up with small changes that ripple throughout your code.

1 Like

For this example, technically you can, but as @JeremyLT noted, there are reasons why you should use the provided API instead of directly manipulating “private” variables.

Recently, real private class variables have been added to JS, which do actually prevent you from accessing them directly. So this example could be changed to:

class Book {
  #_author;
  constructor(author) {
    this.#_author = author;
  }
  // getter
  get writer() {
    return this.#_author;
  }
  // setter
  set writer(updatedAuthor) {
    this.#_author = updatedAuthor;
  }
}

And then you would not be able to directly access #_author, you would be forced to use the getter/setter.

const novel = new Book('anonymous');
console.log(novel.#_author); // syntax error, can't do it
2 Likes

Thanks for taking the time to answer! Really, my frustration has been somewhat repressed, and I can get through another day of learning to code!

1 Like