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 getter in JavaScript is a class-level accessor method prefixed with a hash (#) that dynamically computes and returns a value. It is bound to the class constructor itself rather than its instances, and its invocation is strictly encapsulated within the lexical scope of the class body. Because it is a getter, it is accessed as a property lookup rather than a function call. Because it is static, its internal this context refers to the class constructor. Because it is private, the JavaScript engine enforces hard privacy, throwing a SyntaxError if access is attempted from outside the class definition.
class ClassName {
  static get #identifier() {
    // execution logic
    return computedValue;
  }
}

Technical Characteristics

  • Lexical Scoping: The # identifier is resolved lexically. The getter can only be referenced by code physically located within the class {} block.
  • Storage and Binding: According to the ECMAScript specification, a static private getter is stored in the [[PrivateElements]] internal slot of the class constructor function object. This contrasts with private instance getters, which are installed in the [[PrivateElements]] internal slot of individual instances, and public instance getters, which are defined on the class prototype.
  • Context (this): When invoked, the this keyword inside the static private getter evaluates to the class constructor itself, allowing access to other static private or public members.
  • Non-writable: A getter without a corresponding setter creates a read-only property. Attempting to assign a value to a static private getter throws a TypeError.

Syntax and Invocation Mechanics

To invoke a static private getter, you must reference it via the class name (or this if within another static method) from inside the class body.
class SystemNode {
  static #baseId = 1000;

  // 1. Definition of the static private getter
  static get #nodePrefix() {
    // 'this' refers to SystemNode
    return `SYS_${this.#baseId}_`; 
  }

  // 2. Internal access via a static method
  static getIdentifier() {
    // Accessed as a property, no parentheses
    return SystemNode.#nodePrefix; 
  }

  // 3. Internal access via an instance method
  revealPrefix() {
    // Instances must explicitly reference the class constructor
    return SystemNode.#nodePrefix; 
  }
}

console.log(SystemNode.getIdentifier()); // "SYS_1000_"

const node = new SystemNode();
console.log(node.revealPrefix());        // "SYS_1000_"

// 4. External access throws a SyntaxError during parsing
// console.log(SystemNode.#nodePrefix); 
// SyntaxError: Private field '#nodePrefix' must be declared in an enclosing class

Memory and Reflection

Static private getters are not observable via standard JavaScript reflection mechanisms.
  • They do not appear in Object.getOwnPropertyNames(ClassName).
  • They do not appear in Object.getOwnPropertySymbols(ClassName).
  • They cannot be accessed dynamically using bracket notation (e.g., ClassName['#nodePrefix'] evaluates to undefined looking for a public string key, rather than accessing the private getter).
Master JavaScript with Deep Grasping Methodology!Learn More