The structured clone algorithm copies complex JavaScript objects. It is used internally when invoking
, to transfer data between
via
, storing objects with
, or copying objects for
.
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 DataCloneError exception.
Cloning DOM nodes likewise throws a DataCloneError exception.
Certain object properties are not preserved: The lastIndex property of
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
, it will be read/write in the duplicate, since that's the default.
The prototype chain is not walked or duplicated.
are not duplicated. (Although internal fields of built-in types may.)
types (but see
below).
objects: but only plain objects (e.g., from object literals).
, except symbol.
: but note that lastIndex is not preserved.
Error types
For Error types, the error name must be one of:
,
,
,
,
,
,
(or will be set to "Error").
Browsers must serialize the properties name and message, and are expected to serialize other "interesting" properties of the errors such as stack, cause, etc.
support is expected to be added to the specification in
(and is already supported in some browsers).
: browsers must serialize the properties
and
. Other attributes may also be serialized/cloned.
Note: Serializable objects are marked up in
with the attribute [Serializable].
HTML Specification: Safe passing of structured data