1

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

The object has a structure like this:

  • Board has n elements of BoardElement
  • BoardElement has n Elements of BoardElementUnits

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.

Spoom
  • 195
  • 3
  • 12
  • This may help ... [extendtrue-deep-copy](http://stackoverflow.com/questions/16512773/jquery-extendtrue-obj-not-creating-a-deep-copy) – Stphane Oct 20 '13 at 10:14

1 Answers1

1

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

Spoom
  • 195
  • 3
  • 12