Private declarations S1
- Stage: Stage 1
- Status: Active
- ECMAScript edition: —
- Synchronized: Aug 28, 2026
- 中文译文 · Source repository
This proposal introduces private declarations, allowing trusted code outside the class lexical scope to access private state. It supports protected state for subclasses, friend functions for controlled access, and private fields on non-class object factories.
The README below comes from the upstream repository and may contain outdated stage or status metadata. Use the proposal details above as the current source of truth.
Private Declarations
A proposal to add Private Declarations, allowing trusted code outside of the class lexical scope to access private state.
This also allows us to bring private state to regular objects!
Possible later proposals can allow sharing private declarations to friendly module.
Champions
- Justin Ridgewell (@jridgewell)
- Kevin Gibbons (@bakkot)
Status
Current Stage: 1
Guiding Use Cases
"Protected" state
Protected state is a valuable visibility state when implementing class hierarchies. For for instance, a hook pattern might be used to allow subclasses to override code:
Here, AttributeCommitter explicitly allows trusted (written in the
same source file) subclasses to override the behavior of the
#createPart method. By default, a normal AttributePart is returned.
But PropertyCommitter works only on properties and would return a
PropertyPart. All other code is free to be inherited via normal
publicly visible fields/methods from AttributeCommitter.
Note that this does not privilege code outside the file to override
the #createPart method, as the #createPart private declaration is
visible only in the scope where it is declared.
Friend classes/functions
For prior art in C++, see https://en.wikipedia.org/wiki/Friend_function.
The AMP Project has a particular staged linting pattern that works well
with friendly functions that guard access to restricted code. To begin
with, statically used functions are considerably easier to lint for than
object-scoped method calls because we do not need to know the objects
type. So its much easier to determine if this is a restricted call or
just a non-restricted call that uses the same method name. Eg, it's
easier to tell that a static export registerExtendedTemplate is
restricted vs obj.registerExtendedTemplate.
Non-class object factories
Not all code wants a class. Sometimes you still want private fields in such cases.