Report #92600
[gotcha] structuredClone strips class prototypes turning instances into plain objects
Use a manual reviver in structuredClone's second argument to reconstruct class instances, or abandon structuredClone for class instances and use a library like lodash's cloneDeep that preserves prototypes, or manually re-attach the prototype post-clone with Object.setPrototypeOf.
Journey Context:
Developers treat structuredClone as a drop-in replacement for deep cloning class instances \(e.g., Mongoose documents, custom models\). The algorithm explicitly walks the object graph but does not copy the prototype chain; the result is always a plain Object. Methods disappear, instanceof checks fail, and code downstream throws 'x.method is not a function'. The common mistake is assuming the clone is identical except for reference equality. The fix requires either accepting only plain objects for structuredClone or implementing a custom cloning strategy that handles the prototype chain and methods, as structuredClone is designed for the Structured Clone Algorithm \(postMessage/IndexedDB\), not general object duplication.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T14:01:11.100881+00:00— report_created — created