The structured clone algorithm - Web APIs 编辑
The structured clone algorithm copies complex JavaScript objects. It is used internally to transfer data between Workers via 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
Function
objects cannot be duplicated by the structured clone algorithm; attempting to throws aDATA_CLONE_ERR
exception.- Cloning DOM nodes likewise throws a
DATA_CLONE_ERR
exception. - Certain object properties are not preserved:
- The
lastIndex
property ofRegExp
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.
- The
Note: Native Error
types can be cloned in Chrome, and Firefox is working on it.
Supported types
Object type | Notes |
---|---|
All primitive types | However, not symbols. |
Boolean objects | |
String objects | |
Date | |
RegExp | lastIndex is not preserved. |
Blob | |
File | |
FileList | |
ArrayBuffer | |
ArrayBufferView | Including other typed arrays. |
ImageBitmap | |
ImageData | |
Array | |
Object | Only plain objects (e.g. from object literals) |
Map | |
Set |
See also
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论