Reference
Which tool copies a value — and what you actually get. Companion to lesson 1.
| Hard problem | Default tool |
|---|---|
| New outer object; nested refs may stay shared | Shallow copy |
| A JSON document (HTTP body, file, log) | JSON — design for loss |
| Same-realm snapshot; nested identity must break |
structuredClone()
|
| Value already crossing a platform API that clones | Implicit clone — don’t double-copy |
| Large binary; sender must give up the bytes | Transfer |
| Methods, class identity, versioned wire, revival | Custom serializer |
| Value | JSON round-trip | Structured clone |
|---|---|---|
| Plain objects / arrays / primitives | OK (no symbols) | OK (no symbol primitives) |
Date |
ISO string via toJSON |
Still a Date |
Map / Set |
Become {} (silent) |
Preserved |
| Cycles | TypeError |
Preserved |
undefined on an object |
Key dropped | Kept |
| Function / DOM node | Dropped or null (silent) |
DataCloneError |
ArrayBuffer / typed arrays |
Useless / object-shaped | Cloned (or transferred) |
| Class instance methods / prototype | Lost | Lost (plain data) |
JSON behavior:
MDN JSON.stringify. Clone types:
MDN structured clone.
Worker.postMessage / window.postMessage —
clone; optional transfer list
history.pushState / replaceState — clonestructuredClone() — explicit, same algorithm