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!
When we did the reassignment in the body of tryReassignment() , the obj variable came to refer to the memory location of the object {'identified' : false, 'transport type' : 'flying'} , while the spaceship variable was completely unchanged from its earlier value.
I’m having a hard time understanding the concepts here. I understand that values are stored at a location in memory, but if we change that value at the memory location, wouldn’t it affect both spaceship and obj?
Values at memory locations do not change. If we assign a new value, we get a new memory location and the old memory is garbage collected (freed up) if there are no other references to it.
In the case of objects and arrays, we can only change the values within the object. When two or more variables refer to the same object they can all see the changes made to the object since the references are still the same. When we assigned a new object to the variable obj it became unbound from the original reference and took on bindings to the new reference. It did nothing to the binding of spaceship which remains bound to its original object.
Correct; and, since obj is defined only, and not declared with var, let or const it is in the same scope as spaceship so we should be able to access it. Going to test this assertion, now. BRB.
Assertion is false. obj is not accessible outside of the function scope. This is different behavior than we are used to in conventional ES5 function syntax. It must be because let does not create any bindings to the window object (or perhaps it is to do with the arrow function?).
Going to try with conventional syntax and see what happens. BRB
D’oh! Parameters are locally declared. Sheesh! They cannot be accessed from outside. obj simply evaporates when the function is exited.
Yes, declaration keywords along with var which is less used since it only gives function scope, not block scope. It also creates properties on the window.object but that’s for another study, later on down the road.