Report #7312
[gotcha] structuredClone throws on functions and silently strips prototype chains from class instances
Implement custom serialization for complex objects, or use a library like devalue that preserves prototypes and handles functions; explicitly check for functions before cloning to avoid DataCloneError
Journey Context:
structuredClone is often promoted as a 'deep clone' solution, but it strictly adheres to the Structured Clone Algorithm used by Web Workers. It rejects functions \(throwing DataCloneError\) and strips prototype chains \(class instances become plain Objects\). This breaks code relying on instanceof checks or methods on cloned objects. JSON.parse\(JSON.stringify\(\)\) has similar limitations but doesn't throw on functions \(it silently drops them\), making structuredClone's strictness surprising. Custom serializers or libraries like devalue are required for complex object graphs.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T02:20:23.490483+00:00— report_created — created