-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpass-by-reference.js
63 lines (48 loc) · 1.46 KB
/
pass-by-reference.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//Objects are passed by reference.
//This means when we pass a variable assigned to an object
//into a function as an argument, the computer interprets the parameter
//name as pointing to the space in memory holding that object. As a result,
//functions which change object properties actually mutate the
//object permanently (even when the object is assigned to a const variable).
const spaceship = {
homePlanet : 'Earth',
color : 'silver'
};
let paintIt = obj => {
obj.color = 'glorious gold'
};
paintIt(spaceship);
spaceship.color // Returns 'glorious gold'
//
let spaceship = {
homePlanet : 'Earth',
color : 'red'
};
let tryReassignment = obj => {
obj = {
identified : false,
'transport type' : 'flying'
}
console.log(obj) // Prints {'identified': false, 'transport type': 'flying'}
};
tryReassignment(spaceship) // The attempt at reassignment does not work.
spaceship // Still returns {homePlanet : 'Earth', color : 'red'};
spaceship = {
identified : false,
'transport type': 'flying'
}; // Regular reassignment still works.
// Another Example
let spaceship = {
'Fuel Type' : 'Turbo Fuel',
homePlanet : 'Earth'
};
// Write your code below.
let greenEnergy=obj=>{
obj['Fuel Type']='avocado oil';
};
let remotelyDisable=obj=>{
obj.disabled=true;
};
greenEnergy(spaceship);
remotelyDisable(spaceship);
console.log(spaceship);