这是克隆ES6中对象的好方法吗? [英] Is this a good way to clone an object in ES6?

查看:164
本文介绍了这是克隆ES6中对象的好方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谷歌搜索javascript克隆对象带来了一些非常奇怪的结果,其中一些是绝望的过时而有些过于复杂,不是那么简单:

Googling for "javascript clone object" brings some really weird results, some of them are hopelessly outdated and some are just too complex, isn't it as easy as just:

let clone = {...original};

这有什么问题吗?

推荐答案

完全可以接受,甚至更多,现在对象传播是第3阶段。人们过于复杂。

Totally acceptable, even moreso now that object spread is stage 3. People over complicate things.

const clone = {... original} 克隆

const newobj = {... original,prop:newOne} 以不可变的方式将另一个道具添加到原始文件并存储为新对象。

const newobj = {...original, prop: newOne} to immutably add another prop to original and store as a new object.

这篇关于这是克隆ES6中对象的好方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆