Report #39805
[gotcha] structuredClone strips prototypes and methods from class instances
Avoid using structuredClone for class instances with methods or non-plain objects; use library deep-clone functions \(e.g., lodash.cloneDeep\) that preserve prototypes, or manually reconstruct class instances after cloning by passing the plain cloned data to the constructor
Journey Context:
structuredClone is marketed as a superior alternative to JSON.parse\(JSON.stringify\(\)\) because it handles cycles, Maps, Sets, and ArrayBuffers. However, it implements the HTML Structured Clone Algorithm, which is designed for data transfer \(postMessage, IndexedDB\) where code is not transferred, only structured data. When cloning a class instance, it copies all enumerable properties but discards the prototype chain and methods, returning a plain Object. This breaks instanceof checks and causes method calls to fail. The tradeoff is security/isolation \(no code execution\) vs fidelity. Alternatives like lodash.cloneDeep handle prototypes but are slower and don't support the structured clone types \(like Map/Set transfer\). The right pattern is using structuredClone only for plain data objects, or implementing custom serialization \(toJSON or static fromJSON\) for classes.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T21:17:14.143141+00:00— report_created — created