So after reading about this proposed change, and then having a read through various comments in the issues section of this repo, few things have become apparent to me:
- There is a substantially larger dislike of using
#
as a way to define values within a class.
private
can occasionally be a misnomer, since other classical languages use it to mean different things.
- People (including me) have a hard time being decisive about what they actually want, and are less quick to adapt larger deviations in terms of syntax.
My proposed changes:
I might be a small part of a minority (not sure yet), but as a developer I have no issues accessing hidden scopes using #
. This makes more sense to me in terms of being able to know immediately if a method or static method is calling conditionally accessible code.
Since private
seems to be the pivotal issue (in terms of syntax), I decided that a keyword masked
would fit the bill due to it being more readable and less jarring to existing developers.
Example
class Counter extends HTMLElement {
masked xValue = 0;
get masked x() { return #xValue; }
set masked x(value) {
this.#xValue = value;
window.requestAnimationFrame(this.#render.bind(this));
}
// potentially even the below
masked x get()
masked x set()
masked clicked() {
this.#x++;
}
constructor() {
super();
this.onclick = this.#clicked.bind(this);
}
connectedCallback() { this.#render(); }
masked render() {
this.textContent = this.#x.toString();
}
}
window.customElements.define('num-counter', Counter);
Why masked
?
A more obvious keyword like hidden
obfuscates the actual syntax required to access the value. Masked makes it intentionally more clear that the value is present but requires specific intentions to access, which in this case would be the #
keyword.
An argument regarding syntax parsers.
There is a substantial upside to using a named keyword such as mask
or masked
or rather any unique keyword, which is the accessibility for less sophisticated parsers to analyze code (including grepping and editor searching). Having the #
keyword exist as both the declarative statement and the way to access properties is slightly unintuitive.
To be more specific, I worry about the ambiguity this holds against something more standardized like static
, as it allows us to specifically detect the amount of times static values have been declared within a class
. The same does not hold true to this specification.
Final thoughts (as an aside)
Like I assume most of the people here do, I use this language daily at my place of work. Being so frequently exposed to a language causes one to make certain expectations about how it looks and the way it feels.
Over the last 4, maybe 5 years there has been a substantial growth in the additions to JavaScript's features, which has enabled use to solve problems using powerful patterns (class
, extends
, spread operators, and async await
come directly to mind).
That being said, this kind of exponential growth can also lead to a ghastly feature creep, or in this case, having proposals pass through multiple stages that feel not in par with the overall design of the language. I might just be another drop in the sea but this sort of feature (and the implications it presents) can have a lasting effect on many workflows, and to be completely honest I do not believe this feature to be ready for stage 3.
I've been wanting private class
methods for so long, so I'm looking forward to whatever form it takes when it reaches stage 4, though I'm really hoping it's the one that the majority of us actually want to use.