参见

Object.assign()

const obj1 = { a: 1, b: 2 };
const obj2 = { b: 3, c: 4, d: 5 };
const result = Object.assign(obj1, obj2);
console.log(result);//{ a: 1, b: 3, c: 4, d: 5 }

循环遍历

function extend() {
    var length = arguments.length;
    if(length == 0)return {};
    if(length == 1)return arguments[0];
    var target = arguments[0] || {};
    for (var i = 1; i < length; i++) {
        var source = arguments[i];
        for (var key in source) {
            if (source.hasOwnProperty(key)) {
                target[key] = source[key];
            }
        }
    }
     
    return target;
}
const obj1 = { a: 1, b: 2 };
const obj2 = { b: 3, c: 4, d: 5 };
const result = extend(obj1, obj2);
console.log(result);//{ a: 1, b: 3, c: 4, d: 5 }
/**
 * @Description 克隆对象
 * 能被克隆的对象类型:
 *     Plain object, Array, TypedArray, number, string, null, undefined.
 * 直接用原始数据进行赋值的数据类型:
 *     BUILTIN_OBJECT
 * 用户定义类的实例将克隆到一个原型中没有属性的普通对象。
 * @method clone
 * @param {*} source
 * @return {*} new
 */
clone(source) {
    if (source == null || typeof source !== 'object') {
        return source;
    }
    var result = source;
    var typeStr = this.objToString.call(source);
    if (typeStr === '[object Date]') {
        result = this.cloneDate(source);
    }
    else if (typeStr === '[object RegExp]') {
        result = this.cloneRegExp(source);
    }
    else if (typeStr === '[object Function]') {
        result = this.cloneFunction(source);
    }
    else if (typeStr === '[object Array]') {
        result = [];
        for (var i = 0, len = source.length; i < len; i++) {
            result[i] = this.clone(source[i]);
        }
    }
    else if (this.TYPED_ARRAY[typeStr]) {
        var Ctor = source.constructor;
        if (source.constructor.from) {
            result = Ctor.from(source);
        }
        else {
            result = new Ctor(source.length);
            for (var i = 0, len = source.length; i < len; i++) {
                result[i] = this.clone(source[i]);
            }
        }
    }
    else if (!this.BUILTIN_OBJECT[typeStr] && !this.isDom(source)) {
        result = {};
        for (var key in source) {
            if (this.hasOwn(source, key)) {
                result[key] = this.clone(source[key]);
            }
        }
    }
    return result;
},
/**
 * @Description 合并函数
 * @method merge
 * @param {*} target
 * @param {*} source
 * @param {boolean} [overwrite=false]
 * @return {Object}
 */
merge(target, source, overwrite) {
    // We should escapse that source is string
    // and enter for ... in ...
    if (!this.isObject(source) || !this.isObject(target)) {
        return overwrite ? this.clone(source) : target;
    }
    for (var key in source) {
        if (this.hasOwn(source, key)) {
            var targetProp = target[key];
            var sourceProp = source[key];
            if (this.isObject(sourceProp)
                && this.isObject(targetProp)
                && !this.isDom(sourceProp)
                && !this.isDom(targetProp)
                && !this.isBuiltInObject(sourceProp)
                && !this.isBuiltInObject(targetProp)
            ) {
                // 如果需要递归覆盖,就递归调用merge
                this.merge(targetProp, sourceProp, overwrite);
            }
            else if (overwrite || !(key in target)) {
                // 否则只处理overwrite为true,或者在目标对象中没有此属性的情况
                // NOTE,在 target[key] 不存在的时候也是直接覆盖
                target[key] = this.clone(source[key], true);
            }
        }
    }
    return target;
}