Sunday 12 November 2017

What is the most efficient way to deep clone an object in JavaScript?

What is the most efficient way to clone a JavaScript
object? I've seen obj = eval(uneval(o)); being used, but href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/uneval"
rel="noreferrer">that's non-standard and only supported by
Firefox.

I've done things like obj =
JSON.parse(JSON.stringify(o));
but question the efficiency.

I've also seen recursive copying functions with various
flaws.

I'm surprised no canonical solution
exists.

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...