Why can't objects be reassigned inside a function?

Can someone please clarify why objects cannot be reassigned inside of a function?
Correct me if I’m wrong - JavaScript passes a copy of the value to the parameter variable
and in the case of objects it passes a copy of the objects reference value to the parameter variable, but
wouldn’t the copy and original reference both point to the same place?

Example from Objects lesson 8:


I think this has to do with it being a copy of the object reference, like you mentioned. Somewhere under the hood, if the entire object gets reassigned while working with the copy of the reference, the original reference is left intact. It feels like a copy of a pointer, so if you reassign, it simply gives your copied reference a new pointer.

There are ways around this though. You could mimic the behavior you’re after with something like this:

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'


Instead of using Object.assign() like I did above, you could just give it properties using other notation.

  obj.identified = false;
  obj['transport type'] = 'flying';

1 Like

I figured there had to be some magique going on under the hood, hah! Thanks for the example!

1 Like