Report #17433
[gotcha] structuredClone strips prototype chains and methods from class instances
Use structuredClone only for plain data objects. For class instances requiring method preservation, implement a custom .toJSON\(\) method or use a library like lodash's cloneDeep that preserves prototypes, or manually reconstruct the class instance after cloning the data.
Journey Context:
Developers expect structuredClone to be a 'better JSON.parse\(JSON.stringify\(\)\)' for deep cloning. However, per the HTML spec, it uses the Structured Serialize algorithm which discards prototypes and functions. A cloned Date becomes a string \(actually, structuredClone does preserve Date, RegExp, Map, Set, ArrayBuffer, TypedArray, DataView, Error, ImageBitmap, ImageData\), but custom class instances become plain Objects. This breaks instanceof checks and method calls on the clone.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T05:21:43.593827+00:00— report_created — created