liveBook · Manning

Read-write property

As explained in Secrets of the JavaScript Ninja, Third Edition

A writable property is a data property whose stored value may be changed through assignment when its writable attribute is true.

A writable property is a data property whose value can be changed by assignment. With Object.defineProperty, writable defaults to false, so make it true when the stored value should be assignable. section 9.1.2

Keep these descriptor rules in mind:

  • Assigning to a non-writable property throws an error in strict mode. section 9.1.2
  • A property that is writable but not configurable can change value, but its other property aspects cannot be changed. section 9.1.2
const obj = {};
Object.defineProperty(obj, "surfNinjaCount", {
  value: 3,
});

console.assert(
  obj.surfNinjaCount === 3,
  "There are 3 surf ninjas.",
);

This data property has a value but omits writable, so it uses the default and is not assignable. section 9.1.2

How Secrets of the JavaScript Ninja, Third Edition covers it

How deep it goes
Explains the default, strict-mode assignment behavior, and limits imposed by non-configurability.
Examples
Uses Object.defineProperty to create a value property without specifying writable.
What you can do after
Apply it via Object.defineProperty to control whether a data property accepts assignment.
How it connects
Connects data descriptors with Object.defineProperty, configurability, and accessor descriptors.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Secrets of the JavaScript Ninja, Third Edition. The section links point to the parts of the book it draws on.