For example we have an object:

const todo = {
text: "Water the flowers",
completed: false,
tags: ["garden"]
};

We shallow copy it:

const shallowCopy = { ...todo };

Verify that shallowCopy is not todo:

console.log(todo === shallowCopy) // false

Change text prop of shallowCopy to somethingelse:

shallowCopy.text = "Mow the lawn"; 

console.log(shallowCopy.text) //  "Mow the lawn";
console.log(todo.text); // "Water the flowers"

But if we want to push a new value to the tags array:

shallowCopy.tags.push("weekend");

Then we can find out that, both shallowCopy and todo object's tags both changed.

The reason for that is the shallow copy's array prop, still point to the original reference. We need to do a deep clone in order to avoid the mistake.

[Javascript] Different ways to create an new array/object based on existing array/object

最新文章

  1. mvc SelectList selected失效的解决方法
  2. position之fixed固定定位、absolute绝对定位和relative相对定位
  3. Objective-C 中类属性(修饰)
  4. C#装箱和拆箱(值类型和引用类型之间的转换)
  5. javascript对象与实例
  6. C# 中的值类型和引用类型
  7. 超强vim配置文件
  8. js获取每个按键的ASCII值
  9. Linux 下安装python软件包
  10. Why is celsius = 5 * (fahr - 32) / 9 ?
  11. iOS开发播放文本
  12. Django models Fild详解
  13. 一个普通的 Zepto 源码分析(三) - event 模块
  14. 201521123018 《Java程序设计》第3周学习总结
  15. SSH反向连接及Autossh
  16. (转)Spring Boot(十八):使用 Spring Boot 集成 FastDFS
  17. oracle故障解决
  18. Android逆向 破解第一个Android程序
  19. Redis存读取数据
  20. linux中tomcat内存溢出解决办法

热门文章

  1. bzoj1618 购买干草
  2. cal---显示日历
  3. 中断函数中不能使用printf的原因
  4. CodeForces 337A Puzzles
  5. PipeCAD之管道标准库PipeStd(2)
  6. 罗列几个Android插件化开发框架
  7. vim7.4官方源码在vs2013的编译方法及问题总结
  8. route---设置Linux内核中的网络路由表
  9. HOJ——T 2430 Counting the algorithms
  10. 怎样借助log4j把日志写入数据库中