Report #68387
[gotcha] structuredClone strips class prototypes and methods from objects
Use structuredClone only for plain data transfer \(postMessage, IndexedDB\). For class instances, implement a toJSON\(\) method or custom replacer/reviver, or manually reconstruct the class after cloning by passing the plain object to the constructor.
Journey Context:
structuredClone is the standard way to deep clone objects in modern JS, but per the HTML spec, it only clones 'serializable objects' by value, not by reference. It intentionally discards prototype chains, getters, setters, methods, and private fields. This is a security and isolation feature for Workers/IndexedDB, but developers often try to use it to duplicate class instances \(e.g., new Date\(\) works because it's a built-in type, but custom classes become plain objects\). The fix is to recognize that structuredClone is for 'structured data' \(POJOs, Arrays, typed arrays, Dates, Maps, Sets\) transfer, not for object-oriented duplication. For OOP patterns, use manual copying or serialization strategies.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T21:16:11.801533+00:00— report_created — created