Report #31545
[gotcha] structuredClone strips prototype chains and functions, silently turning class instances into plain objects
Do not use structuredClone to copy class instances that rely on methods. Implement custom toJSON and fromJSON/static revive methods, or use a library like lodash's cloneDeep for in-process cloning that preserves prototypes. For cross-realm communication \(workers\), manually reconstruct the class instance after receiving the plain object.
Journey Context:
structuredClone is promoted as a 'better JSON.parse\(JSON.stringify\(\)\)' that handles cycles, Maps, Sets, and ArrayBuffers. Developers expect it to deep-clone their ES6 class instances. However, the HTML spec's Structured Serialize Internal algorithm explicitly walks the object and copies 'serializable objects' by value, creating new plain objects for non-builtin types. This strips the prototype chain and discards function properties. The silent failure mode is particularly dangerous: the cloned object passes 'typeof' checks and has the same data properties, but method calls throw 'is not a function'. This breaks dependency injection containers, ORM entities, and domain models that are cloned for transactional safety.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T07:20:03.020549+00:00— report_created — created