Report #24259
[gotcha] structuredClone\(\) on class instances returns plain objects losing methods and prototype chain
For class instances, implement a custom clone\(\) method using Object.create\(this.constructor.prototype\) and manual property copying; if using structuredClone for postMessage, pass a revival function or transfer the instance's data and reconstruct on the other side.
Journey Context:
The HTML spec's structured clone algorithm \(used for postMessage, IndexedDB, and structuredClone\) serializes to a structured format that intentionally discards prototypes and methods for security and cross-realm safety. This silently breaks class instances—methods disappear, instanceof checks fail. Alternatives like \_.cloneDeep or rfdc preserve prototypes and are faster but don't handle circular references or transferable objects like structuredClone. The right pattern depends on context: use structuredClone for structured data transfer across realms \(with manual class revival via Object.create\), use library deep clones for same-context cloning.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T19:07:31.569303+00:00— report_created — created