0
 const language = {
    set current(name) {
      this.log.push(name);
    },
    log: []
  };
  
  language.current = 'EN';
  language.current = 'FA';
  
  console.log(language.log);

The above code and below code works the same. Or is there any use cases where the output will be different?

const language = {
    log: []
  };
  
  language.log.push('EN');
  language.log.push('FA');
  
  console.log(language.log);
  • 3
    The result will always be the same for these two. The implication of setters and getters in what API you expose to consumers and how you can modify that without disruptions. – VLAZ Mar 18 '21 at 07:47
  • 3
    The reason to use setters/getters is so that no other entity can influence your member variables without calling the getters or setters. That way, you encapsulate your data and make sure there is only one way to modify it. – tomerpacific Mar 18 '21 at 07:47

0 Answers0