Hash -> Underscore #100
Comments
Please read the FAQ; it should explain it quite well. |
I think I did, but if there's a specific segment you think is most enlightening I am happy to reread. |
I think the FAQ doesn't address this question specifically. @probablyup, the short version is, |
Actually, it is possible to use underscores instead of hashes, but you would have to accept some downsides. Here's how it would work: But the basic idea is that if you declare private _x; in a scope, then all references to class A {
private _x;
// Now every occurrence of obj._x in this class will refer to the private field
} Out of curiosity, would you be willing to accept the downside in order to get the underscore? |
@zenparsing I at least like this much better! I actually don't see the lack of privates shadowing as downside but an upside. |
@corbinu Consider the following example: class A {
private _x;
static printX(obj) {
console.log(obj._x);
}
}
A.printX({
_x: 1;
}); Intuitively, would you expect that to print "1" or throw a TypeError? |
1
…On Fri, Jul 28, 2017 at 10:38 AM zenparsing ***@***.***> wrote:
@corbinu <https://github.com/corbinu> Consider the following example:
class A {
private _x;
static printX(obj) {
console.log(obj._x);
}
}
A.printX({
_x: 1;
});
Intuitively, would you expect that to print "1" or throw a TypeError?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#100 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAiy1vU5N683bzCCGEFcMdUqpyezSOEIks5sSfJ6gaJpZM4OmHQA>
.
|
@probablyup Then you're gonna need that hash 😉 |
Why? The privateness should only apply to this._x |
@probablyup But in JS, |
We're discussing new language features, yes? Maybe this has a getter for classes that inspects the privateness of variables and throws. |
It's too bad that caller was removed from ES5. Would have been useful. |
Please see the FAQ. |
Alright, reread the FAQ and answered my questions. I still don't think hash is the right symbol though, given its historical usage as a notation. Thanks for your comments! |
I agree it's not ideal, but I don't think we have any other options, really. |
I was very excited to see this proposal, but I'm not sure using a hash symbol is the right choice to mark a class variable as "private".
Typically when writing class or module methods, a common pattern is to prefix the name of the function, etc. with an underscore to denote that it is "internal". I think the concept of internal variables has good symmetry with the private variable proposal and thus an underscore is a better fit.
There is also the matter of
Class#Method
being an established notation pattern, which would be very confusing if the private variable prefix was also a hash symbol.The text was updated successfully, but these errors were encountered: