1
00:00:02,240 --> 00:00:08,170
Now if we talk about copying, we also have to talk about another way of copying values and for that, I reloaded

2
00:00:08,180 --> 00:00:15,380
the page and I'll again start with my name, Max object here and now if I want to create person2,

3
00:00:15,380 --> 00:00:19,670
of course we can use that spread operator which actually would be my recommendation because it's a really

4
00:00:19,670 --> 00:00:20,990
short syntax

5
00:00:21,230 --> 00:00:26,300
but there also is another approach which simply has been around for longer and has a bit of better browser

6
00:00:26,300 --> 00:00:32,090
support, though as you will learn in the browser support module, that might not matter for you and that

7
00:00:32,160 --> 00:00:40,040
other approach is to use object.assign. Now object is a globally available object actually in Javascript

8
00:00:40,370 --> 00:00:49,040
and it has an assign method where you pass in a target which is for example a new object you create

9
00:00:49,520 --> 00:00:54,120
and then the object where you want to copy over your values, like person.

10
00:00:54,180 --> 00:01:00,700
Now you can add multiple sources where you want to copy over key-value pairs and in the end, object.assign

11
00:01:00,700 --> 00:01:07,030
will spit out a new object here then and merge all the key-value pairs of the other arguments into

12
00:01:07,030 --> 00:01:11,030
that new object and you by the way don't have to use it to create a new object,

13
00:01:11,080 --> 00:01:16,180
you could have an existing other object which has its own properties already and merge in properties

14
00:01:16,180 --> 00:01:17,570
of other objects into that,

15
00:01:17,620 --> 00:01:23,260
that would work as well but a common use case which you'll see is also exactly this use case where you

16
00:01:23,260 --> 00:01:30,160
assign properties of an existing object to a new object with object.assign. By the way

17
00:01:30,160 --> 00:01:36,020
when I say properties, the same for the spread operator, I also always mean methods as well,

18
00:01:36,040 --> 00:01:37,900
so both is copied over.

19
00:01:38,020 --> 00:01:40,640
So here if I hit enter, you will see person2

20
00:01:40,780 --> 00:01:47,320
now also is a copy of person and it's actually a copy where we have a real new object as I can prove

21
00:01:47,350 --> 00:01:55,240
by changing the name to Maximilian for example, person2 still has Max here because here, we created

22
00:01:55,240 --> 00:02:01,340
a real new object and just merge the two key-value pairs into it just as we did with the spread operator.

23
00:02:01,360 --> 00:02:05,650
Now again, the spread operator would be my recommendation simply because it's a bit shorter.
