深copy对象
function deepClone(obj) { if (obj === null) return null; if (typeof obj !== 'object') return obj; if (obj instanceof Date) { let date = new Date(); date.setTime(obj.getTime()); return date; } if (obj instanceof RegExp) { let re = new RegExp(obj.source); re.lastIndex = obj.lastIndex; return re; } let newObj = new obj.constructor(); for (let key in obj) { if (obj.hasOwnProperty(key)) { newObj[key] = deepClone(obj[key]); } } return newObj; }
作者:人参,每篇随笔皆原创(除非注明原作者的随笔),欢迎指正!