6
6
// (even when the object is assigned to a const variable).
7
7
8
8
const spaceship = {
9
- homePlanet : 'Earth' ,
10
- color : 'silver'
9
+ homePlanet : 'Earth' ,
10
+ color : 'silver'
11
11
} ;
12
-
12
+
13
13
let paintIt = obj => {
14
- obj . color = 'glorious gold'
14
+ obj . color = 'glorious gold' ;
15
15
} ;
16
-
16
+
17
17
paintIt ( spaceship ) ;
18
-
18
+
19
19
spaceship . color // Returns 'glorious gold'
20
-
20
+ console . log ( spaceship ) ; // {homePlanet: 'Earth', color: 'glorious gold'}
21
+
21
22
// Our function paintIt() permanently changed the color of our spaceship object.
22
23
// However, reassignment of the spaceship variable wouldn’t work in the same way:
23
24
24
25
let spaceship2 = {
25
- homePlanet : 'Earth' ,
26
- color : 'red'
26
+ homePlanet : 'Earth' ,
27
+ color : 'red'
27
28
} ;
29
+
28
30
let tryReassignment = obj => {
29
31
obj = {
30
- identified : false ,
31
- 'transport type' : 'flying'
32
+ identified : false ,
33
+ 'transport type' : 'flying'
32
34
}
33
- console . log ( obj ) // Prints {'identified': false, 'transport type': 'flying'}
34
-
35
+ console . log ( obj ) ; // Prints {'identified': false, 'transport type': 'flying'}
35
36
} ;
36
- tryReassignment ( spaceship2 ) // The attempt at reassignment does not work.
37
- spaceship2 // Still returns {homePlanet : 'Earth', color : 'red'};
38
-
37
+
38
+ tryReassignment ( spaceship2 ) ; // The attempt at reassignment does not work.
39
+ console . log ( spaceship2 ) ; // Still returns {homePlanet : 'Earth', color : 'red'};
40
+
39
41
spaceship2 = {
40
- identified : false ,
42
+ identified : false ,
41
43
'transport type' : 'flying'
42
44
} ; // Regular reassignment still works.
43
45
// Let’s look at what happened in the code example:
@@ -56,23 +58,36 @@ spaceship2 = {
56
58
// while the spaceship variable was completely unchanged from its earlier value.
57
59
58
60
59
- let spaceship3 = {
60
- 'Fuel Type' : 'Turbo Fuel' ,
61
- homePlanet : 'Earth'
62
- } ;
63
-
64
- // Func setting objects 'Fuel Type' to 'avocado oil'
65
- // using concise form arrow functions
66
- let greenEnergy = obj =>
67
- obj [ 'Fuel Type' ] = 'avocado oil'
68
-
69
- // Func setting objects disabled property to true
70
- let remotelyDisable = obj =>
71
- obj . disabled = true
72
-
73
- // call and log spaceship object
74
- greenEnergy ( spaceship3 ) ;
75
- remotelyDisable ( spaceship3 ) ;
76
-
77
- console . log ( spaceship3 ) ;
78
-
61
+ const spaceship3 = {
62
+ 'Fuel Type' : 'Turbo Fuel' ,
63
+ homePlanet : 'Earth'
64
+ } ;
65
+
66
+ // Func setting objects 'Fuel Type' to 'avocado oil'
67
+ // using concise form arrow functions
68
+ let greenEnergy = obj =>
69
+ obj [ 'Fuel Type' ] = 'avocado oil'
70
+
71
+ // Func setting objects disabled property to true
72
+ let remotelyDisable = obj =>
73
+ obj . disabled = true
74
+
75
+ // call and log spaceship object
76
+ greenEnergy ( spaceship3 ) ;
77
+ remotelyDisable ( spaceship3 ) ;
78
+
79
+ spaceship3 . homePlanet = 'Mars' ;
80
+ // spaceship3 = {}; // TypeError: Assignment to const variable
81
+ // had spaceship3 been defined using let reassignment would work
82
+ console . log ( spaceship3 ) ;
83
+
84
+
85
+ // JS objects are mutable even when declared with const, it is the reference to the object, bound to the variable, that cannot be changed.
86
+
87
+ const cat = { hair : 'long' , cold : false }
88
+
89
+ cat . hair = 'short' ;
90
+ cat . cold = true ;
91
+
92
+ // cat = { } // TypeError: Assignment to constant variable
93
+ console . log ( cat ) ; // { hair: 'short', cold: true }
0 commit comments