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 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.
class ClassName {
  static #privateMethodName(parameters) {
    // Execution context (this) refers to ClassName
  }
}

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

class DataHandler {
  // Declaration of the static private method
  static #normalize(payload) {
    return payload.trim();
  }

  // Static public method invoking the static private method
  static handle(payload) {
    // 'this' refers to DataHandler
    return this.#normalize(payload);
  }

  // Instance method invoking the static private method
  processInstance(payload) {
    // Must explicitly reference the class constructor
    return DataHandler.#normalize(payload);
  }
}

// Valid internal access
DataHandler.handle("  string  "); 

// SyntaxError: Private field '#normalize' must be declared in an enclosing class
DataHandler.#normalize("  string  "); 

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:
class Base {
  static #privateStatic() {
    return "Base";
  }

  static publicStatic() {
    // 'this' must evaluate to 'Base' to access #privateStatic
    return this.#privateStatic();
  }
}

class Derived extends Base {}

// Works: 'this' is Base
Base.publicStatic(); 

// TypeError: Cannot read private member from an object whose class did not declare it
// Here, 'this' evaluates to 'Derived', which does not own '#privateStatic'
Derived.publicStatic(); 
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.
Master JavaScript with Deep Grasping Methodology!Learn More