So when they created es6 classes they just made everything public by default, and that was a bit odd to me but I just went with it, given I still used the older es5 style scope based classes.
Anyway a few years on now we are getting private members in classes, which seems great, but then you look at the synax:
somePublicVar = 10;
#somePrivateVar = 20;
As you can see we now have to prefix the private stuff with the hash/pound sign which seems a very odd choice given JS has public
and private
keywords which are reserved for future use, so now we want to differentiate between public and private why not just now do.
public somePublicVar = 10;
private somePrivateVar = 20;
So I am sure there is a technical reason why but I am struggling to find it, as it would seem now would be the perfect time to turn those public
and private
from reserved to "in use", making it a bit more explicit and obvious as to the access modifiers of a given member.
So when they created es6 classes they just made everything public by default, and that was a bit odd to me but I just went with it, given I still used the older es5 style scope based classes.
Anyway a few years on now we are getting private members in classes, which seems great, but then you look at the synax:
somePublicVar = 10;
#somePrivateVar = 20;
As you can see we now have to prefix the private stuff with the hash/pound sign which seems a very odd choice given JS has public
and private
keywords which are reserved for future use, so now we want to differentiate between public and private why not just now do.
public somePublicVar = 10;
private somePrivateVar = 20;
So I am sure there is a technical reason why but I am struggling to find it, as it would seem now would be the perfect time to turn those public
and private
from reserved to "in use", making it a bit more explicit and obvious as to the access modifiers of a given member.
-
#
is more concise, and requires only one word - I prefer it – CertainPerformance Commented Mar 28, 2019 at 10:05 - Possible duplicate of Referring to javascript instance methods with a pound/hash sign – Joel Commented Mar 28, 2019 at 10:07
-
1
concise: giving a lot of information clearly and in a few words; brief but prehensive.
I would argue#
doesn't convey anything, it's just a convention that was made up, why not any single unused symbol like~
etc? – Grofit Commented Mar 28, 2019 at 10:08 - 3 To be honest, I don't like it either, it looks like a ment at first glance. You need to know that it means 'private'. – KIKO Software Commented Mar 28, 2019 at 10:12
-
3
@Grofit
~
is not an unused symbol in JS. – Teemu Commented Mar 28, 2019 at 10:15
1 Answer
Reset to default 10The official FAQ answers it:
Why aren't declarations
private x
?This sort of declaration is what other languages use (notably Java), and implies that access would be done with
this.x
. Assuming that isn't the case (see above), in JavaScript this would silently create or access a public field, rather than throwing an error. This is a major potential source of bugs or invisibly making public fields which were intended to be private.It also allows a symmetry between declaration and access, just as there is for public fields:
class A { pub = 0; #priv = 1; m() { return this.pub + this.#priv; } }
https://github./tc39/proposal-class-fields/blob/master/PRIVATE_SYNTAX_FAQ.md#why-arent-declarations-private-x