9

I've searched everywhere and found similar questions with answers that didn't really address my issue so I apologize if this seems like a repeat, but it appears from my experimenting that jQuery's deep copy function doesn't actually work as it's described (or maybe I'm misreading its description).

Here's an example demonstrating the problem I'm having: http://jsfiddle.net/wcYsH/

Or this for download: https://github.com/kevroy314/jQuery-Extend-Test

Why does the data in the previous copy get changed when the deep copy is manipulated?

5
  • 1
    Do you want to use jQuery's .clone instead? Commented May 8, 2012 at 19:29
  • 1
    There was a thread about this on the jQuery forums recently. I think your usecase falls outside of the purpose of the $.extend method. forum.jquery.com/topic/…
    – Kevin B
    Commented May 8, 2012 at 19:35
  • 1
    Here's a related bug ticket bugs.jquery.com/ticket/10014
    – Kevin B
    Commented May 8, 2012 at 19:38
  • So glad I'm not going crazy! jQuery .clone was what I investigated initially, but I saw this answer to a similar question and decided I'd try it first.
    – user986122
    Commented May 8, 2012 at 19:44
  • Update: It looks like .clone will not clone my type of object. Only DOM objects. I think the best solution right now is to write a clone function for each of my objects and let them handle self-replication themselves.
    – user986122
    Commented May 8, 2012 at 20:03

1 Answer 1

5

For one, you aren't creating normal objects.

I'm looking at the source code for jQuery 1.7.2 for extend.

https://github.com/jquery/jquery/blob/master/src/core.js

And I'm noticing the line:

if ( deep && copy && ( jQuery.isPlainObject(copy) || (copyIsArray = jQuery.isArray(copy))

has to evaluate to true to do deep copying. copy is just part of the current object being copied.

But you aren't creating "plain" objects. You are creating objects generated by invoking a constructor with the new operator.

Now, in isPlainObject, it seems these lines have to be evaluated. (where hasOwn is hasOwn = Object.prototype.hasOwnProperty

    try {
        // Not own constructor property must be Object
        if ( obj.constructor &&
            !hasOwn.call(obj, "constructor") &&
            !hasOwn.call(obj.constructor.prototype, "isPrototypeOf") ) {
            return false;
        }
    } catch ( e ) {
        // IE8,9 Will throw exceptions on certain host objects #9897
        return false;
    }

And there's where it concludes it's not a "plainObject".

This makes sense when you consider objects with a constructor probably ought to be created via that constructor or at least use some sort of "clone" method as you'd see in other languages/frameworks.

1
  • 1
    Thank you! That's the explanation I was looking for. I've gone ahead and written a .clone function for each object I've created. Unfortunately, the hacky way I was using .extend apparently was copying the objects just deep enough that my application worked, but shallow enough as to not waste memory, so now my implementation is a horrible memory hog! Back to the drawing board!
    – user986122
    Commented May 9, 2012 at 15:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.