File tree Expand file tree Collapse file tree 1 file changed +17
-0
lines changed
14 - JavaScript References VS Copying Expand file tree Collapse file tree 1 file changed +17
-0
lines changed Original file line number Diff line number Diff line change 31
31
32
32
// or create a new array and concat the old one in
33
33
34
+ const concatPlayers = [ ] . concat ( players )
35
+ concatPlayers [ 3 ] = "Tobi"
36
+ console . log ( concatPlayers )
37
+
34
38
// or use the new ES6 Spread
35
39
40
+ const spreadPlayers = [ ...players ]
41
+ spreadPlayers [ 3 ] = "Tobi"
42
+ console . log ( spreadPlayers )
43
+ console . log ( players )
36
44
// now when we update it, the original one isn't changed
37
45
38
46
// The same thing goes for objects, let's say we have a person object
44
52
} ;
45
53
46
54
// and think we make a copy:
55
+ const parsePerson = JSON . parse ( JSON . stringify ( person ) )
56
+ console . log ( parsePerson )
57
+ const spreadPerson = { ...person }
58
+ console . log ( spreadPerson )
59
+
60
+ person . age = 21
61
+ spreadPerson . age = 25
62
+
63
+ console . log ( `Person is ${ person . age } years old, while spread person is ${ spreadPerson . age } years old` )
47
64
48
65
// how do we take a copy instead?
49
66
You can’t perform that action at this time.
0 commit comments