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.
*/
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 _

View File

@@ -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);