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

相关推荐
Rhys..16 分钟前
如何禁止chrome自动更新
前端·chrome
巴巴_羊23 分钟前
AJAX 使用 和 HTTP
前端·http·ajax
刺客-Andy30 分钟前
React 第四十一节Router 中 useActionData 使用方法案例以及注意事项
前端·react.js·前端框架
岁岁岁平安35 分钟前
Vue3学习(组合式API——reactive()和ref()函数详解)
前端·javascript·vue.js·学习·vue3·reactive·ref
肠胃炎37 分钟前
React事件机制
前端·javascript·react.js
CUIYD_198944 分钟前
javascript —— ! 和 !! 的区别与作用
前端·javascript·vue.js
帅帅哥的兜兜2 小时前
next.js实现项目搭建
前端·react.js·next.js
筱歌儿2 小时前
css 左右布局
前端·css
GISer_Jing3 小时前
编译原理AST&以Babel为例进行解读、Webpack中自定义loader与plugin
前端·webpack·node.js
GISer_Jing3 小时前
Webpack中Compiler详解以及自定义loader和plugin详解
前端·webpack·node.js