-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Why not fields? #21
Comments
I'd like to make it easy for people to refactor from public to private. I think this can improve the uptake of private fields and make them easier to learn. The private methods explainer walks through an incremental update example. Maybe it would be helpful to do a similar exercise for this proposal. |
Great idea. I've started on a refactoring document here. |
Closing in preparation for public review. I'll add a "why not fields" document shortly. |
I'll add a few points
|
To match what public instance fields do, they'd have to |
That is certainly true, but typically users don't need to use If you add public fields to the language you of course must use |
Here are my thoughts:
JS has no such thing as "fields". There are properties and there are internal slots/instance variables. When we attempt to clump them together under a single unifying concept named "fields" we get all kinds of problems. For instance:
obj.#x
andobj['#x']
.Additionally, fields tend to be an attractive nuisance leading developers towards these anti-patterns:
I re-reviewed the original motivation document. The stated motivations are:
While I think public "fields" makes sense on their own, the "fields" concept does not generalize well to encapsulated per-instance state and hidden methods.
Here's the main idea: if we choose to have public fields (which have some minor problems of their own), then we will be lead irresistibly toward private fields and private methods (which have a bunch of problems). The upside of public fields is pretty small, while the downside of private fields and methods is fairly large. Therefore, it is better not to have public fields at all, and choose a different syntax for instance variables.
The text was updated successfully, but these errors were encountered: