ES6 Use getters and setters

Why are they using ‘_’ in this._autor = autor.

What is that??

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

In JavaScript, the first character must be a letter, or an underscore (_), or a dollar sign ($). Use of the underscore is one convention for naming an object’s property or method that is intended to be private in this case, nothing more.

oh, thank you for the fast reply mate :star_struck:

You’re welcome. :slight_smile:

I was curious and looked around… Airbnb actually has published a style guide that looks to be a good read, with more information here as well.

1 Like