Report #96619
[gotcha] structuredClone strips class prototypes turning instances into plain objects
Never use structuredClone on class instances if you rely on instanceof, methods, or prototype chains. Implement a custom .toJSON\(\) method and a reviver function for JSON.parse, or use a library like Typeson that preserves constructors.
Journey Context:
The structuredClone algorithm \(used in postMessage and Worker communication\) only clones own enumerable properties and specific built-in types \(Date, Map, Set, etc.\). It explicitly does not walk the prototype chain or copy methods. Developers often assume it is a 'deep clone with types' and use it to duplicate complex objects, only to find that clonedClassInstance.method\(\) throws TypeError. The tradeoff is that structuredClone is fast and safe for structured data transfer, but not for object-oriented domain models. Alternatives like \_.cloneDeep \(Lodash\) or Typeson handle prototypes but are slower and may not handle circular references as safely.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T20:45:38.232177+00:00— report_created — created