From ed3f8cc39df9742ac56937d786eecdc03d723668 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Sun, 3 Jan 2016 18:49:32 -0600 Subject: [PATCH] Add support for creating clones with the same `[[Prototype]]` as `value`. --- lodash.js | 15 +++++++-------- test/test.js | 8 +++++++- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/lodash.js b/lodash.js index fdae8dfbf..ff3f722b9 100644 --- a/lodash.js +++ b/lodash.js @@ -2272,7 +2272,7 @@ * @returns {Function} Returns the new function. */ function baseConforms(source) { - var props = keysIn(source), + var props = keys(source), length = props.length; return function(object) { @@ -4834,7 +4834,7 @@ */ function initCloneArray(array) { var length = array.length, - result = new array.constructor(length); + result = array.constructor(length); // Add properties assigned by `RegExp#exec`. if (length && typeof array[0] == 'string' && hasOwnProperty.call(array, 'index')) { @@ -4853,7 +4853,7 @@ */ function initCloneObject(object) { var Ctor = object.constructor; - return (typeof Ctor == 'function' && Ctor instanceof Ctor) ? new Ctor : {}; + return baseCreate(isFunction(Ctor) ? Ctor.prototype : undefined); } /** @@ -9066,11 +9066,10 @@ * **Note:** This method is loosely based on the * [structured clone algorithm](https://mdn.io/Structured_clone_algorithm) * and supports cloning arrays, array buffers, booleans, date objects, maps, - * numbers, `Object` objects, regexes, sets, strings, symbols, and typed arrays. - * The own enumerable properties of `arguments` objects and objects created - * by constructors other than `Object` are cloned as plain `Object` objects. - * An empty object is returned for uncloneable values such as error objects, - * functions, DOM nodes, and WeakMaps. + * numbers, `Object` objects, regexes, sets, strings, symbols, and typed + * arrays. The own enumerable properties of `arguments` objects are cloned + * as plain objects. An empty object is returned for uncloneable values such + * as error objects, functions, DOM nodes, and WeakMaps. * * @static * @memberOf _ diff --git a/test/test.js b/test/test.js index 336c92e9f..39bbc4512 100644 --- a/test/test.js +++ b/test/test.js @@ -2263,7 +2263,7 @@ (function() { function Foo() { this.a = 1; } - Foo.prototype = { 'b': 1 }; + Foo.prototype.b = 1; Foo.c = function() {}; if (Map) { @@ -2451,6 +2451,12 @@ assert.strictEqual(actual.lastIndex, 3); }); + QUnit.test('`_.' + methodName + '` should create clone with the same `[[Prototype]]` as `value`', function(assert) { + assert.expect(1); + + assert.ok(func(new Foo) instanceof Foo); + }); + QUnit.test('`_.' + methodName + '` should clone properties that shadow those on `Object.prototype`', function(assert) { assert.expect(2);