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'
Our function paintIt()
permanently changed the color of our spaceship
object. However, reassignment of the spaceship
variable wouldn’t work in the same way:
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.
Let’s look at what happened in the code example:
- We declared this
spaceship
object withlet
. This allowed us to reassign it to a new object withidentified
and'transport type'
properties with no problems. - When we tried the same thing using a function designed to reassign the object passed into it, the reassignment didn’t stick (even though calling
console.log()
on the object produced the expected result). - When we passed
spaceship
into that function,obj
became a reference to the memory location of thespaceship
object, but not to thespaceship
variable. This is because theobj
parameter of thetryReassignment()
function is a variable in its own right. The body oftryReassignment()
has no knowledge of thespaceship
variable at all! - When we did the reassignment in the body of
tryReassignment()
, theobj
variable came to refer to the memory location of the object{'identified' : false, 'transport type' : 'flying'}
, while thespaceship
variable was completely unchanged from its earlier value.
Instructions
Write a function greenEnergy()
that has an object as a parameter and sets that object’s 'Fuel Type'
property to 'avocado oil'
.
Write a function remotelyDisable()
that has an object as a parameter and sets (or reassigns) that object’s disabled
property to true
.
Call your two functions with the spaceship
object in the code editor, then console.log()
the spaceship
object to confirm those properties were changed/added.