Skip to curriculum
Jump to the editor

5.5. A property that can be set only once

Medium
Topics
15–30 min

Some values are decided once and must never change afterwards: an order's id once the server has assigned it, the user a session belongs to, a configuration value read at start-up. A later write to them is always a bug, and the earlier it is caught, the easier it is to find. A property can enforce that itself: the first assignment is stored, and any later one throws.

Write defineWriteOnce(object, key) in write-once.js. It adds a property key to object that can be assigned exactly once.

Signature

JAVASCRIPT
function defineWriteOnce(object, key) // returns object
  • object: the object to add the property to. It does not already have key.
  • key: the property name.
  • Returns object itself.

Requirements

  1. The property exists right away (key in object is true) and reads as undefined until it is set. Reading never counts as setting.
  2. The first assignment stores its value.
  3. Every later assignment throws a TypeError whose message names the property, and the first value stays. Assigning the same value again also throws.
  4. The first assignment counts whatever its value is: after object.id = undefined (or null, 0, "", false, NaN), the next assignment throws.
  5. The rule holds in strict-mode code and sloppy code alike, and for Object.assign.
  6. The property is enumerable (Object.keys, JSON.stringify), and it cannot be deleted or redefined with Object.defineProperty.
  7. Each property has its own state: two keys on one object, or the same key on two objects, are independent.
  8. No other property is added to the object.

Examples

JAVASCRIPT
const order = defineWriteOnce({ total: 250 }, "id");
order.id;          // undefined
order.id = "A-17";
order.id;          // "A-17"
order.id = "B-02"; // TypeError: "id" can be set only once
order.id;          // "A-17"
JAVASCRIPT
const session = defineWriteOnce({}, "user");
session.user = undefined; // counts as the one assignment
session.user = "asha";    // TypeError
JAVASCRIPT
delete order.id;                                   // false: still "A-17"
Object.defineProperty(order, "id", { value: 1 });  // TypeError
JSON.stringify(order);                             // '{"total":250,"id":"A-17"}'

Environment

  • Plain JavaScript in a browser page; the tests call the global defineWriteOnce from write-once.js.
  • Every built-in is allowed. Object.freeze on the whole object is not a solution: other properties must stay writable.

Hints

  1. A non-writable data property ignores writes silently in sloppy code. What kind of property can throw on every write?
  2. How do you remember "it was set" when the value that was set is undefined?

The explanation comes with the course

A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.

  • Every line explained
  • The mistakes each test catches
  • Links to the lessons behind it
Back to the question
Loading the editor…