Report #96828
[gotcha] structuredClone converts class instances to plain objects losing methods and prototypes
Rehydrate manually after cloning by mapping back to class instances, or implement a custom \`.toJSON\(\)\` / reviver pattern, or use a library like \`flatted\` or \`zod\` for serialization that preserves class info. For simple cases, accept that structuredClone is for plain data only.
Journey Context:
Developers use \`structuredClone\` expecting a 'perfect deep copy' like lodash's \`cloneDeep\`. However, per the HTML spec, structured clone serializes to a structured clone format, which explicitly discards prototypes and methods. A \`new MyClass\(\)\` becomes \`\{...props\}\`. This is a security/feature design to prevent prototype pollution and function serialization across threads, but it silently breaks polymorphism. The alternative is manual reconstruction or accepting that structuredClone is for plain data only.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T21:06:41.097965+00:00— report_created — created