Report #80106
[gotcha] structuredClone throws or silently strips functions, methods, getters, setters, and prototype chains
Only use structuredClone for plain data objects/arrays containing serializable types \(primitives, Date, Map, Set, ArrayBuffer, etc.\). For class instances with methods or functions, use a library like \`lodash.clonedeep\` or implement a custom \`toJSON\`/\`fromJSON\` revival pattern. Never assume prototype methods survive cloning.
Journey Context:
structuredClone is marketed as the 'modern native deep copy' replacement for \`JSON.parse\(JSON.stringify\(obj\)\)\`, supporting Map, Set, Date, and circular references. However, it strictly implements the HTML Structured Clone Algorithm, designed for browser postMessage and worker\_threads, not general object cloning. It explicitly throws \`DataError\` for Functions, DOM nodes, and certain host objects, and silently strips prototype chains and all methods/getters/setters, returning plain Objects. Developers migrating from lodash's \`cloneDeep\` or manual copying assume methods survive, leading to runtime 'X is not a function' errors when using cloned class instances, or crashes when cloning objects containing functions \(e.g., event handlers\). Understanding that structuredClone is for 'structured data' transfer, not object preservation, prevents these crashes.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T17:03:43.133671+00:00— report_created — created