游戏陪玩app开发,必须知道的拷贝代码
(数组/对象)(深/浅)拷贝
let list = [{ name: "o" }]; let obj = { stu: { name: "o" } }; // 数组浅拷贝 let listCopy1 = [].concat(list); let listCopy2 = list.slice(); let listCopy3 = Array.from(list); let listCopy4 = [...list]; // 对象浅拷贝 let objCopy1 = Object.assign({}, obj); let objCopy2 = { ...obj }; // 数组|对象深拷贝 let listCopy = JSON.parse(JSON.stringify(list)); let objCopy = JSON.parse(JSON.stringify(obj)); // 深拷贝,即复制并独立一份数据,操作不影响原数据 function deepCopy(obj) { if (typeof obj !== "object") { return obj; } let result = Array.isArray(obj) ? [] : {}; for (let i in obj) { if (obj.hasOwnProperty(i)) { if (typeof obj[i] === "object" && obj[i] !== null) { result[i] = deepCopy(obj[i]); } else { result[i] = obj[i]; } } } return result; } // 深拷贝 function deepClone(obj) { let copyObj = null; if (typeof obj === "object" && obj !== null) { copyObj = Array.isArray(obj) ? [] : {}; for (let i in obj) { copyObj[i] = deepClone(obj[i]); } } else { copyObj = obj; } return copyObj; }
以上就是游戏陪玩app开发,必须知道的拷贝代码, 更多内容欢迎关注之后的文章
标签:obj,游戏,app,list,copyObj,let,拷贝 From: https://www.cnblogs.com/yunbaomengnan/p/18299673