1

Why Javascript decided to go with "#" (hash) prefix for private fields, instead of the traditional convention "_" (underscore) we see in other languages?

As described in: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/Private_class_fields

class ClassWithPrivateField {   
  #privateField
}
Lucas Pottersky
  • 1,844
  • 6
  • 22
  • 38
  • 5
    There's a committee that governs changes to the language, and that's the decision they made. Using `#` allows for a new feature that cannot affect older working code. – Pointy Aug 06 '20 at 12:40
  • 4
    TL;DR: backwards compatibility. `_` is already used today as a mere *convention*. – deceze Aug 06 '20 at 12:41
  • 4
    how is the question opinion based ? anyways here's the official FAQ https://github.com/tc39/proposal-private-fields/blob/master/FAQ.md#why-was-the-sigil--chosen-among-all-the-unicode-code-points – jonatjano Aug 06 '20 at 12:42
  • @Pointy Instead of writing a short-hand answer in a comment, why not post an answer? – Etheryte Aug 06 '20 at 12:43
  • @Nit I think this is a many-times-over duplicate – Pointy Aug 06 '20 at 13:23
  • @Pointy In that case, wouldn't it be better to close it as a duplicate instead of opinion-based? If it's a many-times-over duplicate as you say, there's surely interest around it and would be good to have one canonical source to link to. – Etheryte Aug 06 '20 at 13:27
  • @Nit I am terrible at actually *finding* duplicates. And really, it doesn't matter; this question is of low value to the JavaScript community. – Pointy Aug 06 '20 at 13:37
  • 3
    the proof this question is not opinion based is that there's a very OBJECTIVE answer right from the TC39 group, as pointed out by @jonatjano comment – Lucas Pottersky Aug 06 '20 at 13:41
  • @Pointy While this is a very subjective opinion, if a question comes up so much that it becomes a many-times-over duplicate, then I would say that there's clearly value to the community. Even if it isn't directly applicable to the code you write, understanding the rationale behind why and how the language you use evolves can be very valuable insight. – Etheryte Aug 07 '20 at 08:42

0 Answers0