Closed lifeart closed 6 years ago
I don't get what the issue is. When simulating private fields, you should use one weakmap per field and this
is the instance, not a privates
object.
But this creates a problem when even the internal methods of the class do not have the ability to affect private fields, no?
Maybe we could just bind the context to all functions entering the private space from outside of the class?
privates.set(this, {
id: ids++,
makeGreeting: isFunction(makeGreeting) ? makeGreeting.bind(this) : makeGreeting,
});
Agree with Nicolo, such an implementation doesn't provide the privacy you want.
You can affect private fields using this.#id
inside internal class methods.
https://github.com/tc39/proposal-class-fields/blob/master/PRIVATE_SYNTAX_FAQ.md#how-can-you-model-encapsulation-using-weakmaps
To prevent overriding we need to defineProperty instead of plain assign
Before:
After: