Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
125
rated 0 times [  130] [ 5]  / answers: 1 / hits: 28585  / 11 Years ago, sun, may 12, 2013, 12:00:00

jsFiddle here.



If deep copying worked, the output would be Curious George and not Ender's Game. How can I make a deep copy? An answer to this question indicates that $.extend(true, [], obj) creates a deep copy. Yet my example shows that it doesn't.



function Person(){}
Person.prototype.favorite_books = [];

var george = new Person();
george.favorite_books = [Curious George];

var kate = new Person();
kate.favorite_books = [The Da Vinci Code, Harry Potter];

var people = [kate, george];

var people_copy = $.extend(true, [], people);
people_copy[0].favorite_books[0] = Ender's Game;

$('#text').text(people[0].favorite_books[0]);


SOLUTION



I updated the jsFiddle. It turns out I need to deep copy each object in the array individually if the object is a custom object (that is, $.isPlainObject returns false).


More From » jquery

 Answers
12

And now here is the real answer:



At the moment jQuery can only clone plain JavaScript Objects, while you're using custom ones. And that's obvious, since jQuery cannot know how exactly to instantiate a new custom object. So this works as expected:



var george = {};
george.favorite_books = [Curious George];

var kate = {};
kate.favorite_books = [The Da Vinci Code, Harry Potter];

var people = [kate, george];

var people_copy = $.extend(true, [], people);

console.log(people_copy[0].favorite_books == people[0].favorite_books);


Reference to a jQuery code: https://github.com/jquery/jquery/blob/master/src/core.js#L305



See that it checks if it's jQuery.isPlainObject(copy) or it's an array. Otherwise it performs just a reference copy.


[#78272] Friday, May 10, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
aidan

Total Points: 72
Total Questions: 95
Total Answers: 121

Location: Uzbekistan
Member since Sat, Feb 27, 2021
3 Years ago
aidan questions
Mon, Oct 11, 21, 00:00, 3 Years ago
Wed, Sep 29, 21, 00:00, 3 Years ago
Sun, Sep 5, 21, 00:00, 3 Years ago
Thu, Jan 16, 20, 00:00, 4 Years ago
;