0

Let us consider an example

    var a = {}
    c = a
    
    var a = {'abc':12}
    var b = 10;
    
    console.log(c)

So my understanding of hoisting is that in the creation phase of execution context a being undefined. But in the next line a is assigned to {} and so is c. When it goes to the next line a is assigned the object {'abc':12} . I have read that objects are assigned by reference. Then why does c not refer the new object instead of {}.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
leo
  • 103
  • 1
  • 10
  • 5
    `a = {'abc':12}` assigns a new value - a new reference - to `a`. The fact that it previously held a reference to the same value as the reference held in `c` doesn't matter. You would observe the "referenceness" if you *mutated* `a` (say `a.foo = 'bar'`), but assignment is something different. – Robin Zigmond Aug 26 '20 at 17:47
  • since c is poting to the memory location of a (being an object) shouldn't the fact that reassigning a should also change c ? – leo Aug 26 '20 at 17:48
  • 1
    No. If it worked like that, how would you *ever* give `a` or `c` different values from each other? – Robin Zigmond Aug 26 '20 at 17:49
  • 6
    Visualize `a` and `c` as two people. Both are pointing at a truck. If the first person changes to point at a car, that doesn't affect the other person at all. They are still pointing at the same truck. – Taplar Aug 26 '20 at 17:50
  • 2
    `var a = {}; c = a; a.foo = 1; console.log(c);` will show that they are indeed referencing the same object which is now `{ foo: 1 }`, so you're right in that they are referencing the same object. When you do `var a = {'abc': 12}`, you're now telling `a` to reference a brand new object. `c` is still referencing the old object. – MattDiamant Aug 26 '20 at 17:58
  • 1
    that was a good explanation. Thanks for replying. I would normally assume that (coming from a JAVA background) but JS acts mystically sometimes :D – leo Aug 26 '20 at 18:02
  • 1
    Assignment by reference (or rather, not) has nothing to do with hoisting of declarations. – Bergi Aug 26 '20 at 18:29

2 Answers2

0
  1. Visit this link to understand about value vs referencing in js: https://codeburst.io/explaining-value-vs-reference-in-javascript-647a975e12a0
  2. When you reassign an object(in this case object a), what happens under the hood is that javascript engine performs garbage collection, since object c was already referencing {}, the engine can't delete {} object from the memory. Therefore, c still references to {}.
0

The second line of your code is the important bit to understand.

var a = {} // This assigns {} to a variable called a
    c = a  // This creates another variable called c, and assigns {} to c.

The assignment operation looks at the contents of a, and puts the contents into c.

If it helps, you could also think of it as though the variable c has no knowledge of the existence of variable a, it merely "looks through" it to its value {} when it is being assigned that value in the line c = a.

ed2
  • 1,457
  • 1
  • 9
  • 26
  • "*The variable c […] looks through [variable a]*" is more confusing than helpful. The variable doesn't look at anything at all. The assignment operation looks at the contents of `a`, yes, and puts those into `c`. – Bergi Aug 26 '20 at 18:27
  • @Bergi, I have updated the answer accordingly. One person's confusion can sometimes represent a useful analogy to a different reader. The answer now supplies both, and clarifies. Thanks. – ed2 Aug 26 '20 at 18:31