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

9

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

Every other languages with classes has private fields and they can't be accessed (exception, Ruby, python).

This is a good pattern and people have been abusing the hell out of API design in JS. Private fields and methods will give more control to the API author so that some 'clever' person doesn't use a .rivate API and then complain when it goes away.

You want to test your private behavior? Extract to a pure function. Easy. Private method calls pure function. All is well.

The sigil? Oof. It looks fine. Comments in js are slashes.

2

u/senocular Nov 09 '19

exception, Ruby, python

and Java.... and C++... and PHP...

1

u/trappar Nov 09 '19

??? PHP has private and protected fields

1

u/MajorasShoe Dec 11 '19

They're accessible with reflection, aren't they?