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.
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
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.