1

Is it possible to change the value of the property getter of an object? let's say we have

const autoIncrementer = (function() {
  let value = 0;

  return {
    incr() {
        value++
    },

    get value() {
        return value
    }
  };
})(); 
function anotherFunctin (){//log smth.}
autoIncrementer.value = anotherFunction;

P.S. I know that this does not do any good, so I just need an explanation of why is this so? and is there any way to achieve this goal?

1 Answers1

0

You can use setter along with getter.

let obj = {
  get propName() {
    // getter, the code executed on getting obj.propName
  },

  set propName(value) {
    // setter, the code executed on setting obj.propName = value
  }
};

About getters/setters usage - read more here

vovchisko
  • 2,049
  • 1
  • 22
  • 26