Hard Object References: A Discipline to Eliminate Stale Aliases in JavaScript
In JavaScript and TypeScript, replacing object or array references instead of mutating them in place can create stale aliases, where one part of a program holds an outdated reference while another has moved on to a new object. The Hard Object References discipline addresses this by keeping object and array references stable throughout their lifetime, copying new data into existing objects rather than rebinding variables. The approach recommends declaring object and array variables with const to signal that the reference itself should not change, even though the underlying data may still be mutated. This principle applies broadly — not just to global application state, but also to local variables, nested fields, component state, and short-lived draft objects. Tools like HardObject.set are proposed to copy data into existing objects in place, ensuring that all parts of a program consistently observe the same, up-to-date reference.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.

Discussion (0)
Log in to join the discussion and vote.
Log in