Skip to content

Instantly share code, notes, and snippets.

@glebcha
Created July 18, 2018 11:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save glebcha/02f42006c614879521ded88c953d0c33 to your computer and use it in GitHub Desktop.
Save glebcha/02f42006c614879521ded88c953d0c33 to your computer and use it in GitHub Desktop.
Deep object clone without functional properties
function structuralClone(obj) {
return new Promise(resolve => {
const {port1, port2} = new MessageChannel();
port2.onmessage = ev => resolve(ev.data);
port1.postMessage(obj);
});
}
var obj = {a: 1, b: {
c: b
}}
// pay attention that this method is asynchronous
// it can handle `undefined` and circular reference object
const clone = await structuralClone(obj);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment