Skip to main content

Bug Tracker

Side navigation

#6958 closed bug (duplicate)

Opened August 27, 2010 10:13AM UTC

Closed October 03, 2010 08:35PM UTC

$.extend (not deep) copying arrays by reference not cloning

Reported by: enideo Owned by:
Priority: undecided Milestone: 1.4.3
Component: core Version: 1.4.2
Keywords: extend array Cc:
Blocked by: Blocking:
Description

BUG: as put in the title, see the attachment for the full explanation and examples.

I believe this to be a bug as the behaviour is not as desired: arguments of the extend function, other than target, can be modified if the target is later modified (they are referenced not cloned)

PROPOSED FIX: replace the following lines in the jQuery.extend function:

// Don't bring in undefined values
} else if ( copy !== undefined ) {
	target[ name ] = copy;
}

with...

// Don't bring in undefined values
} else if ( copy !== undefined ) {
  target[ name ] = ( jQuery.isArray!==undefined && jQuery.isArray(copy) )
  ? target[ name ] = copy.slice(0) : target[ name ] = copy;
}

NOTE: it seems necessary to check for the jQuery.isArray function's existence as the jQuery.extend function is called before this has been defined. Those who are more familiar with the rest of the code may be able to come up with a better solution.

Attachments (1)
  • jqExtendArrayBug.html (4.2 KB) - added by enideo August 27, 2010 10:14AM UTC.

    Example of the bug and fixes

Change History (1)

Changed October 03, 2010 08:35PM UTC by dmethvin comment:1

priority: → undecided
resolution: → duplicate
status: newclosed