这是克隆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.