-
Notifications
You must be signed in to change notification settings - Fork 0
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
I need to replace let/const
. Any suggestions?
#12
Comments
For now in the documentation, the keyword will be |
What about this? class A {
private [somePrivate] = 2;
someMethod() {
assert(this[somePrivate] === 2);
assert(typeof somePrivate === 'symbol');
}
} |
Like I said before, I'd love to, but I don't want to deal with the push-back over Typescript's use of |
I don't think this is a very important issue to most programmers. If So I think I think You already know I have some other keyword candidate to map |
I also vote for keeping |
imo let/const can’t possibly make sense if you’re accessing them on an object - ie, if you have per-instance data, and you need to access them on multiple objects at the same time. |
@ljharb |
I should qualify my comment: it only applies in the context of this proposal. More generally, this is one of the reasons I prefer the class fields proposal: field/property declarations look like field/property declarations. But without a prefix like |
The problem is that ES already has a history of not doing things that way. There is absolutely no way to declare a property using As for |
IMO using |
Although I don't like this idea either, just to throw it out there, I think |
@mbrowne I'm not insisting on dismissing your point. I just need a stronger argument to get me past the conflation of variables and properties. Your argument about other languages doesn't work for me because those other languages have had that conflation in them from the beginning, whereas ES has kept them separate since the beginning. I need an argument that says they can be conflated without breaking the mental model differences between them in ES. Otherwise I just can't do If you can give me a strong-enough argument, I'll be happy to reconsider. |
@rdking I understand that why you think I think long history make nothing more valid, especially every one to two years the js community are doubled, so it's not a really "old" language if you consider the community. There are also many (don't know the accurate number) js programmers also use other languages, especially Dart, Swift, Kotlin which are popular languages for client-side developing. So I think at least fullstacks engineers and those who do both web developing and client developing, have no issue about the conflation. On the other side, if you consider current field proposal ,which break too many current js syntax/semantic, you can believe the conflation cost is ok for get rid of other much huge cost! 😂 |
We’re designing for all future JS devs, the majority of whom will likely never have used another language. It’s helpful to take lessons from other languages, but JS is its own thing, and it’s ok if we deviate from other languages in order to achieve a more consistent mental model. |
Agree, but before that you should not break current mental model of JS. I will say most programmers will agree current proposal fail on that as the feedbacks we got. |
I think I just came up with a way to justify continuing to use @ljharb I need you to give what I'm about to suggest some real thought. I know you mentioned that there's a seeming embargo on shorthand notations, but in this case, it may turn out to be a saving grace. With this idea, I even fixed the imbalance problem that led me to give up on shorthand notation in the first place. Facts:
Solution:Require all members to use the same namespace, but only within the Basically, this would mean it would be impossible to declare a public and private member with the same name. However, it would be perfectly fine to later attach a new public member that happens to have the same name as an existing private member. Advantages:
Example//Throws due to duplicate name in class definition
class Bad {
let x = 0;
get x() { return x; } //Syntactically ok, but weird
}
class Good {
let _x = 0;
let y = 1;
get x() { return _x; } //Because we can't duplicate names
print() { console.log(`y = ${y}, x = ${x}`);
}
var g = new Good();
g.print(); //y = 1, x = 0
g.y; //undefined
g.y = 3; //This is ok because private members are still in their own namespace
g.print(); //y = 1, x = 0
g.y; //3 |
What about What happens if the class does |
@ljharb
I'm not sure I understand this question. If you're asking what would happen if there was a function in the class that ran |
i'm talking about the public Given that, what's the benefit to denying class authohrs the ability to do |
Oh. OK. |
The thing to remember is that the rule about keeping public and private members within the same namespace only applies within the lexical scope of the definition during the evaluation of that definition. Even the constructor of the class is free to add new public properties that share the name of private ones. |
Consider current js allow |
@hax Nope. Can't do that. If it's possible to declare any 2 properties with the same name inside the lexical scope of the class, the whole house of cards falls. |
@rdking Obviously you can't allow |
The problem with having a public and private member with the same name is that it completely imbalances any attempt at shorthand notation. Since there can be duplicate names, which one is going to be the one that gets accessed when you use the shorthand? This means you'd only be able to shorthand either the public or the private, but not both. But if you can't do both, it's not worth doing either. |
not sure, temporarily deleted. |
Example of some pointless but perfectly valid code. class X {
let x = 1;
let y = 2;
constructor() {
Object.defineProperty(this, "x", {
configurable: true,
get() { return x * 4; }
}
this.y = 7;
y++;
}
print() {
console.log(`(private) x = ${x}`);
console.log(`(private) y = ${y}`);
console.log(`(public) x = ${this.x}`);
console.log(`(public) y = ${this.y}`);
}
}
/* Prints
* (private) x = 1
* (private) y = 3
* (public) x = 4
* (public) y = 7
*/
(new X).print(); |
In general, any property declared within the class must have a unique name. All Since all non- |
@rdking You're making a big deal about the upcoming implementation change and saying it completely invalidates the closure concept, but I don't understand why. Aside from proxies forwarding to private members, it doesn't seem like much would change for developers in terms of the observable behavior of private members (you mentioned protected and friend earlier but I think that's a separate discussion). And since there will be an intermediate container object for private members, the notion of separate "private properties" doesn't precisely reflect what's going on under the hood anyhow. Also, the use of shorthand syntax obviously makes it seems more like a closure, not less, so I'm not following your argument there. But I think the intuitiveness of let/const and shorthand syntax for access might be a good thing for this proposal anyway (although of course there's still the learning curve of understanding that these members are instance-level and not static). |
See hax/hax.github.com#44 (note it use |
@mbrowne The original update did invalidate the closure concept. It just wouldn't work as long as it was possible to declare 2 different properties with the same name. However, as of the latest update, that's not a problem anymore. The mental model can go back to the closure concept. Like you said, the solution for the shorthand problem does indeed drive the model home. |
The README for this proposal has been updated. |
Here's the problem. I'm re-writing the README in terms of the new suggestion in #10. This means that instead of having "instance-closure variables", I now have "instance-private properties". This means
let
andconst
feel out of place since these terms define variables instead of properties. I already know that, even though I desperately want to, I can't useprivate
since it will get shot down over the insanely weak "but I expectprivate x
to be accessed withthis.x
" argument. Even PHP didn't uphold that expectation...Anyway, I'm open to suggestions. If it weren't for the fact that Symbols can be property names, I would have kept
let
andconst
. However, who wants to deal with developers thinking that because they can writelet [someSymbol] = 2
in aclass
definition, that it's ok to do that in a function? I know I don't.The text was updated successfully, but these errors were encountered: