深度替换

65 阅读1分钟
function deep(isDeep, ...params) {
    var target = params[0],
        options,
        copy,
        name,
        i = 1
    for (;i < params.length;i++) {
        options = params[i]
        for (name in options) {
            copy = options[name]
            if (isDeep && typeof copy == 'object') {
                if (typeof target[name] !== 'object') {
                    target[name] = copy
                } else {
                    target[name] = deep(true, target[name], copy)
                }
            } else {
                target[name] = copy
            }
        }
    }
    return target
}