3 ms·
> just put the private fields in the parent closure where they can't be accessed externally Your approach can't translate the following code: class MyNumb
by lifthrasiir 5y ago
> just put the private fields in the parent closure where they can't be accessed externally
Your approach can't translate the following code:
class MyNumber {
#value;
constructor(value) { this.#value = value; }
add(other) { return new MyNumber(this.#value + other.#value); }
}
The proper solution is to use a non-enumerable property with an unnamed Symbol name, which is possible but cumbersome.
- tnzm 5y agoYou're not supposed to be able to refer to other.#value, though, isn't that the entire point of a private field?
- lifthrasiir 5y agoThey are private to classes, not instances. Many languages with private fields do allow access to private fields in other instances of the same class.