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 getter is a specialized method that binds an object property to a function, automatically invoking that function when the property is accessed. It allows a property to be computed or retrieved dynamically while maintaining the syntactic interface of a standard data property. Under the hood, getters are implemented as accessor properties rather than data properties. In the property descriptor, a getter utilizes the get attribute instead of the value and writable attributes.

Syntax in Object Literals

You define a getter within an object literal using the get keyword followed by the property name.
const entity = {
  _identifier: 'A100',
  
  get id() {
    return this._identifier;
  }
};

// Accessed as a property, not invoked as a method
console.log(entity.id); // 'A100'

Syntax in Classes

Getters are defined in ES6 classes using the exact same get syntax. They are typically paired with private fields (#) or convention-based private properties (_).
class Sensor {
  #reading = 25.5;

  get temperature() {
    return this.#reading;
  }
}

const thermostat = new Sensor();
console.log(thermostat.temperature); // 25.5

Dynamic Definition via Object.defineProperty

To attach a getter to an existing object, you must use Object.defineProperty() and explicitly define the get function within the property descriptor.
const state = { _status: 'active' };

Object.defineProperty(state, 'status', {
  get: function() {
    return this._status;
  },
  enumerable: true,
  configurable: true
});

Technical Constraints and Behavior

  • Zero Arity: A getter method must have exactly zero parameters. Attempting to define a getter with parameters will throw a SyntaxError.
  • Invocation: Getters are invoked implicitly via property lookup (object.property). Appending parentheses (object.property()) will result in a TypeError unless the getter itself returns a function.
  • Assignment and Read-Only Behavior: Defining a getter without a corresponding setter makes the property read-only. Attempting to assign a value to a getter-only property fails silently in non-strict mode and throws a TypeError in strict mode (which implicitly includes all ES6 classes).
  • Naming Collisions: In modern JavaScript (ES6+), defining a getter and a data property with the same name in an object literal does not throw a SyntaxError; the last defined property simply overwrites the previous one. However, attempting to define both a get accessor and a value (or writable) data property simultaneously via Object.defineProperty throws a TypeError.
  • Memoization / Smart Getters: Because getters are evaluated on access, properties can be lazily evaluated. A getter can redefine itself as a standard data property upon first execution to cache the result.
  • Deletion: Getters are removed from an object using the standard delete operator (delete object.propertyName).
Master JavaScript with Deep Grasping Methodology!Learn More