-
Notifications
You must be signed in to change notification settings - Fork 113
Possible solution for lack of [] access #104
Comments
I don't understand what problem this proposal is trying to solve. What's the problem with getting |
@littledan Nice to hear from you again. There's nothing wrong with using a private object to gain Another reason for wanting to get var #somevar; //1
class Example {
#somevar //2
constructor() {
this.#somevar = 0; //3
}
} Code at 1 is a syntax error under the given proposal. That makes sense to everyone because that's how it's always been. Code at 2 is legal, and uninitiated developers will think of var #somevar; //1
class Example {
#somevar //2
constructor() {
this#.somevar = 0; //3
}
} Code at 1 is still a syntax error. Not only is it a double declaration, but also a declaration of a private member outside of the declaration of the containing object. So no problems there. Code at 2 is roughly equivalent to Example#.somevar = undefined; which puts In C-like languages, the tool for accessing a language-specific resource without exposing that resource is an operator. With this proposal (given the spec) you're trying to access an internal, sealed object. That object should not ever be exposed or otherwise directly or indirectly referenced in code as this could lead to a violation of hard private. One of the best ways to accomplish that is to make the sigil a binary operator with the meaning I've given, and with the right-hand term being an access operation. In this way ES code is never allowed access to the internal object. I get that this breaks with the original idea of replacing the As a side benefit, this suggestion opens the possibility for something like this: var a = {
#foo: 0,
next() {
return ++this#.foo
}
}; since there's no need for class-specific private names in the implementation. This means that private fields wouldn't destroy the symmetry between classes and constructor functions since private fields could still be easily put on plain objects. |
I don't plan to make this change. |
May I know what it is you didn't find agreeable about this suggestion? |
A few minutes ago, I had an idea about how to solve some of the sigil woes and "just get used to it" learning curve issues with this proposal, and all without needing to replace or remove the sigil.
Let's make
#
an operator of it's own!Since according to the spec that comes with this proposal, all objects have a [[PrivateFieldValues]] slot, and the contents of that slot is essentially a sealed object with
privateName:privateValue
pairs in it, why not useobj#
to mean "access the [[PrivateFieldValues]] of obj". LValues ending with the sigil would still be illegal, and the sigil would still only be accessible inside aclass
declaration.What this gets us is the following:
this.field
would be identical forthis#.field
[]
notation access to private fields#
private
keyword in class declaration as alternate for#
in member variable declarations without conflict for meaning.I think that this might be less disturbing to those of us who didn't/don't want the sigil.
@littledan @bakkot @ljharb
Is this a viable idea?
The text was updated successfully, but these errors were encountered: