Bug in 1.4rc1 $.extend

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Bug in 1.4rc1 $.extend

Tyron-2
I was using jQuery 1.3.2 on my application, and I used to have the
following:
var data = jQuery.extend(true, {}, arrComp);
whereas arrComp is an array of objects. This was used to duplicate the
array, so that I could remove some properties from the objects before
passing the "data" to ajax, and keep the originals objects intact.

The problem is that in 1.4rc1 the "data" is not just a copy of
arrComp. When I delete the properties of its objects, the original
array is modified as well.

Is this a bug? Or am I doing the wrong thing?

Thank you in advance!

--
You received this message because you are subscribed to the Google Groups "jQuery Development" group.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/jquery-dev?hl=en.


Reply | Threaded
Open this post in threaded view
|

Re: Bug in 1.4rc1 $.extend

dave.methvin
> var data = jQuery.extend(true, {}, arrComp);

You're extending an Array into an empty Object? That would lose the
Array-ness of the original object. I don't think this is a use case
that was ever anticipated, not sure it makes sense.

You could extend into an empty array [] to fix that, but after
creating a test case and looking at the code I can see it doesn't deep-
copy that either. I don't think the top-level object was intended to
be an Array. You could open an enhancement ticket for it though.

var x = [ {test:1}, {toast:2} ];
var y = $.extend(true, [], x);
x[1] = {twist: 3};
alert( x.toSource() ); // [{test:1}, {twist:3}]

--
You received this message because you are subscribed to the Google Groups "jQuery Development" group.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/jquery-dev?hl=en.


Reply | Threaded
Open this post in threaded view
|

Re: Bug in 1.4rc1 $.extend

dave.methvin
> x[1] = {twist: 3};
> alert( x.toSource() ); // [{test:1}, {twist:3}]

My test case was broken! When I change it to this:

var x = [ {test:1}, {toast:2} ];
var y = $.extend(true, [], x);
x[1].toast = 3;
alert( y.toSource() );

I get the expected output:

[{test:1}, {toast:2}]

That is, x was deep-extended to y and a change to x didn't affect y.

So it seems like that should work. Can you show more of the code?

--
You received this message because you are subscribed to the Google Groups "jQuery Development" group.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/jquery-dev?hl=en.


Reply | Threaded
Open this post in threaded view
|

Re: Bug in 1.4rc1 $.extend

Tyron-2
Well, I've got your point, and changed my code to var data =
jQuery.extend(true, [], arrComp);
With that, I wanted to make a copy of "arrComp" into "data", so that
deleting something in data won's mess with arrComp, right?

Testing like you did (and even if I make arrComp.toSource() , copy the
content and paste directly in my code, like:)

var arrComp = [{name:'One', value:1}, {name:'Two', value:2}];
var data = jQuery.extend(true, [], arrComp);

It works! Deleting something in data won't change arrComp. But in my
real case, it doesn't work! When I delete something in the data, it
deletes from arrComp as well. I don't know how could I show you this,
I've been trying to reproduce this in small scale, but I had no
success.

If it happens to me to find out a better simulation, I will post here
again. Thanks for you help, though!




On Jan 14, 2:44 am, Dave Methvin <[hidden email]> wrote:

> > x[1] = {twist: 3};
> > alert( x.toSource() ); // [{test:1}, {twist:3}]
>
> My test case was broken! When I change it to this:
>
> var x = [ {test:1}, {toast:2} ];
> var y = $.extend(true, [], x);
> x[1].toast = 3;
> alert( y.toSource() );
>
> I get the expected output:
>
> [{test:1}, {toast:2}]
>
> That is, x was deep-extended to y and a change to x didn't affect y.
>
> So it seems like that should work. Can you show more of the code?

--
You received this message because you are subscribed to the Google Groups "jQuery Development" group.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/jquery-dev?hl=en.