Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

For me, the most awesome and useful change to Javascript has been the addition of private and static modifiers, although the implementation is kinda weird (why use “static” keyword but not “private” and the # sigil instead?). I use both TS and JS professionally but much prefer native JS and use it in my personal projects, however, the closer native JS can get to TS, the better, as I really do appreciate many of it’s features.


The # sigil has always bummed me out too.

Supposedly it's because of this:

> Why isn't access this.x?

> Having a private field named x must not prevent there from being a public field named x, so accessing a private field can't just be a normal lookup.

https://github.com/tc39/proposal-class-fields/blob/main/PRIV...

Combined with this:

> 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.

https://github.com/tc39/proposal-class-fields/blob/main/PRIV...

But I still think it's weird.


Because they awkwardly bolted C++-like class syntax on top of prototype-based language and are now fighting that.

Same goes for methods that you have to manually bind to `this` in the constructor etc.

It literally is one of the "they didn't think if they should" parts of the language.


This is why I appreciate TypeScript, as it fixes a lot of Javascript’s syntactic goofiness, but of course I can’t use it natively, which takes it off the table for some projects.


The # for private throws me every time I look at my code. Keyword "private" would have been so much cleaner and in line with most programming languages out there.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: