Terminology (4)Terms used in this lesson
Term on the front, definition on the back. Paste with tabs between fields and a new line between cards, or import the download in Anki as Basic.
- Shallow object copy
- A new outer object that retains its existing nested references.
- Copy boundary
- The objects that a copy policy duplicates, with references beyond that boundary intentionally retained.
- Alias group
- Access paths identifying the same object, rather than merely equal object values.
- Selective fork
- A new structure that detaches a chosen shared object group while preserving specified live links elsewhere.
Copy depth and shared identity
A nested data structure connects objects through references or pointer-like values. A shallow copy duplicates an outer value while retaining its nested access paths. A deeper copy creates independent objects at additional levels; the operation and the field types determine where that boundary lies.
A copy policy must also account for intentional sharing. Two paths may reach the same object, while equal-looking objects may have separate identities. Decide which links remain live and which become local before copying a graph; copying more objects is not automatically the desired result.
01The copied level
A new outer record can still share a nested cell
A graphics document’s rows link to mutable palette swatches. Duplicate row keeps its library swatch linked; detach row gives one row its own swatch. Creating a document variant can instead make one selected swatch group local, keeping that group shared inside the variant while other library links stay live.
The graphics editor duplicates a row while keeping its library swatch linked. This reduced probe copies a row, edits its own code and then edits the swatch through it. Predict which original fields are affected.
type Cell = { value: number };
type Row = { code: number; cell: Cell };
const original: Row = { code: 3, cell: { value: 5 } };
const copy = { ...original };
copy.code = 9;
copy.cell.value = 7;
console.log("original", original.code, original.cell.value);
console.log("copy", copy.code, copy.cell.value);
npx tsc main.ts --noEmit --skipLibCheck --target es2022 --module nodenext --types node && node main.ts
Save one complete example at a time in a fresh folder under the selected filename. Run the shown checking/build command before execution. The browser lab supplies its editor, stdin fixtures and native runner.
02Copying another level
An independent cell requires another ownership boundary
Detach row needs a swatch that can be edited without changing the library. First isolate that allocation boundary: this probe copies one standalone Cell with its current value. Predict the original and copy after editing the copy.
type Cell = { value: number };
const original: Cell = { value: 5 };
const copy = structuredClone(original);
copy.value = 7;
console.log("original", original.value, "copy", copy.value);
04Practice
Repair the palette document
Repair the palette-link editor: duplicate a row while retaining its linked swatch, detach one row and swatch for an independent edit, then create a document variant with one chosen swatch group made local.
The numeric cell value is the editable swatch setting. Shared cells model intentional palette links. Unselected swatches remain linked to their original cells, and equal values alone never establish a link.
Start the lab
Copy the requested level, preserve a nested value independently, then implement the selective document boundary.
In the frontend Copy depth describes the data boundary; state publication is a separate framework step. Optional
A swatch preview can keep an earlier plain-data proof while the working numeric setting changes. This excerpt isolates one Cell; surrounding row and document sharing policies still need their own boundaries.
'use client';
import { useState } from 'react';
export function SwatchEditor() {
const [cell, setCell] = useState({ value: 5 });
function paint() {
setCell((previous) => ({ value: previous.value + 1 }));
}
return <button onClick={paint}>{cell.value}</button>;
}
The updater returns a new Cell object rather than mutating the previous state object. The current value is carried into that new object before the increment.
Sources: React: Updating objects in state,Svelte: state snapshots.
Sign in to keep your read marks on every device. Sign in
Was this helpful?