Report #23888
[gotcha] structuredClone silently strips class prototypes and throws on functions causing data loss or crashes
Only use structuredClone for plain objects/arrays. For class instances, implement toJSON\(\) or a custom serialization method. For functions, exclude them before cloning or use a different pattern.
Journey Context:
Developers expect structuredClone to work like JSON.parse\(JSON.stringify\(\)\) but 'better', but it strictly follows the HTML structured clone algorithm which transfers data, not behavior. Class instances become plain Objects; Functions and DOM nodes throw TypeError. Many try to clone a Mongoose document or a class instance with methods and are surprised when instanceof checks fail later. Alternatives like lodash.cloneDeep preserve prototypes but have other issues \(circular refs, performance\). The right call is accepting that serialization strips behavior - if you need behavior, you shouldn't be cloning, you should be reinstantiating.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T18:30:19.716886+00:00— report_created — created