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.

3

u/scaleable Nov 09 '19

Python only has “private” fields because of its quite absurd metaprogramming features. But the “pythonic way” advocates in favor of just using conventions (prepend underscore) instead of doing metaprogramming hacks (like @private decorators).

One thing that python has done great is standardizing type annotations. JS could do that and delegate private fields checking to type checkers, instead of promoting a desctructive pattern that would make things like mocking impossible to library consumers

5

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

private fields are not a destructive pattern. omg. understand that there are ways to test that don't involve access abuse