liveBook · Manning

[[Writable]]

As explained in JavaScript in Depth

An internal attribute of a data property descriptor that determines whether the property's value may be changed.

A failed assignment can look mysterious: `obj.foo = 'bar'` either changes nothing or throws. [[Writable]] supplies the missing diagnosis. It is the boolean internal attribute of a data-property descriptor that determines whether the property's stored [[Value]] may change. It applies only to data properties; accessor properties use [[Get]] and [[Set]] instead. section 5.2 section 5.2.3

Use the descriptor APIs to inspect the rule and interpret its observable effects:

  • `Object.getOwnPropertyDescriptor(object, name)` exposes `value` and `writable`, identifying a data property and its mutability. section 5.2.3
  • With `writable: false`, assignment silently fails in sloppy mode but throws an error in strict mode; the stored value remains unchanged. section 5.2.3
  • With `writable: true`, assignment may update the stored value. `writable` controls the value, whereas `configurable` controls whether the descriptor can later be changed or deleted. section 5.2.3

A deliberately read-only identifier

const drawer = { contents: 'tax documents' }; Object.defineProperty(drawer, 'internalId', { value: 12345, writable: false }); drawer.contents = 'invoices'; drawer.internalId = 54321; `contents` changes, but `internalId` does not—and strict mode determines whether the latter assignment is silent or exceptional. section 5.2.3

How JavaScript in Depth covers it

How deep it goes
Explains descriptor classification, API inspection, strict/sloppy outcomes, and the distinction from configurability.
Examples
Uses Object.getOwnPropertyDescriptor and Object.defineProperty with mutable contents and a non-writable internal identifier.
What it assumes
Assumes basic JavaScript objects and assignments, while defining property descriptors and data-versus-accessor properties.
What you can do after
Diagnose property-assignment failures and configure data-property mutability with standard Object APIs.
How it connects
Connects data properties with property descriptors, [[Value]], strict mode, sloppy mode, accessors, and [[Configurable]].

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.