The structured clone algorithm is a serialization algorithm used, among other things, to pass data between windows via window.postMessage. It supports recursive objects (unlike JSON) but not things like DOM Nodes, Functions, and Errors, and other
What I'd like is a simple way to check if a given object can be serialized by the structured clone algorithm. I could recursively walk the object and check if each property is a DOM Node, Function, or Error, but that's not a complete answer, and I was wondering if there was a better way.
From the spec, I think it would be something like
Note this has some limitations:
{}.toStringis not a reliable way to get the [[Class]], but is the only one.So it may be more reliable to attempt to run the algorithm, and see if it produces some error:
Note if you have a
messageevent listener, it will be called. If you want to avoid this, send the value to another window. For example, you can create one using an iframe: