Understand : Use getters and setters to Control Access to an Object

Hi,

I would like to correctly understand what is it explain in this course.
Are my comments rights ?

class Book {
  constructor(author) { /* is used by const lol = new Book('anonymous'); */
    this._author = author;
  }
  // getter
  get writer(){ /* is used by console.log(lol.writer); */
    return this._author;
  }
  // setter
  set writer(updatedAuthor){ /* is used by lol.writer = 'wut'; */
    this._author = updatedAuthor;
  }
}
const lol = new Book('anonymous');
console.log(lol.writer); 
lol.writer = 'wut';
console.log(lol.writer);

For example, if I use const lol = new Book('anonymous'); only, we’ll only use the constructor part, is that right ?
Maybe I’m completly wrong haha :sweat_smile:

Thanks in advance,
Quentin

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/es6/use-getters-and-setters-to-control-access-to-an-object

When you instanciate and object like new Boo('superman') the first method or function that call automatically is the constructor, that way you can pass data like ‘superman’, but when you create an instance you have access to all the methods that have that class

1 Like