0

As I understand, object references x in x = { greet: hi } stores a reference to object { greet:hi} unlike primitive types that hold actual values ( y=10)

In the following code, console.log(y) outputs {greet: "hi"}.

Why does y object reference is not updated to point to {greet: 'hello'} when x obj ref is updated to point to x = {greet: 'hello'}

var x = {greet: 'hi'};
var y = x;
x = {greet: 'hello'};

console.log(y);
console.log(x)
dkjain
  • 831
  • 1
  • 9
  • 36
  • What you describe would mean that `y = x` makes `y` and *alias* of `x`, i.e. that they're both the same variable really. That's not how it works. You're merely assigning the object reference stored in `x` to `y`. – deceze Dec 20 '17 at 08:51
  • @deceze Not sure if this is apt duplicate. – Rajesh Dec 20 '17 at 08:54
  • @OP, objects are assigned/copied using reference. What this means is, you are exchanging memory location and the accessing property of the object at this memory location. Hence changing property in one variable reflect in others. Now when you do `x = {greet: 'hello'};`, you are replacing the reference itself. So now the variable will hold pointer to some other location and object at previous location would remain as is. If no one is referencing it, GC will get rid of it. – Rajesh Dec 20 '17 at 08:57
  • @deceze Sure, I checked the referred question. Although there are some commonalities regarding the nature of both question but they aren't exactly same. – dkjain Dec 20 '17 at 08:57
  • @Rajesh Meh… at the very least this question definitely gets answered several times a month, and we don't need yet another copy of it. If you have a better suggestion for a dupe target, by all means… – deceze Dec 20 '17 at 08:58
  • @dkjain Perhaps you can tell us why you think it should be working as you expect…? What do you think is the underlying mechanism at work here? There's clearly some misconception to be eradicated here. – deceze Dec 20 '17 at 08:59
  • @deceze I was actually looking for it. I'm sure I have commented my above comment few times. Would tag you if I find a correct one. – Rajesh Dec 20 '17 at 08:59

2 Answers2

1

Because in line x = {greet: 'hello'}; a new object is being created.

Use x.greet = 'hello'; to update old object.

var x = {greet: 'hi'};
var y = x;
x.greet = 'hello';

document.getElementById("text_show").innerHTML= x.greet + " | " + y.greet;
<div id="text_show"></div>

See here to know about objects in JavaScript
cse
  • 4,066
  • 2
  • 20
  • 37
  • Indeed. Basically, you'd see what you expect (@OP), if you'd change x by saying `x.greet = "hello"`. In this case `y` would look like `x` – Adelin Dec 20 '17 at 08:50
0

Why does y object reference is not updated to point to {greet: 'hello'} when x obj ref is updated to point to x = {greet: 'hello'}

Because you have assigned a new reference to x while y is still pointing to original one.

When you say

var y = x;

you are pointing y to the same reference that x was pointing to, you are not pointing y to x.

gurvinder372
  • 66,980
  • 10
  • 72
  • 94
  • `you are not pointing y to x.` exactly, x is a reference and that reference is also copied to `y` when I write `var y = x` so when that reference changes in `x` that change should propagate to `y` also. Right? – dkjain Dec 20 '17 at 08:54
  • *so when that reference changes in x that change should propagate to y also* No, if the reference value itself has changed then x will point to a new reference (new object) while y will keep pointing to older one. If the value is changed for example `var x = {greet: 'hi'}; var y = x; x.greet = 'hello';` then `y.greet` would also have changed. – gurvinder372 Dec 20 '17 at 08:57
  • Found a more direct explanation [here](https://stackoverflow.com/questions/639514/how-can-i-get-the-memory-address-of-a-javascript-variable). > Javascript's evaluation strategy is to always use call by value, but in the case of Objects (including arrays) the value passed is a reference to the Object, which is not copied or cloned. If you reassign the Object itself in the function, the original won't be changed, but if you reassign one of the Object's properties, that will affect the original Object. – dkjain Dec 20 '17 at 10:43