当前位置:网站首页>深拷贝真难
深拷贝真难
2022-07-05 13:47:00 【wade3po】
深拷贝浅拷贝的原理我是已经懂了,之前也有分享过。只是深拷贝浅拷贝的方法却从来没有真的去了解过。
想想之前的面试,问到深拷贝浅拷贝的时候,都是说一下原理,问到方法,张口就来JSON.parse和JSON.stringify,如果有函数的话就递归循环拷贝。却从来没有写过递归或者循环拷贝的函数。
今天写了一下深拷贝数据的函数,发现深拷贝其实好难好难,单单一个数据的深拷贝就让我觉得脑壳都受不了了,更别说一些大佬们还提到了原型链、dom、RegExp、函数、浏览器内置函数之类的是否拷贝或者说怎么处理,再加上兼容,以后出去再也不敢说能写出深拷贝函数了。
今天也不是写个什么好的深拷贝函数,单纯的写一下数据处理的深拷贝,虽然对于json对象的深拷贝JSON.parse和JSON.stringify是最简单的,只是让自己稍微见见世面。
先上一个递归深拷贝的函数:
function clone(data) {
var target = data;
if(isObject(data)){
target = {};
for(var i in data) {
target[i] = clone(data[i])
}
}
if(isArray(data)){
target = [];
for (let i = 0; i < data.length; i++) {
target[i] = clone(data[i])
}
}
return target;
}
function isObject(x){
return Object.prototype.toString.call(x) === '[object Object]';
}
function isArray(x){
return Object.prototype.toString.call(x) === '[object Array]';
}
很简单,判断传进来的参数是否是数组或者对象,这边对数组或者对象的判断比较严格,如果是就分别循环递归拷贝子对象。这只是对对象和数组的深拷贝。for in我们都知道会遍历出自身和原型链上可枚举的属性,有时候我们是不想拷贝这些的。所以这边兼容就要在for in里面加上data.hasOwnProperty的判断。
基本上递归的拷贝都能理解,但是如果一个数据层级太多,会出现爆栈:
Maximum call stack size exceeded
包括JSON.parse和JSON.stringify也会。
这时候就要循环深拷贝了,直接上代码:
function cloneLoop(x) {
let target = '';
if(isObject(x)){
target = {};
};
if(isArray(x)){
target = [];
}
const loopList = [
{
parent: target,
key: undefined,
data: x,
}
];
while(loopList.length) {
const node = loopList.pop();
const parent = node.parent;
const key = node.key;
const data = node.data;
let res = parent;
if(typeof key !== 'undefined'){
if(isObject(data)) {
res = parent[key] = {};
};
if(isArray(data)) {
res = parent[key] = [];
}
}
if(isObject(data)){
for(let k in data) {
initLoopList(data[k], k, res);
}
}
if(isArray(data)){
for (let i = 0; i < data.length; i++) {
initLoopList(data[i], i, res);
}
}
}
function initLoopList(val, key, res) {
if (isObject(val) || isArray(val)) {
loopList.push({
parent: res,
key: key,
data: val,
});
} else {
res[key] = val;
}
}
return target;
}
function isObject(val){
return Object.prototype.toString.call(val) === '[object Object]'
}
function isArray(val){
return Object.prototype.toString.call(val) === '[object Array]'
}
我自己都不知道要这么去注释这个函数,精华就是这两个引用:
res = parent[key] = {};
res = parent[key] = [];
就是对引用稍微绕了一点,可以自己理解一下。
深拷贝暂时只能是了解到这种程度了,当然,一般来说数据不会有这么深的层级。这只是最简单的对数组对象的拷贝,没有涉及函数、dom或者原型链这些,连数据的引用都没考虑进去,比如:
let a = {};
let b = {b:b, c:c};
这种情况还得考虑是否保留引用还是创建新的。
最后提个序列化和反序列化的概念,今天分享的其实就是针对可以序列化和发序列化的数据。百度百科上面对序列化的定义:
序列化是将对象的状态信息转换为可以存储或传输的形式的过程。在序列化期间,对象将其当前状态写入到临时或持久性存储区。以后,可以通过从存储区中读取或反序列化对象的状态,重新创建该对象。
不同语言有不同的方法,百度百科上面Java的序列化是转成二进制,反序列化是将二进制转成对象。而JavaScript我理解的序列化就是对象转成字符串,发序列化把字符串转成对象。
边栏推荐
- Solve the problem of "unable to open source file" xx.h "in the custom header file on vs from the source
- 内网穿透工具 netapp
- FPGA learning notes: vivado 2019.1 add IP MicroBlaze
- 运筹说 第68期|2022年最新影响因子正式发布 快看管科领域期刊的变化
- Can graduate students not learn English? As long as the score of postgraduate entrance examination English or CET-6 is high!
- PHP character capture notes 2020-09-14
- Solve the problem of invalid uni app configuration page and tabbar
- 通讯录(链表实现)
- These 18 websites can make your page background cool
- The development of speech recognition app with uni app is simple and fast.
猜你喜欢
Idea remote debugging agent
Redis6 transaction and locking mechanism
What are the private addresses
Assembly language - Beginner's introduction
我为什么支持 BAT 拆掉「AI 研究院」
Godson 2nd generation burn PMON and reload system
jasypt配置文件加密|快速入门|实战
Record in-depth learning - some bug handling
These 18 websites can make your page background cool
一网打尽异步神器CompletableFuture
随机推荐
Aspx simple user login
Win10——轻量级小工具
今年上半年,通信行业发生了哪些事?
Attack and defense world crypto WP
FPGA learning notes: vivado 2019.1 add IP MicroBlaze
2022司钻(钻井)考试题库及模拟考试
Redis6 master-slave replication and clustering
ETCD数据库源码分析——集群间网络层客户端peerRt
【MySQL 使用秘籍】一网打尽 MySQL 时间和日期类型与相关操作函数(三)
如何把大的‘tar‘存档文件分割成特定大小的多个文件
ZABBIX monitoring
Parsing XML using Dom4j
这18个网站能让你的页面背景炫酷起来
【Hot100】34. Find the first and last positions of elements in a sorted array
【华南理工大学】考研初试复试资料分享
几款分布式数据库的对比
How to apply the updated fluent 3.0 to applet development
嵌入式软件架构设计-消息交互
Apicloud studio3 WiFi real machine synchronization and WiFi real machine preview instructions
Record in-depth learning - some bug handling