Skip to content

Commit 93be758

Browse files
rkaticjeresig
authored andcommitted
Made jQuery.extend(true, ...) to extend recursively only 'object literal' values.
1 parent 990d9ca commit 93be758

File tree

2 files changed

+11
-16
lines changed

2 files changed

+11
-16
lines changed

src/core.js

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -272,19 +272,10 @@ jQuery.extend = jQuery.fn.extend = function() {
272272
continue;
273273
}
274274

275-
// Recurse if we're merging object values
276-
if ( deep && copy && typeof copy === "object" && !copy.nodeType ) {
277-
var clone;
278-
279-
if ( src ) {
280-
clone = src;
281-
} else if ( jQuery.isArray(copy) ) {
282-
clone = [];
283-
} else if ( jQuery.isObjectLiteral(copy) ) {
284-
clone = {};
285-
} else {
286-
clone = copy;
287-
}
275+
// Recurse if we're merging object literal values
276+
if ( deep && copy && jQuery.isObjectLiteral(copy) ) {
277+
// Don't extend not object literals
278+
var clone = src && jQuery.isObjectLiteral(src) ? src : {};
288279

289280
// Never move original objects, clone them
290281
target[ name ] = jQuery.extend( deep, clone, copy );

test/unit/core.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -553,16 +553,20 @@ test("jQuery.extend(Object, Object)", function() {
553553
same( empty.foo, optionsWithDate.foo, "Dates copy correctly" );
554554

555555
var myKlass = function() {};
556-
var optionsWithCustomObject = { foo: { date: new myKlass } };
556+
var customObject = new myKlass();
557+
var optionsWithCustomObject = { foo: { date: new customObject } };
557558
empty = {};
558559
jQuery.extend(true, empty, optionsWithCustomObject);
559-
same( empty.foo, optionsWithCustomObject.foo, "Custom objects copy correctly (no methods)" );
560+
ok( empty.foo && empty.foo.date && empty.foo.date === customObject, "Custom objects copy correctly (no methods)" );
560561

561562
// Makes the class a little more realistic
562563
myKlass.prototype = { someMethod: function(){} };
563564
empty = {};
564565
jQuery.extend(true, empty, optionsWithCustomObject);
565-
same( empty.foo, optionsWithCustomObject.foo, "Custom objects copy correctly" );
566+
ok( empty.foo && empty.foo.date && empty.foo.date === customObject, "Custom objects copy correctly" );
567+
568+
var ret = jQuery.extend(true, { foo: 4 }, { foo: new Number(5) } );
569+
ok( ret.foo == 5, "Wrapped numbers copy correctly" );
566570

567571
var nullUndef;
568572
nullUndef = jQuery.extend({}, options, { xnumber2: null });

0 commit comments

Comments
 (0)