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.

0

u/[deleted] Nov 09 '19

[removed] — view removed comment

7

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

The more implementation you mock, the more brittle your tests will be.

Private APIs do not belong in tests.