structured clone algorithm copies complex JavaScript objects. It is used internally to transfer data between 工作者 凭借 postMessage() , storing objects with IndexedDB , or copying objects for other APIs . It clones by recursing through the input object while maintaining a map of previously visited references, to avoid infinitely traversing cycles.

Things that don't work with structured clone

  • 函数 objects cannot be duplicated by the structured clone algorithm; attempting to throws a DATA_CLONE_ERR 异常。
  • Cloning DOM nodes likewise throws a DATA_CLONE_ERR 异常。
  • Certain object properties are not preserved:
    • lastIndex property of RegExp objects is not preserved.
    • Property descriptors, setters, getters, and similar metadata-like features are not duplicated. For example, if an object is marked readonly with a property descriptor , it will be read/write in the duplicate, since that's the default.
    • The prototype chain is not walked or duplicated.

注意 : Native Error types can be cloned in Chrome, and Firefox is working on it .

Supported types

Object type 注意事项
All primitive types However, not symbols.
布尔 对象
String 对象
日期
RegExp lastIndex is not preserved.
Blob
File
FileList
ArrayBuffer
ArrayBufferView Including other typed arrays .
ImageBitmap
ImageData
数组
对象 plain objects (e.g. from object literals)
地图
Set

另请参阅

元数据

  • 最后修改: