Ensure _.defaults assigns properties that shadow those on Object.prototype.

This commit is contained in:
John-David Dalton
2015-11-08 12:16:47 -08:00
parent 73d68c9609
commit 8a3842b788
2 changed files with 25 additions and 15 deletions

View File

@@ -587,18 +587,6 @@
return false;
}
/**
* Used by `_.defaults` to customize its `_.assignIn` use.
*
* @private
* @param {*} objValue The destination object property value.
* @param {*} srcValue The source object property value.
* @returns {*} Returns the value to assign to the destination object.
*/
function assignInDefaults(objValue, srcValue) {
return objValue === undefined ? srcValue : objValue;
}
/**
* Assigns `value` to `key` of `object` if the existing value is not equivalent
* using [`SameValueZero`](http://ecma-international.org/ecma-262/6.0/#sec-samevaluezero)
@@ -2137,6 +2125,21 @@
/*------------------------------------------------------------------------*/
/**
* Used by `_.defaults` to customize its `_.assignIn` use.
*
* @private
* @param {*} objValue The destination value.
* @param {*} srcValue The source value.
* @param {string} key The key of the property to assign.
* @returns {*} Returns the value to assign.
*/
function assignInDefaults(objValue, srcValue, key) {
return (objValue === undefined || objValue === objectProto[key])
? srcValue
: objValue;
}
/**
* The base implementation of `_.assign` without support for multiple sources
* or `customizer` functions.
@@ -4853,9 +4856,9 @@
* Used by `_.defaultsDeep` to customize its `_.merge` use.
*
* @private
* @param {*} objValue The destination object property value.
* @param {*} srcValue The source object property value.
* @returns {*} Returns the value to assign to the destination object.
* @param {*} objValue The destination value.
* @param {*} srcValue The source value.
* @returns {*} Returns the value to assign.
*/
function mergeDefaults(objValue, srcValue, key, object, source, stack) {
if (isObject(objValue) && isObject(srcValue)) {

View File

@@ -3626,6 +3626,13 @@
var actual = _.defaults({ 'a': undefined }, { 'a': 1 });
assert.strictEqual(actual.a, 1);
});
QUnit.test('should assign properties that shadow those on `Object.prototype`', function(assert) {
assert.expect(1);
var actual = _.defaults({}, { 'constructor': 1 });
assert.strictEqual(actual.constructor, 1);
});
}());
/*--------------------------------------------------------------------------*/