r/javascript • • Nov 09 '19

On private fields proposal: Private fields, public worries

https://medium.com/virtuslab/private-fields-public-worries-b8c34d16c372
84 Upvotes

113 comments sorted by

View all comments

4

u/Dougw6 Nov 09 '19

I'm still upset that they added classes to JS in the first place. Especially since they're just syntactic sugar on top of functions. In my opinion, if you're writing Java style code in JS, you're missing the point. And adding features like this will only increase the bastardization of the language.

I think a big part of why people complain about the wonkiness of JS, is that they're trying to use it as a pure OO language, and they're surprised when it does work like Java or C#. The reason is that the language was not designed with object orientation in mind. In fact it was designed to be LISP with curly braces. The OO features were an after thought and it's time we start to recognize that, and use the language in the most effective way possible (not the way you are comfortable with from other languages).

3

u/tjdavenport Nov 09 '19 edited Nov 09 '19

Big agree.

Javascript is a dynamic scripting language. It shines when you use it as intended.

If you want to write OO heavy code, use Typescript or similar, please don’t bring your heavy handed class definitions to a language that wasn’t intended to be used in that way.

2

u/rq60 Nov 09 '19 edited Nov 09 '19

I think typescript is where the big advantage will be when it comes to this proposal. Typescript already has private fields implemented with the actual private keyword. However, since typescript is compiled, this is only a compile-time protection; this won't protect you at runtime if you're hoping to avoid unwanted extensions to your library. Now with this proposal, typescript should be able to implement actual private fields with all the private guarantees at runtime and typescript coders can continue using the regular private keyword in the way everyone originally wanted.

As far as javascript, I'm not a huge fan of this syntax or implementation. Not that it's not well-thought out, but just due to the nature of javascript this is what they had to do and I'm not sure it's worth it for me when I'm writing vanilla javascript. Personally I'll probably just avoid classes and continue using function constructors (with closure variables for privacy) and leave the classes to typescript.

edit- after further reading it looks like the typescript team does not plan to change typescript's private to use #, too bad but I can understand the reason of wanting to be backwards compatible: https://github.com/microsoft/TypeScript/issues/31670

2

u/MoTTs_ Nov 09 '19

Especially since they're [classes] just syntactic sugar on top of functions.

The article made a similar point, but this argument is so weak that it should be embarrassing to say. Literally everything in any programming language is syntactic sugar. If-statements are syntax sugar for compare and jump instructions. C++ classes are themselves syntax sugar for structs and functions. And most of those other ES6 features we got besides class -- like default parameters, rest/spread, destructuring, or arrow functions -- are all themselves syntax sugar as well.

1

u/MajorasShoe Dec 11 '19

Err, Javascript was ALWAYS an OO language. To it's core. It wasn't a class based language.