Deep clone an object with jQuery

D

To copy/clone an object using jQuery, you will want to leverage the extend function. This function accepts N objects to clone/merge as well as a boolean to indicate whether it should copy recursively or not.

Using $.extend to clone an object

In the example below, the jQuery extend function will be used to copy the properties from objectA to objectB. This will be done twice to demonstrate the difference between a recursive clone and non-recursive clone.

After objectA has been copied to objectB, the output of the copy is outputted on screen and shows that the full object, including child properties, have been copied to the new object.

About the author

By Jamie

My Books