FAQ: Objects - Pass By Reference

The explanation in the narrative partly covers it…

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!

How might this apply to your example?

I’ve been puzzling over this for a little while, I can’t suss it out. I keep getting stuck on this:

When we passed spaceship into that function, obj became a reference to the memory location of the spaceship object…

If I call that memory location 1234 then spaceship passes 1234 to obj as the location of that object, so the program now has the location to overwrite. I can’t see why it matters that the spaceship variable isn’t passed, if the variable is just the memory location anyway.
Does the new object get created in a different memory location as it’s called into existence, where it would never be connected to spaceship?

and will capture that, but not overwrite it since it is still assigned to another variable. It does not ‘see’ spaceship, only the value assigned. That value and any other changes will take up a new place in memory and belong to a new variable.

In JS we never refer to memory directly. The OS and the JS engine look after that. All we need to focus on is the references. So long as a reference exists, the value remains in memory. Once the references are removed or given new assignments the old values are garbage collected.

1 Like

That makes perfect sense now, thank you so much for taking the time to talk me through it

Like many others here this exercise has me confused. I have read the explanations - but like others they don’t seem to answer the question. Could it be the better explanation is that “spaceship” is a global variable, “obj” is an undeclared variable accessible only within the scope of the function? Therefore, after the function call “spaceship” still retains it’s original global value.

This sentence, “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.” is probably the most confusing I’ve come across so far. “space in memory” seems to come out of nowhere with no explanation. I understand it now thanks to other’s here on the forum but I think the folks at Codecademy could find a more clear way to explain this.

Ugh! That is some pretty confusing text. You were right to bring this issue up.

A variable is not assigned to an object. An object is assigned to a variable; so we’re clear.

If that object is a data structure, then when we pass the variable to a function, we are only passing what the variable refers to (a single reference in memory) and not the actual object. That’s what we need to understand. The parameter and the variable are one in the same hook. They are two threads tied to the same object.

From one language to another this concept is blurred. Each language will have its own dictates and nomenclature. High level languages tend to blur the lines to make things less imperative. For our purposes in JS, the above sets the boundaries for our application.

Why does reassignment dont stick when object name is passed as an argument and stick if we directly refer to it in the function body

let goat = {
sound:‘baa’,
name:‘billy’,
age:2
}

// reassignment does not stick when obj is passed as an argument
const reObj=(obj)=>{
obj={color:‘red’}
}
reObj(goat)
console.log(goat) // {sound:‘baa’, name:‘billy’, age:2}

// reassignment sticks when object is used directly and not as an argument
const reassignObject=()=>{
goat={color:‘red’}
}
reassignObject();
console.log(goat)// (color:red)

It is a good explanation: https://youtu.be/fD0t_DKREbE

How can a single address point to two different objects? i.e if spaceship is passed by reference to the reassignment function then obj variable must also be pointing to same location where spaceship variable is pointing. So when we reassign spaceship object must also change. If it is being passed by value then it doesn’t change, which is not the case here.

It’s the other way around. There are two references to a single spaceship object’s adress. Our original reference is stored in the variable ‘spaceship’. Object variables such as ‘spaceship’ hold the reference (address) to an object rather than the value itself (unlike a primitive data type variable such as an int which hold the actual value).

When we call a function with a spaceship parameter, we pass the reference stored in our ‘spaceship’ variable to the function. We still have our original ‘spaceship’ variable, and the function receives a copy of the reference to the spaceship object (let’s call it ‘spaceshipIn’). The function can re-assign ‘spaceshipIn’ to a new object but it won’t affect our ‘spaceship’ variable, which continues to reference the original object. However, when the function is called, ‘spaceship’ and ‘spaceshipIn’ refer to the same object, so if we use ‘spaceshipIn’ to change vaues in that object, those changes will persist after the function has returned, where we can see them, using the variable ‘spaceship’ to access the object.