diff --git a/lodash.js b/lodash.js index e92f31e89..2fc38ccf4 100644 --- a/lodash.js +++ b/lodash.js @@ -4786,10 +4786,10 @@ */ function mergeDefaults(objValue, srcValue, key, object, source, stack) { if (isObject(objValue)) { - stack.set(objValue, objValue); + stack.set(srcValue, objValue); baseMerge(objValue, srcValue, mergeDefaults, stack); } - return objValue === undefined ? srcValue : objValue; + return objValue === undefined ? baseClone(srcValue) : objValue; } /** diff --git a/test/test.js b/test/test.js index b96a67f1d..2f14c3d79 100644 --- a/test/test.js +++ b/test/test.js @@ -3562,7 +3562,20 @@ source.bar.b = source.foo.b; var actual = _.defaultsDeep(object, source); - assert.ok(actual.bar.b === source.foo.b && actual.foo.b.c.d === actual.foo.b.c.d.foo.b.c.d); + assert.ok(actual.bar.b === actual.foo.b && actual.foo.b.c.d === actual.foo.b.c.d.foo.b.c.d); + }); + + QUnit.test('should not modify sources', function(assert) { + assert.expect(3); + + var object = {}, + source1 = { 'a': 1, 'b': { 'c': 2 } }, + source2 = { 'b': { 'c': 3, 'd': 3 } }, + actual = _.defaultsDeep(object, source1, source2); + + assert.deepEqual(actual, { 'a': 1, 'b': { 'c': 2, 'd': 3 } }); + assert.deepEqual(source1, { 'a': 1, 'b': { 'c': 2 } }); + assert.deepEqual(source2, { 'b': { 'c': 3, 'd': 3 } }); }); }());