QUESTION: Что такое приватные поля классов?
Private class fields, introduced in the ES2022+ standard, radically changed the approach to data encapsulation in JavaScript, allowing developers to create properties and methods that are truly hidden from the outside world. To make a field or method private, it is enough to add a hash symbol before its name, which looks like #privateField for properties and #privateMethod() {} for methods. The main security guarantee of such members is that they are accessible exclusively inside the body of the class itself where they were declared, and are completely isolated from the outside world.
If you try to access a private field or call a private method from outside the class via an instance or from descendant functions, the JavaScript interpreter will immediately throw a syntax or runtime error, preventing unauthorized access to internal logic. This innovation solved a long-standing problem in JavaScript, where developers for a long time had to use various naming conventions, like adding an underscore before the property name, or complex closures to hide data.
In practice, private fields are ideal for storing internal authorization tokens, request caches, auxiliary counters, or intermediate calculation results that should not concern the user of this class. For example, in a User class, you can create a #passwordHash private field that will be populated upon registration and used only inside the verification method, remaining inaccessible for reading from the global scope.
Now, native support for private fields at the language level allows you to write reliable, secure code protected from accidental modifications, clearly separating the public interface of the class from its internal implementation. This elevates the architecture of client and server applications to a completely new level of reliability and security.