News for hackers. Stuff that matters.
Re:JavaScript private class fields considered harmful by graypegg on Saturday April 29, 2023 @12:28AM (#35750029)
Totally agree. This is very much a symptom of JS’s class-ish syntactic sugar. Access modifiers have to be a runtime check, and be bolted onto an environment that was built around function scope being the only way control access.

Using typescript to handle things like access modifiers at compile time makes everything much more predictable.

Re:JavaScript private class fields considered harmful by dexwiz on Saturday April 29, 2023 @05:06AM (#35751325)
Doesn’t typescript just transpile to closure based access control?
Re:JavaScript private class fields considered harmful by afiori on Saturday April 29, 2023 @03:52PM (#35756519)
Closures are a much better "class" primitive than JS classes
Re:JavaScript private class fields considered harmful by nosianu on Saturday April 29, 2023 @06:08PM (#35757736)
TypeScript's "private" is only for type checks. It is simply removed.

You may also want to read what I wrote a few days ago already: https://news.ycombinator.com/item?id=35653653

TypeScript now supports both the ECMAScript #private - it has no choice in the matter, since it's in the language now - and their own development-time "private". It sounds messy but that's the fault of those who wanted it in the language, as runtime checks.

I agree with the other comment, closures are far more powerful and flexible. I understand that this OOP stuff in JS was probably a nod to the many programmers in many companies already used to it, to make JS more palatable for them. It's easy to demand millions of people worldwide to adapt, but in business reality it does not work that way. So I grudgingly accept those new(er) things in JS as a business decision and necessity, for the huge crowd of people doing programming mostly just for the money, who had to write more and more code for the web that used to be run as installable software on Windows.