Skip to main content
A static private method is a function bound to a class constructor rather than its instances, encapsulated such that it can only be invoked from within the lexical scope of the class declaration. It combines the class-level execution context of the static keyword with the hard privacy enforcement of the # prefix.

Syntax

To declare a static private method, precede the method name with the # identifier and the static modifier.

Mechanics and Execution Context

Lexical Scoping and Access Static private methods are strictly scoped to the class body. Attempting to access or invoke the method from outside the class declaration results in a SyntaxError during the parsing phase. They cannot be accessed dynamically via bracket notation (e.g., this['#method']). The this Binding Inside a static private method, the this keyword evaluates to the class constructor itself, not an instance of the class. Invocation Within the class, the method can be invoked by other static methods using this.#methodName(). If invoked from an instance method, it must be called explicitly on the class constructor (e.g., ClassName.#methodName()), because this inside an instance method refers to the instantiated object, which does not possess the static method.

Code Demonstration

Subclassing and Inheritance Behavior

Static private methods are not inherited by subclasses. They are permanently bound to the lexical environment of the declaring class. This creates a specific behavior regarding the this context when subclasses invoke inherited static public methods that internally reference static private methods:
To prevent this TypeError in inheritance chains, static public methods must explicitly reference the base class constructor (e.g., Base.#privateStatic()) rather than relying on this.#privateStatic(), ensuring the receiver is always the class that declared the private method.
Tired of Poor JavaScript Skills? Fix That With Deep Grasping!Learn More