Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

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.
class Identifier {
  static #privateStaticField = 'initial_value';
  static #uninitializedField;
}

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).
class Processor {
  static #maxThreads = 4;

  static getThreads() {
    // Access via 'this' in a static context
    return this.#maxThreads; 
  }

  static resetThreads() {
    // Access via explicit class reference
    Processor.#maxThreads = 4; 
  }
}
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.
class Processor {
  static #maxThreads = 4;
}

// SyntaxError: Private field '#maxThreads' must be declared in an enclosing class
console.log(Processor.#maxThreads); 

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.
class Base {
  static #baseId = 100;

  static getId() {
    return this.#baseId;
  }
}

class Derived extends Base {}

Base.getId();    // Returns 100
Derived.getId(); // TypeError: Cannot read private member #baseId from an object whose class did not declare it
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.
class Base {
  static #baseId = 100;

  static getSafeId() {
    return Base.#baseId; // Lexically bound, immune to subclass 'this' context
  }
}

class Derived extends Base {}

Derived.getSafeId(); // Returns 100

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.
class Validator {
  static #token = 'xyz';

  static hasToken(obj) {
    return #token in obj;
  }
}

Validator.hasToken(Validator); // true
Validator.hasToken(Object);    // false
Master JavaScript with Deep Grasping Methodology!Learn More