牛逼哄哄的对象深复制
function cloneObj(source, target) { if (!target) { if (source.constructor === RegExp) { target = new RegExp(source.source, source.flags) } else { target = new source.constructor() } } var names = Object.getOwnPropertyNames(source) for (var i = 0; i < names.length; i++) { var desc = Object.getOwnPropertyDescriptor(source, names[i]) if (typeof desc.value === 'object' && desc.value !== null) { var values = cloneObj(desc.value) Object.defineProperty(target, names[i], { enumerable: desc.enumerable, configurable: desc.configurable, writable: desc.writable, value: values }) } else { Object.defineProperty(target, names[i], desc) } } return target }
function deepClone(obj = {}) { if (typeof obj !== 'object' || obj == null) return obj let result result = obj instanceof Array ? [] : {} for (let key in obj) { if (obj.hasOwnProperty(key)) result[key] = deepClone(obj[key]) } return result }