- /**
- * 深拷贝
- * @param {Object} obj 要拷贝的对象
- */
- function deepClone(obj = {}) {
- if (typeof obj !== "object" || obj == null) {
- return obj;
- }
- let result;
- if (obj instanceof Array) {
- result = [];
- } else {
- result = {};
- }
- for (let key in obj) {
- if (obj.hasOwnProperty(key)) {//保证key不是原型属性
- result[key] = deepClone(obj[key]); //递归调用
- }
- }
- return result;
- }