1
00:00:01,210 --> 00:00:03,503
When copying arrays with primitive values

2
00:00:03,503 --> 00:00:05,919
in it, it's fine, but let's take a look at

3
00:00:05,919 --> 00:00:08,317
what happens when we copy arrays with

4
00:00:08,317 --> 00:00:13,396
objects in each element. Line 15, I'm

5
00:00:13,396 --> 00:00:16,443
saying let _products equal to an array,

6
00:00:16,443 --> 00:00:19,763
and each element of the array now is a

7
00:00:19,763 --> 00:00:23,222
literal object, a product object, and you

8
00:00:23,222 --> 00:00:26,216
can see I have three elements in here. I

9
00:00:26,216 --> 00:00:29,308
then have this function copyObjectArrays.

10
00:00:29,308 --> 00:00:34,460
And inside of here on line 45, I let diff

11
00:00:34,460 --> 00:00:38,490
= …_products. So this creates a copy, but

12
00:00:38,490 --> 00:00:41,149
since each one is an object, it's actually

13
00:00:41,149 --> 00:00:43,120
not copying the objects themselves. It's

14
00:00:43,120 --> 00:00:45,963
just copying the pointer to where that

15
00:00:45,963 --> 00:00:49,120
object is in memory. On line 48 I'm going

16
00:00:49,120 --> 00:00:52,867
to show this by doing diff subscript 0.

17
00:00:52,867 --> 00:00:57,092
productID = 999. If we then on lines 50

18
00:00:57,092 --> 00:01:00,275
and 51 spit out the original productID

19
00:01:00,275 --> 00:01:05,666
from _product subscript 0 and diff

20
00:01:05,666 --> 00:01:10,951
subscript 0.productID, we see that we

21
00:01:10,951 --> 00:01:13,750
changed the new one. We're actually

22
00:01:13,750 --> 00:01:16,030
changing the original because their

23
00:01:16,030 --> 00:01:22,000
objects, and objects are not copied by value. They're copied by reference.

