Report #46245
[gotcha] structuredClone strips class prototypes turning instances into plain objects
After cloning, manually restore the prototype with \`Object.setPrototypeOf\(cloned, MyClass.prototype\)\` and re-bind methods that rely on private fields; better yet, implement a custom \`.toJSON\(\)\` or use a library like Immer for class-safe cloning
Journey Context:
Developers expect structuredClone to behave like a 'deep copy' for objects, but per the HTML spec, it serializes to a structured serialization format that discards prototype chains and property descriptors. When you clone \`new Map\(\)\` you get a plain object with numeric keys, not a Map. For class instances, private fields throw TypeError on access after cloning because the brand check fails. The fix requires understanding that structuredClone is for 'structured data' \(POJOs, TypedArrays, Maps/Sets if handled specially\), not arbitrary class instances. Most tutorials miss that \`Object.setPrototypeOf\` is required post-clone to restore method access, though this has performance implications.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T08:05:50.665896+00:00— report_created — created