1

class Counter {

  constructor(initialValue = 0) {
    this.value = initialValue;
  }

  increment() {
    this.value += 1;
  }

}

const counter = new Counter();

console.log(counter.value); // 0

counter.increment();
console.log(counter.value); // 1

counter.value = 42; // <-- any way to forbid this?

counter.increment();
console.log(counter.value); // 43 :(
sp00m
  • 47,968
  • 31
  • 142
  • 252
  • "mutable only by own methods" is the same as "private to own methods, with a public getter". The same solutions, approaches and drawbacks apply. – Bergi Jun 01 '17 at 15:38

1 Answers1

0

I'm not aware of any way to have the value on the instance, but forbidding write access only when accessed outside of a class instance function body. You could have a look at private fields, if you only want access within the class instance methods (get and set): github.com/tc39/proposal-private-fields

You could also work around these limitations using getters and WeakMaps:

counter.js:

const privateProps = new WeakMap();
const get = instance => privateProps.get(instance);
const set = (instance, data) => privateProps.set(instance, data);

export default class Counter {

  constructor(initialValue = 0) {
    set(this, { value: initialValue });
  }

  increment() {
    get(this).value += 1;
  }

  get value() {
    return get(this).value;
  }

}

main.js

import Counter from 'counter.js';

const counter = new Counter();

console.log(counter.value); // 0

counter.increment();
console.log(counter.value); // 1

counter.value = 42; // <-- don't define a getter to forbid this

counter.increment();
console.log(counter.value); // 2 :)
nils
  • 25,734
  • 5
  • 70
  • 79