Using getters/setters as opposed to methods?

Keff - Jan 26 '20 - - Dev Community

What are your thoughts on using getters & setters in Javascript as opposed to methods? And when and why would you use them?

This

const obj1 = {
  _name: 'Rick',
  get name(){
    return this._name;
  },
  set name(name){
    this.name = name;
  }
};
Enter fullscreen mode Exit fullscreen mode

As opposed to

const obj2 = {
  _name: 'Morty',
  getName() {
    return this._name;
  },
  setName(name){
    this.name = name;
  }
};
Enter fullscreen mode Exit fullscreen mode
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .