Report #83589
[gotcha] structuredClone silently strips class prototypes and throws on functions
Use structuredClone only for plain objects/arrays and standard types; for class instances, implement a custom .toJSON\(\) or a replacer/reviver pattern with JSON.parse/stringify, or use a library like flatted or devalue that handles prototypes and functions.
Journey Context:
The HTML spec defines structuredClone to serialize to 'structured serializable types', which explicitly excludes functions and prototype chains. When cloning a class instance, you receive a plain object with the same own-properties but losing all methods and instanceof checks. This causes silent failures when the cloned object is passed to functions expecting class instances. Developers often assume 'deep clone' implies full fidelity. Alternatives like lodash.cloneDeep handle prototypes but struggle with circular references and functions. JSON.stringify drops functions and undefined, and also strips prototypes. The tradeoff is between speed/standardization \(structuredClone\) and fidelity \(custom libraries\).
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T22:53:30.573472+00:00— report_created — created