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.
- Binding
- The connection between a name and the storage it refers to.
- Address
- A program-visible location of native storage, normally a virtual address on this OS. JavaScript object references do not expose raw addresses.
- Copy
- A separate value or outer record. Fields that refer to other data can still share that data; copying is not necessarily recursive.
- Capacity
- For the native contiguous collections here, the element count available without reallocating. JavaScript Array has no corresponding public capacity contract.
Storage, access, and identity
Storage is where a value is kept; access is how code reaches it. A second name can receive a separate value, access the same stored value or take over ownership, depending on the native type and operation. A copy of an outer record can still contain references to other shared data, so a binding diagram must show what is copied and what remains connected.
An address identifies a storage location during its valid lifetime. A collection position identifies a slot in the collection’s current order. Neither alone expresses which domain record an application means to keep using. Follow changes to the record, its storage and its position separately; the native examples establish which forms of access remain usable.
01Copies and sharing
Does a second name share the storage?
The booking command reports seat 20 booked for Ana, but the seat list still says free. Trace which record the command changed before keeping a seat for later confirmation. Then add seats while that hold is pending: does the saved name, address or position still reach the intended record?
Reduce the booking to two names and one field update. A binding connects a name to a value; some values provide access to shared storage. Track which Seat record each name reaches before deciding whether changing the second name updates the collection. This is a source-level storage model, not a measurement of physical addresses.
Numbers, strings and booleans are copied. Objects and arrays are reached through references: assigning one copies the reference.
// Two names, one assignment, one change.
type Seat = {
id: number;
holder: string;
};
const a: Seat = { id: 20, holder: "" };
const b = a;
b.holder = "Ana";
console.log(`a.holder = "${a.holder}"`);
console.log(`b.holder = "${b.holder}"`);
npx tsc main.ts --noEmit --strict && node main.ts
Put each example in the shown file and run the command. Two of the Rust examples are expected to be rejected by the compiler; the rejection is the result.
02Growth
What happens to a reference when its collection grows?
Once a booking reaches the stored record, the pending hold introduces another question: what if the collection changes before confirmation? Go slices, Rust Vec and C++ vector use contiguous element storage with a capacity. Their growth can replace that storage, while an insertion can shift positions even without reallocation. JavaScript arrays expose indexed elements and object identities without promising this physical representation.
The array elements provide access to objects. Inserting an element changes indexed positions while an existing object reference keeps its identity. The engine may relocate objects internally; JavaScript does not expose those raw addresses.
// Keep a reference to one seat, then insert another seat in front of it.
type Seat = {
id: number;
holder: string;
};
const seats: Seat[] = [{ id: 10, holder: "" }];
const held = seats[0];
seats.unshift({ id: 5, holder: "" });
held.holder = "Ana";
console.log(`held seat: ${held.id}, now at position ${seats.indexOf(held)}`);
console.log(
`seats: ${seats.map((seat) => `${seat.id} ${seat.holder || "free"}`).join(", ")}`
);
03Identity
How do you refer to stored data for later?
The confirmation step must reach the originally selected seat and inspect its current booking state. For this insert-only catalogue, every seat has a unique id that stays with it. Lookups can recover the current record after positions shift. A retained TypeScript object reference also survives these insertions because the program keeps the same objects; a detached copy does not receive later bookings.
| Stored | After an insert before it | After the storage grows |
|---|---|---|
| Position | Names a different seat | Still names the same position |
| Pointer or reference into the array | Go: with reused backing storage, the slot may hold another seat; after reallocation, the pointer stays in the old array. C++: invalidated at/after insert. Rust: a live element borrow blocks the mutation. | On reallocation: Go keeps the old array alive; C++ invalidates element pointers; safe Rust rejects a conflicting live borrow. |
| TypeScript object reference | Same object | Same object |
| Copy of the seat | Changes to it never reach the array | Same |
| The seat’s id | Look it up again | Look it up again |
04Practice
Finish the seat booking program
The lab is one theatre seat booking program. It reads commands from stdin, keeps seats in ascending id order, and prints what each command did. Each level supplies the parts that already work.
The seat booking program
Find a seat, book the stored seat, then keep a hold while seats are added.
In the frontend Check what keyed rows preserve when a selection stores a position. Optional
This diagnostic component starts with seat 30 held at position 1. Its rendered rows have stable keys. Add seat 20, then inspect the held-seat display: does a rendering key update the separate position stored by application code? The example is deliberately incomplete as a reservation design.
"use client";
import { useState } from "react";
export default function SeatSelection() {
const [seats, setSeats] = useState([{ id: 10 }, { id: 30 }]);
const [heldPosition, setHeldPosition] = useState(1);
return (
<>
<button
disabled={seats.some((seat) => seat.id === 20)}
onClick={() =>
setSeats((current) => [...current, { id: 20 }].sort((a, b) => a.id - b.id))
}
>
Add seat 20
</button>
<ul>
{seats.map((seat, index) => (
<li key={seat.id}>
<button onClick={() => setHeldPosition(index)}>Hold {seat.id}</button>
</li>
))}
</ul>
<p>Held seat: {seats[heldPosition]?.id}</p>
</>
);
}
React keys let reconciliation associate rendered rows with their identities across an insertion. They do not rewrite heldPosition. Before another click, the expression at position 1 now reads seat 20; trace the application selection separately from the row keys.
Sources: React: keeping list items in order with key,Svelte: keyed each blocks.
Sign in to keep your read marks on every device. Sign in
Was this helpful?