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:

  • 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.


    最后找到了答案:JQuery目前不支持深度克隆用户定义的对象,但该库确实:owl

    链接地址: http://www.djcxy.com/p/14212.html

    上一篇: jQuery.extend()和jQuery.fn.extend()是一样的...对吗?

    下一篇: jQuery.extend()深入克隆嵌套对象