-
深拷贝: 值拷贝
-
浅拷贝: 地址的拷贝, 只拷贝一层
-
实现:
-
深拷贝: 递归
function deepClone (sourceObj, targetObj) { let cloneObj = targetObj || {} if(!sourceObj || typeof sourceObj !== "object" || sourceObj.length === undefined){ return sourceObj } if(sourceObj instanceof Array){ cloneObj = sourceObj.concat() } else { for(let i in sourceObj){ if (typeof sourceObj[i] === 'object') { cloneObj[i] = deepClone(sourceObj[i], {}) } else { cloneObj[i] = sourceObj[i] } } } return cloneObj }
-
-
工具实现深拷贝
- lodash
- Immutable.js
深拷贝与浅拷贝
最新推荐文章于 2024-09-25 16:36:26 发布