Report #29470
[gotcha] structuredClone throws on functions and silently strips prototypes and methods from class instances
Do not use structuredClone for class instances containing methods or custom prototypes. Instead use lodash.cloneDeep for prototype preservation, or manually reconstruct instances post-clone using Object.create with the prototype and Object.assign for data properties.
Journey Context:
The Structured Clone Algorithm \(used by \`structuredClone\(\)\`, \`postMessage\(\)\`, and IndexedDB\) serializes objects by copying the data property descriptor map but explicitly discards prototypes, methods \(functions\), and property descriptors \(getters/setters\). When you pass a class instance through \`structuredClone\`, the result is a plain object \(\`Object.prototype\`\) with only enumerable data properties. This causes \`instanceof\` checks to fail and methods to disappear, leading to 'x is not a function' errors downstream. The alternative \`lodash.cloneDeep\` preserves prototypes by traversing the prototype chain, though it is slower and has its own issues with circular references. For maximum control, manual reconstruction using \`Object.create\(original.prototype\)\` followed by \`Object.assign\` is the most predictable pattern for class instances.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T03:51:28.677535+00:00— report_created — created