Spoom
Spoom

Reputation: 195

jQuery.extend() deep clone nested objects

I am trying to clone a quite complicated object with nested sub objects.

The object has a structure like this:

http://pastebin.com/2NgQQXUC

using jQuery.extend():

var board = $.extend(true, {}, this.game.board)

doesn't clone the nested objects, so I have used JSON to be sure there are no leftover references to the source object.

var boardJSON = JSON.stringify(JSON.decycle(this.game.board));
var board =  JSON.retrocycle($.parseJSON(boardJSON));

This works very well, but the performance is miserable.

Upvotes: 1

Views: 4140

Answers (1)

Spoom
Spoom

Reputation: 195

Finally found the answer: JQuery doesn't support deep cloning of user defined objects at the moment, but this library does: owl

Upvotes: 1

Related Questions