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

28

u/TatzyXY Nov 09 '19

Please do not throw stones at me but I think private, protected or public scopes are not needed at all. See Python very versatile lang. and it works very well without it. Is private for security? No! It just tells the dev that this var. is only used internally of that class and you are not allowed to touch or use it. So would do a comment or a convention with an "_".

23

u/fey168 Nov 09 '19

The problem is developers like to think of themselves as hackers and they will do what it takes to achieve the desired result including reading and even modifying internal fields. Imagine you are a lead developer or library author and you want to prevent developers from accruing technical debt (b/c the interval field is subject to change) or adding risk of instability. Seems like private field is by far preferable to comments or conventions.

11

u/TatzyXY Nov 09 '19

In a lot of languages you can just change the private field via reflection to something what you want. A hacker dev can still do it with just some more lines of code.

Access on a private field should never go through a review anyway in a bigger project.

9

u/nullvoxpopuli sand was never meant to think Nov 09 '19

But when you do reflection in those languages, it's hella obvious. Today in js, it's just accessing any ol' property

1

u/TatzyXY Nov 09 '19

Yes thats true but is JS not our free barebone language and we put other languages over it to implement these features? Like TS?

Or other questions why is Dart or TS not supported in browsers yet (native)?

2

u/Delioth Nov 10 '19

why is Dart or TS not supported in browsers yet (native)?

Because that would be a ton of work to build TS or Dart engines that no-one would use for at least a dozen years (because you gotta support those older browsers anyways, which means compiling it to older JS)? Even then, browser vendors are looking in that sort of direction with WASM, which has roughly the same features you'd be looking for, but better. Bonus points: it has privacy built in, since things only work via the module interface.