Monday, 15 September 2014

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


What is the most effective way to clone a JavaScript object? I have seen obj = eval (uneval (o)); is being used, but

I obj = JSON.parse (JSON.stringify (o)); But efficiency question

I have also seen the recursive copy function with various flaws.
I am surprised that no prescriptual solution exists.

Note: This is the answer to another answer, The correct answer to this question is not if you want to cloning the object faster, please follow this question.


I would like to note that only clone DOM element in the method jQuery to clone the JavaScript object, you will do this: < Pre> // shallow copy var newObject = jQuery.extend ({}, oldObject); // deep copy var newObject = jQuery.extend (true, {}, old object);

More information can be found in.

I also want to note that the deep copy is really very clever than the ones shown above ???? It is capable of avoiding multiple traps (for example, trying to expand a DOM element). It is often used for great effect in jQuery core and plugins.

No comments:

Post a Comment