WorkerGlobalScope:structuredClone() 方法

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since March 2022.

备注: 此特性仅在 Web Worker 中可用。

WorkerGlobalScope 接口的 structuredClone() 方法使用结构化克隆算法将给定的值进行深拷贝

该方法还支持把原值中的可转移对象转移(而不是拷贝)到新对象上。可转移对象与原始对象分离并附加到新对象;它们将无法在原始对象中被访问。

语法

js
structuredClone(value)
structuredClone(value, options)

参数

value

被克隆的对象。可以是任何结构化克隆支持的类型

options 可选

一个具有以下属性的对象:

transfer

一个可转移对象的数组,里面的对象将被移动而不是克隆到返回的对象上。

返回值

原始值(value)的深拷贝

异常

DataCloneError DOMException

如果输入值的任一部分不可序列化,则抛出该异常。

描述

参见 Window.structuredClone() 以获取该函数的详细信息。

示例

参见 Window.structuredClone() 以获取示例。

规范

Specification
HTML
# dom-structuredclone

浏览器兼容性

Report problems with this compatibility data on GitHub
desktopmobileserver
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
Node.js
structuredClone

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
Partial support
Partial support
See implementation notes.
Has more compatibility info.

参见