当前位置:网站首页>踩坑记:JSON.parse和JSON.stringify
踩坑记:JSON.parse和JSON.stringify
2022-06-29 17:59:00 【你吃香蕉吗?】
目录
三、NaN、Infinity 和 -Infinity 被序列化为 null
四、只能序列化对象可枚举的 自有属性,如果通过构造函数new出来的实例,则会丢失constructor构造函数
前言:
在项目中很多人为求简单、方便在进行数据的深拷贝时会采用JSON.parse(JSON.stringify(...))的方式,殊不知,这两个组合的深克隆方式存在很多弊端,要慎用!
一、数据丢失
当被序列化的数据中有Function 或 undefined 时,序列化后,Function 和 undefined 会丢失
<script>
let obj = {
name: 'lili',
age: 13,
address: undefined,
sayHello: function() {
console.log(`my name is ${this.name}`);
}
}
let copyObj = JSON.parse(JSON.stringify(obj));
console.log(copyObj);
</script>
二、时间对象 被序列化为 字符串
<script>
let obj = {
time1: new Date(),
time2: new Date(1656313196128)
}
console.log(obj);
let copyObj = JSON.parse(JSON.stringify(obj))
console.log(copyObj);
</script>
三、NaN、Infinity 和 -Infinity 被序列化为 null
<script>
let obj = {
notANumber: NaN,
positiveNum: Infinity,
minusNum: -Infinity
}
console.log(obj);
let copyObj = JSON.parse(JSON.stringify(obj))
console.log(copyObj);
</script>
四、只能序列化对象可枚举的 自有属性,如果通过构造函数new出来的实例,则会丢失constructor构造函数
<script>
function Student(name) {
this.name = name
}
let lili = new Student('lili')
let Student1 = {
name: lili,
hobby: '羽毛球'
}
console.log(Student1);
let copyStudent1 = JSON.parse(JSON.stringify(Student1))
console.log(copyStudent1);
</script>
五、RegExp、Error对象,被序列化为 {}
<script>
let obj = {
regularObj: new RegExp('/^1[3456789]\d{9}$/'),
errorObj: new Error('出错了!')
}
console.log(obj);
let copyObj = JSON.parse(JSON.stringify(obj))
console.log(copyObj);
</script>

六、循环引用会报错
<script>
let obj = {
name: 'lili'
}
obj.details = obj
console.log(obj);
let copyObj = JSON.parse(JSON.stringify(obj))
console.log(copyObj);
</script>
七、第三方库(推荐lodash)
lodash里除了深克隆的方法外,还有很多实用的方法,详情可查看官方文档:
八、自己实现一个深拷贝
边栏推荐
- QQ如何开通在线客服
- jdbc_ Related codes
- JS merge two one-dimensional arrays and remove the same items (collation)
- Shell tutorial circular statements for, while, until usage
- markdown知识轻轻来袭
- Adobe Premiere基础-不透明度(蒙版)(十一)
- Serial port experiment based on stm32f103zet6 library function
- Adobe Premiere基础-炫酷文字快闪(十四)
- Wechat applet development reserve knowledge
- ABC253 D FizzBuzz Sum Hard(容斥定理)
猜你喜欢

Adobe Premiere基础-不透明度(混合模式)(十二)

DevCloud加持下的青软,让教育“智”上云端

Distributed | several steps of rapid read / write separation

YoloV6+TensorRT+ONNX:基于WIN10+TensorRT8+YoloV6+ONNX的部署

Automatic software test - read SMS verification code using SMS transponder and selenium

Two controller layer interface authentication methods

Image migration and data migration synchronization of old and new servers with different Alibaba cloud accounts

Adobe Premiere foundation - batch material import sequence - variable speed and rewind (recall) - continuous action shot switching - subtitle requirements (13)

Premature end of script headers 或 End of script output before headers

My first experience of remote office | community essay solicitation
随机推荐
MySql存储过程循环的使用分析详解
Adobe Premiere基础-不透明度(蒙版)(十一)
让 Google 搜索到自己的博客
3h精通OpenCV(七)-颜色检测
shell教程之循环语句for,while,until用法
Find the maximum XOR value in the sequence given a number (01 Dictionary)
【TcaplusDB知识库】TcaplusDB运维单据介绍
牛客小白月赛52 E 分组求对数和(容斥定理+二分)
最长异或路径(dfs+01trie)
Record that the server has been invaded by viruses: the SSH password has been changed, the login fails, the malicious program runs full of CPU, the jar package fails to start automatically, and you ha
Proxmox VE Install 7.2
Web Scraping with Beautiful Soup for Data Scientist
SSH protocol learning notes
Niuke small Bai monthly race 52 D ring insectivorous (feet +st table)
Adobe Premiere Foundation - réglage du son (correction du volume, réduction du bruit, tonalité téléphonique, changement de hauteur, égaliseur de paramètres) (XVIII)
Adobe Premiere Basics - common video effects (cropping, black and white, clip speed, mirroring, lens halo) (XV)
3h精通OpenCV(六)-图像堆叠
自动化软件测试 - 利用短信转发器结合Selenium读取短信验证码
Function independent watchdog (iwdg) experiment based on stm32f103zet6 Library
Parental delegation mechanism