当前位置:网站首页>踩坑记: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里除了深克隆的方法外,还有很多实用的方法,详情可查看官方文档:
八、自己实现一个深拷贝
边栏推荐
- 等保测评结论为差,是不是表示等保工作白做了?
- Detailed analysis on the use of MySQL stored procedure loop
- 国内酒店交易DDD应用与实践——理论篇
- Partial mock of static class of phpunit operation
- jdbc_ Related codes
- js两个一维数组合并并去除相同项(整理)
- Bloom filter:
- Top 30 open source software
- 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
- [tcapulusdb knowledge base] tcapulusdb doc acceptance - Introduction to creating game area
猜你喜欢

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

Adobe Premiere foundation - cool text flash (14)

Prevent form resubmission based on annotations and interceptors

Request header field xxxx is not allowed by Access-Control-Allow-Headers in preflight response问题

Precondition end of script headers or end of script output before headers

jdbc_相关代码
Detailed analysis on the use of MySQL stored procedure loop

Distributed | several steps of rapid read / write separation

Detailed introduction and Simulation of bitmap

Adobe Premiere基础-常用的视频特效(边角定位,马赛克,模糊,锐化,手写工具,效果控件层级顺序)(十六)
随机推荐
YoloV6+TensorRT+ONNX:基于WIN10+TensorRT8+YoloV6+ONNX的部署
WBF: new method of NMS post filter frame for detection task?
Test dble split function execution + import time-consuming shell script reference
VMware安装ESXI
The soft youth under the blessing of devcloud makes education "smart" in the cloud
[tcapulusdb knowledge base] tcapulusdb doc acceptance - Introduction to creating game area
Distributed | several steps of rapid read / write separation
[tcapulusdb knowledge base] tcapulusdb system user group introduction
Encryption and decryption of 535 tinyurl
3h精通OpenCV(八)-形状检测
Partial mock of static class of phpunit operation
WBF:检测任务NMS后虑框新方式?
国内酒店交易DDD应用与实践——理论篇
SSH protocol learning notes
Bloom filter:
PostgreSQL database system table
MySQL数据库每日备份并定时清理脚本
jdbc认识上手
[tcapulusdb knowledge base] tcapulusdb operation and maintenance doc introduction
Abc253 D fizzbuzz sum hard (tolerance exclusion theorem)