Sub topic 3
Class Instances, Getters and Other Things That Do Not Survive
This chapter assumes you already know how structuredClone copies plain data, Date, Map and Set, and that you can run snippets in Node.js 17+ or a modern browser console.
What the copy leaves behind
structuredClone copies data, not behavior. It reads each object's own enumerable string-keyed properties and writes them onto a new plain object. Because of that, several things are lost:
- Prototypes: class instances come back as plain objects, so their methods disappear.
- Getters and setters: the getter runs once, and its result is stored as a plain value.
- Non-enumerable properties and property descriptors: these are skipped or reset.
- Symbol keys: these are dropped.
class Point {
constructor(x, y) { this.x = x; this.y = y; }
get length() { return Math.hypot(this.x, this.y); }
}
const copy = structuredClone(new Point(3, 4));
console.log(copy instanceof Point); // false
console.log(copy); // { x: 3, y: 4 }
const withGetter = structuredClone({ get answer() { return 42; }, [Symbol('s')]: 1 });
console.log(Object.getOwnPropertyDescriptor(withGetter, 'answer').value); // 42
console.log(Object.getOwnPropertySymbols(withGetter).length); // 0To get a class instance back, rebuild it yourself after cloning, for example with new Point(copy.x, copy.y).
Where it is available
structuredClone is a global in current browsers (since 2022), Node.js 17+, Deno and Bun. Older runtimes and some embedded engines lack it, so check for it before you rely on it:
function deepCopy(value) {
if (typeof structuredClone === 'function') {
return structuredClone(value);
}
throw new Error('structuredClone is not available in this runtime');
}Moving instead of copying
The transfer option moves an ArrayBuffer's memory into the result without copying the bytes. After the move, the original buffer is detached and has a length of zero.
const buffer = new ArrayBuffer(8);
const moved = structuredClone(buffer, { transfer: [buffer] });
console.log(moved.byteLength); // 8
console.log(buffer.byteLength); // 0The same algorithm elsewhere
postMessage (used with workers, iframes and MessageChannel) and IndexedDB storage use the same structured clone algorithm. The rules from this tutorial apply there too: functions throw a DataCloneError, prototypes are lost, and postMessage accepts a transfer list.
Choosing a strategy
| Data shape | Good choice |
|---|---|
| Flat object, or nested data you won't mutate | Spread / `Object.assign` |
| Plain JSON-safe data you are about to serialize anyway | JSON round-trip |
| Nested data with Date, Map, Set, typed arrays or cycles | `structuredClone` |
| Class instances, functions, DOM nodes | A custom clone method |
Exercise
You clone an object holding a User class instance and a Map of settings. Which part keeps its type, and how do you restore the other?
Show answer
The Map stays a Map with its entries copied. The User becomes a plain object without its methods, so you rebuild it, for example with new User(copy.user.name) or a static User.from(copy.user) factory.