Json实现深拷贝的缺点

1、如果被拷贝对象中的属性有时间对象的话,拷贝出来会为字符串,将不再是对象

javascript 复制代码
        var test = {
            name: 'a',
            date: [new Date(1536627600000), new Date(1540047600000)],
        };
        console.log(test)
        console.log(JSON.parse(JSON.stringify(test)));

2、如果obj里有RegExp(正则表达式的缩写)、Error对象,则序列化的结果将只得到空对象;

javascript 复制代码
        const test = {
            name: 'a',
            date: new RegExp('\\w+'),
        };
        const copyed = JSON.parse(JSON.stringify(test));
        test.name = 'test'
        //console.error('ddd', test, copyed);
        console.log(test);
        console.log(copyed);

3、如果obj里有函数、undefined,则序列化的结果会把函数或 undefined丢失;

javascript 复制代码
        const test = {
            name: 'a',
            date: function hehe() {
                console.log('fff')
            },
            b:undefined
        };
        // debugger
        const copyed = JSON.parse(JSON.stringify(test));
        test.name = 'test'
        console.log('ddd', test, copyed);

4、如果obj里有NaN、Infinity和-Infinity,则序列化的结果会变成null

相关推荐
大飞记Python25 分钟前
部门管理|“编辑部门”功能实现(Django5零基础Web平台)
前端·数据库·python·django
tsumikistep1 小时前
【前端】前端运行环境的结构
前端
你的人类朋友1 小时前
【Node】认识multer库
前端·javascript·后端
Aitter1 小时前
PDF和Word文件转换为Markdown的技术实现
前端·ai编程
mapbar_front2 小时前
面试问题—上家公司的离职原因
前端·面试
昔人'3 小时前
css使用 :where() 来简化大型 CSS 选择器列表
前端·css
昔人'3 小时前
css `dorp-shadow`
前端·css
流***陌3 小时前
扭蛋机 Roll 福利房小程序前端功能设计:融合趣味互动与福利适配
前端·小程序
可触的未来,发芽的智生3 小时前
新奇特:黑猫警长的纳米世界,忆阻器与神经网络的智慧
javascript·人工智能·python·神经网络·架构
烛阴4 小时前
用 Python 揭秘 IP 地址背后的地理位置和信息
前端·python