|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | +<head> |
| 4 | + <meta charset="UTF-8"> |
| 5 | + <title>JS Reference VS Copy</title> |
| 6 | +</head> |
| 7 | +<body> |
| 8 | + |
| 9 | + <script> |
| 10 | + // 从 String、Number、Boolean 类型的值开始: |
| 11 | + // let age = 100; |
| 12 | + // let age2 = age; |
| 13 | + // console.log(age, age2); |
| 14 | + // age = 200; |
| 15 | + // console.log(age, age2); |
| 16 | + |
| 17 | + // let name = 'Wes'; |
| 18 | + // let name2 = name; |
| 19 | + // console.log(name, name2); |
| 20 | + // name = 'wesley'; |
| 21 | + // console.log(name, name2); |
| 22 | + |
| 23 | + // 下面我们来声明一个数组 |
| 24 | + const players = ['Wes', 'Sarah', 'Ryan', 'Poppy']; |
| 25 | + |
| 26 | + // 然后试图复制这个数组 |
| 27 | + const team = players; |
| 28 | + |
| 29 | + console.log(players, team); |
| 30 | + // You might think we can just do something like this: |
| 31 | + // 也许你觉得可以直接这样修改复制后的数组 |
| 32 | + // team[3] = 'Lux'; |
| 33 | + |
| 34 | + // however what happens when we update that array? |
| 35 | + // 但我们修改这个数组的时候会发生什么呢? |
| 36 | + |
| 37 | + // now here is the problem! |
| 38 | + // 这就是问题所在 |
| 39 | + |
| 40 | + // oh no - we have edited the original array too! |
| 41 | + // NO! 原数组也被修改过了 |
| 42 | + |
| 43 | + // Why? It's because that is an array reference, not an array copy. They both point to the same array! |
| 44 | + // 为什么?因为 team 只是这个数组的引用,并不是它的复制。team 和 players 指向的是同一个数组。 |
| 45 | + |
| 46 | + // So, how do we fix this? We take a copy instead! |
| 47 | + // 所以如何解决这个问题?下面来进行复制。 |
| 48 | + const team2 = players.slice(); |
| 49 | + |
| 50 | + // one day |
| 51 | + |
| 52 | + // or create a new array and concat the old one in |
| 53 | + // 或者创建一个新数组,然后用 concat 方法来获取它 |
| 54 | + const team3 = [].concat(players); |
| 55 | + |
| 56 | + // or use the new ES6 Spread |
| 57 | + // 再或者用 ES6 里面的[扩展语法](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/Spread_operator) |
| 58 | + const team4 = [...players]; |
| 59 | + team4[3] = 'heeee hawww'; |
| 60 | + console.log(team4); |
| 61 | + |
| 62 | + const team5 = Array.from(players); |
| 63 | + |
| 64 | + // now when we update it, the original one isn't changed |
| 65 | + // 现在再修改 team5 的时候,原数组不会变了 |
| 66 | + |
| 67 | + // The same thing goes for objects, let's say we have a person object |
| 68 | + // 对 Object 类型的数据来说也是一样的,我们用一个 person 例子来说明 |
| 69 | + |
| 70 | + // with Objects |
| 71 | + // 现在又一个 Object 对象 |
| 72 | + const person = { |
| 73 | + name: 'Wes Bos', |
| 74 | + age: 80 |
| 75 | + }; |
| 76 | + |
| 77 | + // 然后思考一下如何取得它的复制 |
| 78 | + // and think we make a copy: |
| 79 | + // const captain = person; |
| 80 | + // captain.number = 99; |
| 81 | + |
| 82 | + // how do we take a copy instead? |
| 83 | + // 如何才能复制它呢? |
| 84 | + const cap2 = Object.assign({}, person, { number: 99, age: 12 }); |
| 85 | + console.log(cap2); |
| 86 | + |
| 87 | + // We will hopefully soon see the object ...spread |
| 88 | + // const cap3 = {...person}; |
| 89 | + |
| 90 | + // Things to note - this is only 1 level deep - both for Arrays and Objects. lodash has a cloneDeep method, but you should think twice before using it. |
| 91 | + // 需要注意的是:这里的例子里面,我们用的数组和对象都只是一层嵌套,Lodash 有一个深度复制的方法,但你使用之前需要多考虑一下。 |
| 92 | + |
| 93 | + const wes = { |
| 94 | + name: 'Wes', |
| 95 | + age: 100, |
| 96 | + social: { |
| 97 | + twitter: '@wesbos', |
| 98 | + facebook: 'wesbos.developer' |
| 99 | + } |
| 100 | + }; |
| 101 | + |
| 102 | + console.clear(); |
| 103 | + console.log(wes); |
| 104 | + |
| 105 | + const dev = Object.assign({}, wes); |
| 106 | + |
| 107 | + const dev2 = JSON.parse(JSON.stringify(wes)); |
| 108 | + |
| 109 | + |
| 110 | + </script> |
| 111 | + |
| 112 | +</body> |
| 113 | +</html> |
0 commit comments