Objects

  • 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.

So, there are now two places in memory? One that the spaceship variable references and one that the obj variable references?

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.

Let me know! :slight_smile:

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.

I wondered what you meant when you said they were in the sames scope since one existed inside a function.

1 Like

I had my wires crossed. Try this…

let foo = () => {
    bar = "Bar"
}
foo()
console.log(bar)    // Bar

In the above example, bar has global scope since it is declared without a keyword. Were it the parameter, though, it would have local scope.

1 Like

are let and const keywords?

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.

You can create values.
And you can make variables refer to values.
Assignment never copies.

I think that covers it.


And no this isn’t right

let tryReassignment = obj => ...
tryReassignment(spaceship)

That’s assignment and that doesn’t create a value so no there aren’t two locations in memory.

Assignment does not copy. Ever.
Creating things is an unrelated action from assignment.
If you ever need to make a copy, you will need to

hey_make_a_copy_of_this_please(this_here_thing)