In Javascript, when I use the extend function in the underscore.js library... can somebody please conceptually / visually describe to me what happens in the back-end in regards with memory - with this example:
var obj = {hello: [2]};
var obj2 = {hola: [4]};
_.extend(obj, obj2)
obj2.hola = 5;
console.log(obj) // hola still has a value of `[4]`
My issue is if I console.log(obj), for some reason, hola still has a value of [4]
. I fully thought I would get the value 5 (via pass by reference)...
For the above example, here is what is visually / conceptually going on in my head:
The extend function deeply copies the key
hola
into obj:obj = {hello: [2], hola: TBD} ]btw - is obj storing only one memory address for this object?
Then I suspect that hola stores a memory address to the value of
[4]
(so at this point I suspect obj would beobj = {hello: [2], hola: #0x93490234}
Which is why I fully expected to see a 5 under obj. Can you tell me what's wrong with my visualization above?
Lastly, with the explanation, can you point out how the above example is any different than the following example (I understand how / why the below example works - just not the above example and would like to hear why the below works and the above doesn't).
var obj2 = {hola: [4]};
var obj = obj2;
obj2.hola = 5; //console.log(obj) will say that hola equals 5