Pass by Reference (object)

Hi all,

I need some help with understanding pass by reference for objects.

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'};

Above is the example in the course.
I’m still confused as to why the original object, “spaceship” is not changed after calling the function “tryReassignment”.

Instead of

let tryReassignment = obj => {
  obj = {
    identified : false,
    'transport type' : 'flying'
  }
};

if we alter the function to be

let tryReassignment = obj => {
  obj.identified = false;
  obj['transport type'] = 'flying';
}

this would’ve modified the object, and not sure what the difference is.
Sure, instead of re-assigning, now the spaceship will have four keys instead of two as the function originally intended.
[side question: if we want to achieve what the function is originally doing, is the best method to recursively remove all keys and add two?]

In both cases, spaceship object is passed by reference (let’s say memory addr 0x0022) and therefore obj refers to memory address 0x0022.
If we assign new object to that address, regardless of the function knowing “spaceship” variable, it should overwrite that memory space. And next time when we call that memory location, it should return the new data we just put in.

Back to the example, after calling the function and print “spaceship”, not sure why it is unmodified.

Thank you for all your help!

A function should return something, right?

Sorry, not sure I follow.
Neither functions return any.
The 2nd function which I modified, tries to change the object within the fuction and don’t return any.

Hello, and welcome to the forums!

The way I keep the idea straight in my head is that when spaceship is passed to the function, obj is assigned a reference to the same object. However, when assigning a new object to obj from within the function, what’s being replaced is the reference to the object. So now instead of both spaceship and obj referring to the same object, they now refer to two different objects.

As you’ve seen already, as long as obj keeps the same reference, then changes to the object are carried over.

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

let tryReassignment = obj => {
  // delete all existing keys but, 
  // not the object reference itself
  Object.keys(obj).forEach(key => delete obj[key]);

  // obj (and spaceship) would now be an empty object

  // give it new keys / values by targeting the
  // existing object reference without replacing it
  Object.assign(obj, {
    identified: false,
    'transport type': 'flying'
  });

  // You could use dot notation or bracket notation, 
  // but the method above is closest to what you were
  // doing before.

  console.log(obj);
};

console.log(spaceship);
tryReassignment(spaceship);
console.log(spaceship);
2 Likes

Hello @tag9763606779, welcome to the forums! This article might be of help for you.


EDIT: Wow, @selectall; didn’t even notice you were replying. I’m sure selectall’s answer is much better than my link, but now you’ve got two sources!

2 Likes