liveBook · Manning

Accessor descriptor

As explained in JavaScript in Depth

A JavaScript property whose getter or setter code runs when the property is read or written instead of directly storing and retrieving a value.

When `obj.contents = value` changes hidden state instead of storing `value`, the property is an accessor: reading or writing it executes code. This explains silent assignments, computed results, and unexpected side effects. section 5.2.2 section 5.2

An accessor descriptor uses `get` and `set`; it has no `value` or `writable` attributes. `enumerable` and `configurable` still govern visibility and descriptor changes. Object literals and `Object.defineProperty()` create different default attributes, so inspect unfamiliar behavior with `Object.getOwnPropertyDescriptor()`. section 5.2.3 section 5.2

Use these distinctions when reasoning about accessor behavior:

  • A getter computes and returns a value when the property is read; a setter receives the assigned value and may update other state. section 5.2.2
  • In a class, instance data fields are own properties, while accessors defined by the class are shared through the prototype. section 6.2.4
  • Treat accessors deliberately: property syntax can conceal calculations, validation, mutation, or other observable work. section 5.2.2

Computed drawer contents

A getter reports the number of stored documents, while the setter appends a string rather than replacing a `contents` value. Reading `dynamicDrawer.contents` therefore changes from `3 documents` to `4 documents` after an assignment. section 5.2.2

Table 5.1 The property descriptor attributes and the property types they apply to:
Name
Description
Data Properties
Accessor Properties
[[Value]]
A stored JavaScript value
✓
 
[[Writable]]
A boolean indicating whether [[Value]] can be changed
✓
 
[[Get]]
Code used to compute the property value
 
✓
[[Set]]
Code used to modify the property value
 
✓
[[Enumerable]]
A boolean indicating whether the property can appear in iterations
✓
✓
[[Configurable]]
A boolean indicating whether the property descriptor itself can be changed or deleted
✓
✓

Compare accessor attributes with data-property attributes, especially `get`/`set` versus `value`/`writable`. section 5.2

How JavaScript in Depth covers it

How deep it goes
Descends from getter and setter intuition to descriptor attributes and prototype placement, but not engine implementation.
Examples
Uses a computed drawer, `Object.defineProperty()`, descriptor inspection, and class accessors to expose runtime behavior.
What it assumes
Assumes basic JavaScript objects and properties while establishing data properties, accessor properties, and descriptor vocabulary.
What you can do after
Diagnose/debug with it by inspecting descriptors and distinguishing computed access from stored values.
How it connects
Connects accessor descriptors with property descriptors, object prototypes, classes, and object inspection.

Also explained in

Compare how 2 books explain accessor descriptor →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of JavaScript in Depth. The section links point to the parts of the book it draws on.