Re https://github.com/wycats/javascript-private-state
-
Re "Friend" access: From a theoretical point of view, it seems . But the strawman friend syntax introduces a lot of extra syntax.
I wonder if we might use the same syntax for friends and non-friends, to keep things simpler. Along the lines of this:
const data = Symbol(); class MyClass { private #[data]; // instead of #data constructor (v) { this.#[data] = v; } } class Friend { static reportOnMyClass(m) { // Access m.#[data]. No extra "friend" syntax required. } } class OtherClass { private #[data]; // probably Error: can't reuse symbol }
Requiring an explicit Symbol isn't quite as convenient, but this is a pretty advanced feature anyway, so it might be OK.
-
I think we're all bothered by the fact that we're initializing private state twice: First automatically to
undefined
, then as an assignment in theconstructor
.Maybe we need something to the effect of initializer lists in C++?
The proposal says: "Constructors need to [= must?] explicitly initialize slots to other values." I presume this is intended to address this issue, but I doubt that it's enough: What happens if you read from
this.#private
before it's initialized, either directly or by passingthis
to a friend? What happens if the assignment is inside anif
? -
It seems that this