Add support for creating clones with the same [[Prototype]] as value.

This commit is contained in:
John-David Dalton
2016-01-03 18:49:32 -06:00
parent f39fdc42f6
commit ed3f8cc39d
2 changed files with 14 additions and 9 deletions

View File

@@ -2272,7 +2272,7 @@
* @returns {Function} Returns the new function. * @returns {Function} Returns the new function.
*/ */
function baseConforms(source) { function baseConforms(source) {
var props = keysIn(source), var props = keys(source),
length = props.length; length = props.length;
return function(object) { return function(object) {
@@ -4834,7 +4834,7 @@
*/ */
function initCloneArray(array) { function initCloneArray(array) {
var length = array.length, var length = array.length,
result = new array.constructor(length); result = array.constructor(length);
// Add properties assigned by `RegExp#exec`. // Add properties assigned by `RegExp#exec`.
if (length && typeof array[0] == 'string' && hasOwnProperty.call(array, 'index')) { if (length && typeof array[0] == 'string' && hasOwnProperty.call(array, 'index')) {
@@ -4853,7 +4853,7 @@
*/ */
function initCloneObject(object) { function initCloneObject(object) {
var Ctor = object.constructor; 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 * **Note:** This method is loosely based on the
* [structured clone algorithm](https://mdn.io/Structured_clone_algorithm) * [structured clone algorithm](https://mdn.io/Structured_clone_algorithm)
* and supports cloning arrays, array buffers, booleans, date objects, maps, * and supports cloning arrays, array buffers, booleans, date objects, maps,
* numbers, `Object` objects, regexes, sets, strings, symbols, and typed arrays. * numbers, `Object` objects, regexes, sets, strings, symbols, and typed
* The own enumerable properties of `arguments` objects and objects created * arrays. The own enumerable properties of `arguments` objects are cloned
* by constructors other than `Object` are cloned as plain `Object` objects. * as plain objects. An empty object is returned for uncloneable values such
* An empty object is returned for uncloneable values such as error objects, * as error objects, functions, DOM nodes, and WeakMaps.
* functions, DOM nodes, and WeakMaps.
* *
* @static * @static
* @memberOf _ * @memberOf _

View File

@@ -2263,7 +2263,7 @@
(function() { (function() {
function Foo() { this.a = 1; } function Foo() { this.a = 1; }
Foo.prototype = { 'b': 1 }; Foo.prototype.b = 1;
Foo.c = function() {}; Foo.c = function() {};
if (Map) { if (Map) {
@@ -2451,6 +2451,12 @@
assert.strictEqual(actual.lastIndex, 3); 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) { QUnit.test('`_.' + methodName + '` should clone properties that shadow those on `Object.prototype`', function(assert) {
assert.expect(2); assert.expect(2);