Skip to content

Commit 0df08fb

Browse files
committed
objects references declared with const
1 parent 2693075 commit 0df08fb

File tree

1 file changed

+52
-37
lines changed

1 file changed

+52
-37
lines changed

objectsPassByRef.js

+52-37
Original file line numberDiff line numberDiff line change
@@ -6,38 +6,40 @@
66
// (even when the object is assigned to a const variable).
77

88
const spaceship = {
9-
homePlanet : 'Earth',
10-
color : 'silver'
9+
homePlanet: 'Earth',
10+
color: 'silver'
1111
};
12-
12+
1313
let paintIt = obj => {
14-
obj.color = 'glorious gold'
14+
obj.color = 'glorious gold';
1515
};
16-
16+
1717
paintIt(spaceship);
18-
18+
1919
spaceship.color // Returns 'glorious gold'
20-
20+
console.log(spaceship); // {homePlanet: 'Earth', color: 'glorious gold'}
21+
2122
// Our function paintIt() permanently changed the color of our spaceship object.
2223
// However, reassignment of the spaceship variable wouldn’t work in the same way:
2324

2425
let spaceship2 = {
25-
homePlanet : 'Earth',
26-
color : 'red'
26+
homePlanet: 'Earth',
27+
color: 'red'
2728
};
29+
2830
let tryReassignment = obj => {
2931
obj = {
30-
identified : false,
31-
'transport type' : 'flying'
32+
identified: false,
33+
'transport type': 'flying'
3234
}
33-
console.log(obj) // Prints {'identified': false, 'transport type': 'flying'}
34-
35+
console.log(obj); // Prints {'identified': false, 'transport type': 'flying'}
3536
};
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+
3941
spaceship2 = {
40-
identified : false,
42+
identified: false,
4143
'transport type': 'flying'
4244
}; // Regular reassignment still works.
4345
// Let’s look at what happened in the code example:
@@ -56,23 +58,36 @@ spaceship2 = {
5658
// while the spaceship variable was completely unchanged from its earlier value.
5759

5860

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

Comments
 (0)