Ensure _.clone and _.cloneDeep work on prototype objects.

This commit is contained in:
John-David Dalton
2016-01-28 00:45:42 -08:00
parent 7bf7ab954b
commit 03f7205e6d
2 changed files with 13 additions and 1 deletions

View File

@@ -4956,6 +4956,9 @@
* @returns {Object} Returns the initialized clone.
*/
function initCloneObject(object) {
if (isPrototype(object)) {
return {};
}
var Ctor = object.constructor;
return baseCreate(isFunction(Ctor) ? Ctor.prototype : undefined);
}

View File

@@ -2520,13 +2520,22 @@
assert.strictEqual(actual.lastIndex, 3);
});
QUnit.test('`_.' + methodName + '` should clone prototype objects', function(assert) {
assert.expect(2);
var actual = func(Foo.prototype);
assert.notOk(actual instanceof Foo);
assert.deepEqual(actual, { 'b': 1 });
});
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('should ensure `value` constructor is a function before using its `[[Prototype]]`', function(assert) {
QUnit.test('`_.' + methodName + '` should ensure `value` constructor is a function before using its `[[Prototype]]`', function(assert) {
assert.expect(1);
Foo.prototype.constructor = null;