Created
July 18, 2018 11:33
-
-
Save glebcha/02f42006c614879521ded88c953d0c33 to your computer and use it in GitHub Desktop.
Deep object clone without functional properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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