FAQ: Objects - Pass By Reference

The explanation object -pass by reference concept is confusing .
In the codecademy code example i added a line and am getting ‘Earth’ inside the function but the object in function reassignment fails as stated. So codecademy explanation does not make sense:" When we passed spaceship into that function, obj became a reference to the memory location of the spaceship object, but not to the spaceship variable. This is because the obj parameter of the tryReassignment() function is a variable in its own right. The body of tryReassignment() has no knowledge of the spaceship variable at all!" what now???

If the " The body of tryReassignment() has no knowledge of the spaceship variable at all!" then how come my obj.homePlanet returns Earth? And after reassignment returns Undefined.
please clarify
Thanks

let tryReassignment = obj => {
console.log(obj.homePlanet); // Earth <— I added this Line

obj = {
identified : false,
‘transport type’ : ‘flying’
}
console.log(obj.homePlanet); // Prints : Undefined <------ i added this
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.

To Force it to work as I want it to I added return obj and then assigned returned object to spaceship and following works!!! (but the previous confusion still needs clarification):
let spaceship = {
homePlanet : ‘Earth’,
color : ‘red’
};

let tryReassignment = obj => {
obj = {
identified : false,
‘transport type’ : ‘flying’
}
console.log(obj) // Prints {‘identified’: false, ‘transport type’: ‘flying’}
return obj; //<---- I added this line

};
spaceship = tryReassignment(spaceship) // WORKS!!! { identified: false, ‘transport type’: ‘flying’ } <---- I added this line
console.log(spaceship) // Prints { identified: false, ‘transport type’: ‘flying’ }

spaceship = {
identified : false,
‘transport type’: ‘flying’
}; // Regular reassignment still works.
console.log(spaceship) // Prints {‘identified’: false, ‘transport type’: ‘flying’}

To preserve code formatting in forum posts, see: [How to] Format code in posts
Your code:

let spaceship = {
  homePlanet : 'Earth',
  color : 'red'
};

let tryReassignment = obj => {
  console.log(obj.homePlanet); // Earth <— I added this Line
  obj = {
    identified : false, 
    'transport type' : 'flying'
  }
  console.log(obj.homePlanet); // Prints : Undefined <------ i added this
  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.

You can right click, open the images in new tabs and zoom for better readability,



Once you make the function call tryReassignment(spaceship), the memory location of the object assigned to the variable spaceship will be assigned to the parameter obj. The parameter obj will hold the memory location of the object, it will have no knowledge that the same object is also assigned to the variable spaceship.

console.log(obj.homePlanet); // Earth

The object that obj points to has a property named homePlanet with a value of "Earth", so this value is printed.
You then assign a new object (not mutating/modifying existing object) and assign it to obj. This new object has identified and 'transport type' as properties, but doesn’t have any property named homePlanet, therefore

console.log(obj.homePlanet); // undefined

will print undefined.

1 Like

Thank you for the clarification.appreciate it!

1 Like