-
Notifications
You must be signed in to change notification settings - Fork 37
This proposal does not address the actually existing needs for private fields #22
Comments
@zocky That does seem like a possible alternative, but the private fields repository attempts to explain the important use cases of class-private rather than instance-private (e.g., an equals method). I understand that it's not common practice today, but I'm wondering, what's an example of something that will break by allowing other instances of the same class to see the private fields or methods? How could you have malicious code in the same class body? Note that manipulating |
@littledan I get where you've been trying to go with the class-private names approach, though as someone who has written new languages, still think you've not thought thing through thoroughly (pardon the alliteration). An equals method can be developed even using private fields as described by @zocky while still taking into account most of what you're trying to achieve with private fields, and all without destroying the fact that
translated to this:
I've spent much time trying to think of how the sigil ( |
@littledan Let's see how my suggestion compares with your FAQ.
The example and its expansion above should be in keeping with all the other points of your FAQ,, not withstanding the above comments. Put another way, you'd get the features you want without burdening us with ugly, somewhat confusing syntax, without changing the existing fact that |
We've discussed these proposals in other threads, and my conclusion is that we should not adopt them. |
@littledan Is there a "primary" issue where discussion pertaining to other-instance-visibiilty was carried out/should be carried out? |
I'm aware that much of this has been covered in the FAQ, but I still want to chime in:
The only good reason for this (IMO ugly, but that's a matter of taste) syntax seems to be the complications with what is accessible from "this" and the desire to give other objects in the same class access to the private properties.
But this idea that other objects of the same class should have access to the private fields of an object is what is very non-Javascript.
JavaScript objects don't really have classes, they have inheritable prototypes. All it takes for another object to become "of the same class" is to set its
__proto__
property to some class's prototype. Which could be considered "dirty", but is also very common across libraries. All it takes to add a method to a "class" is to add a function member to the class's prototype. So protection by classes is ineffective and meaningless.What Javascript programmers traditionally do is restrict access to data by scope, not by class. This is why there is a very common pattern that goes like this:
This is what Javascript programmers call "private" variables. It is not the same thing as in Java or other programming languages. But it is very useful for our use cases , because it prevents programmatic access to these "private" variables and thus protects the information from other, potentially malicious scripts outside our control, in the same window (if we're talking about browsers).
The pattern is very tedious to write, and cannot be easily (or at all?) converted to using classes. A proposal that would solve this problem, and not the problem of making Javascript classes more like
classes in other languages, would be preferable.
What we really need is this:
The text was updated successfully, but these errors were encountered: