Report #17644
[gotcha] structuredClone strips prototype chains and methods from class instances
Rehydrate class instances after cloning by manually setting the prototype: \`const clone = structuredClone\(instance\); Object.setPrototypeOf\(clone, MyClass.prototype\);\` or implement custom toJSON/fromJSON methods instead of using structuredClone for class instances
Journey Context:
structuredClone \(and the HTML structured clone algorithm\) serializes data to a structured clone buffer. Per the specification, it explicitly discards prototypes and methods, cloning only 'serializable objects' as plain maps/records. When cloning a class instance, the result is a plain Object with the same own-properties, but it loses its \[\[Prototype\]\] linkage and all methods. This breaks instanceof checks and method calls \(e.g., clone.doWork\(\) throws TypeError\). Developers often assume structuredClone is a 'deep copy' that preserves type, but it is a serialization roundtrip that only preserves primitive data, typed arrays, Date, Map, Set, and a few other host objects. Manual prototype restoration or custom serialization \(toJSON/fromJSON\) is required for class fidelity.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T05:54:51.687143+00:00— report_created — created