Skip to main content
A static private field is a class-level property encapsulated entirely within the lexical scope of its defining class. It is evaluated once when the class is initialized, belongs to the class constructor itself rather than any instance, and is strictly inaccessible from outside the class body, including from subclasses or instantiated objects.

Syntax

Static private fields are declared using the static keyword followed by the # prefix (the hash or pound sign), which denotes the private identifier.

Access and Scope Rules

Internal Access Within the class body, static private fields can be accessed either by referencing the class name directly or by using this inside static methods (where this refers to the class constructor).
External Access Attempting to access or modify a static private field from outside the class body results in a SyntaxError. This is an early error, meaning the JavaScript engine will fail to parse the script before execution begins.

Inheritance and the this Context

Static private fields are not inherited by subclasses. Because private fields are lexically scoped to the exact class that defines them, accessing them via this in an inherited static method requires strict attention to the execution context. If a subclass invokes an inherited static method that references a static private field via this, a TypeError is thrown. This occurs because this points to the subclass, which does not possess the parent’s private brand.
To safely access a static private field in methods that might be inherited, developers must reference the defining class explicitly rather than relying on this.

Ergonomic Brand Checks

The in operator can be used to determine if a specific static private field exists on a given object or class. This evaluates to a boolean and does not throw a TypeError if the field is missing.
Tired of Poor JavaScript Skills? Fix That With Deep Grasping!Learn More