1 var baseRest = require('./_baseRest'),
3 isIterateeCall = require('./_isIterateeCall'),
4 keysIn = require('./keysIn');
6 /** Used for built-in method references. */
7 var objectProto = Object.prototype;
9 /** Used to check objects for own properties. */
10 var hasOwnProperty = objectProto.hasOwnProperty;
13 * Assigns own and inherited enumerable string keyed properties of source
14 * objects to the destination object for all destination properties that
15 * resolve to `undefined`. Source objects are applied from left to right.
16 * Once a property is set, additional values of the same property are ignored.
18 * **Note:** This method mutates `object`.
24 * @param {Object} object The destination object.
25 * @param {...Object} [sources] The source objects.
26 * @returns {Object} Returns `object`.
30 * _.defaults({ 'a': 1 }, { 'b': 2 }, { 'a': 3 });
31 * // => { 'a': 1, 'b': 2 }
33 var defaults = baseRest(function(object, sources) {
34 object = Object(object);
37 var length = sources.length;
38 var guard = length > 2 ? sources[2] : undefined;
40 if (guard && isIterateeCall(sources[0], sources[1], guard)) {
44 while (++index < length) {
45 var source = sources[index];
46 var props = keysIn(source);
48 var propsLength = props.length;
50 while (++propsIndex < propsLength) {
51 var key = props[propsIndex];
52 var value = object[key];
54 if (value === undefined ||
55 (eq(value, objectProto[key]) && !hasOwnProperty.call(object, key))) {
56 object[key] = source[key];
64 module.exports = defaults;