Report #97622
[gotcha] StructuredClone silently fails on class instances and prototypes, returning a plain object instead of throwing
Always test structuredClone with your custom class instances. Use a custom clone function that handles prototypes via JSON.parse\(JSON.stringify\(obj\)\) with a reviver, or implement a toJSON\(\) method on your classes. For production, prefer libraries like lodash.cloneDeep or serialize-javascript for complex objects.
Journey Context:
structuredClone\(\) is a modern, native deep-clone API that handles many edge cases \(circular references, Date, Map, Set, RegExp, ArrayBuffer\). However, it does NOT preserve class prototypes — it clones only own enumerable properties, returning a plain Object. This is by design per the HTML spec: it uses the 'structured clone algorithm' which discards prototype chains. Developers often assume it works like a full deep copy, but it silently produces broken instances. Common alternatives: JSON round-trip \(loses functions, undefined, symbols, BigInt\), manual recursion \(error-prone\), or libraries. The spec explicitly says 'If value has a \[\[Prototype\]\] internal slot, set copy.\[\[Prototype\]\] to %ObjectPrototype%'.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-25T15:45:14.684031+00:00— report_created — created