5.10 extend

5.10.1 语法:

_.extend(obj, *source);

5.10.2 说明:

extend方法对两个对象或多个对象进行合并,合并到第一个对象中,并返回此对象

5.10.3 代码示例:

var result = _.extend({ one: 1 }, { two: 2 });
console.log(result); //=> {one: 1, two: 2}

5.10.4 参数obj为非对象,则返回参数,参数若为空则返回undefined

var result;

// 参数obj为空
result = _.extend(); 
console.log(typeof result); //=> undefined

// 参数obj为数字
result = _.extend(123);
console.log(result); //=> 123

// 有两个参数都为数组
result = _.extend([1], [2]);
console.log(result); //=> [2]

5.10.5 如果有重复的属性则会覆盖前边的属性

var result = _.extend({ one: 1 }, { two: 2 }, { two: 3 });
console.log(result); //=> {one: 1, two: 3}

5.10.6 函数prototype继承属性也将进行合并

var Child = function () { };
Child.prototype = {
    name: 'zhangsan'
}
var result = _.extend({ one: 1 }, new Child);
console.log(result); //=> {one: 1, name: "zhangsan"}

5.10.7 特殊情况

var result = _.extend({}, { a: void 0, b: null });
console.log(result); //=> {a: undefined, b: null}