mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-10 10:57:49 +00:00
Ensure _.defaults assigns properties that shadow those on Object.prototype.
This commit is contained in:
33
lodash.js
33
lodash.js
@@ -587,18 +587,6 @@
|
|||||||
return false;
|
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
|
* 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)
|
* 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
|
* The base implementation of `_.assign` without support for multiple sources
|
||||||
* or `customizer` functions.
|
* or `customizer` functions.
|
||||||
@@ -4853,9 +4856,9 @@
|
|||||||
* Used by `_.defaultsDeep` to customize its `_.merge` use.
|
* Used by `_.defaultsDeep` to customize its `_.merge` use.
|
||||||
*
|
*
|
||||||
* @private
|
* @private
|
||||||
* @param {*} objValue The destination object property value.
|
* @param {*} objValue The destination value.
|
||||||
* @param {*} srcValue The source object property value.
|
* @param {*} srcValue The source value.
|
||||||
* @returns {*} Returns the value to assign to the destination object.
|
* @returns {*} Returns the value to assign.
|
||||||
*/
|
*/
|
||||||
function mergeDefaults(objValue, srcValue, key, object, source, stack) {
|
function mergeDefaults(objValue, srcValue, key, object, source, stack) {
|
||||||
if (isObject(objValue) && isObject(srcValue)) {
|
if (isObject(objValue) && isObject(srcValue)) {
|
||||||
|
|||||||
@@ -3626,6 +3626,13 @@
|
|||||||
var actual = _.defaults({ 'a': undefined }, { 'a': 1 });
|
var actual = _.defaults({ 'a': undefined }, { 'a': 1 });
|
||||||
assert.strictEqual(actual.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);
|
||||||
|
});
|
||||||
}());
|
}());
|
||||||
|
|
||||||
/*--------------------------------------------------------------------------*/
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|||||||
Reference in New Issue
Block a user