-4

I have code which is similar to

emp1 = emp;

emp= emp2;

if objects are by reference then why emp1 is not equal to emp2?

why emp1 prints "shilpa" instead of "manish" .

var emp = 
    {
      name : "manish",
      number :  10
    }


var emp1 = emp;

var emp2 = 
    {
      name : "shilpa",
      number :  20
    }

emp = emp2;

console.log(emp);
console.log(emp1);
console.log(emp2);
Manish
  • 1,246
  • 3
  • 14
  • 26
  • `emp1` prints `manish` – zerkms Jun 04 '17 at 01:03
  • 1
    1) `emp` is assigned a reference to an object in memory 2) `emp1` is assigned to the reference `emp` is assigned to 3) `emp2` is assigned a difference reference to an object in memory 4) `emp` is assigned to `emp2`'s reference, there's no reason for `emp1`'s reference to change. – Andrew Li Jun 04 '17 at 01:04
  • In other words - in JS there is no way to change the variable's value other than explicitly assigning to it. – zerkms Jun 04 '17 at 01:05
  • 1
    The references are to objects, not variables. (This is the same way Java, C#, Ruby, and Python work, for example.) – Ry- Jun 04 '17 at 01:06
  • @Manish variables are just a pointers to location in memory, `emp` just keeps the address to object, and then You pass that pointer (address) to `emp1`. – num8er Jun 04 '17 at 01:10
  • @AndrewLi thanks for your reply, but emp1 is pointing to emp .. thats where i,m getting confused... – Manish Jun 04 '17 at 01:11
  • @Manish `emp1` doesn't point to `emp`. As Ryan mentioned, references point to objects in memory, not variables. `emp` points to a reference. – Andrew Li Jun 04 '17 at 01:12
  • @Manish watch this: https://www.youtube.com/watch?v=9ooYYRLdg_g explains enough simple – num8er Jun 04 '17 at 01:13
  • why negative rating .. common guys... – Manish Jun 04 '17 at 01:15
  • @num8er I actually watched that video before posting this question.. – Manish Jun 04 '17 at 01:19
  • Your question is unclear. What does "objects are by reference" mean? This is not standard terminology, and if you use non-standard terminology, you should define it, otherwise it is hard to understand what you mean. The closest standard term is "pass-by-reference" (aka "call-by-reference"), but that doesn't apply here: a) there are no calls and nothing is being passed, and b) ECMAScript is pass-by-value, not pass-by-reference. – Jörg W Mittag Jun 04 '17 at 01:21
  • @JörgWMittag I tried to explain what I meant by an example.. does it count? or just the name? – Manish Jun 04 '17 at 01:27

1 Answers1

0

ThiS is what happens.

You create a variable emp which points to a location in the memory where is stored the object value you defined.

Then you you create a variable emp1 which points to the same location in memory of the object value.

Then you define a new variable emp2 which points to a location in the memory where is stored your object value you define.

Then you set that emp points to the nrw location in memory of the emp2 object value.

But emp1 still points to the first location/object.

I think you are getting confused between variable declarations and passing parameters in methods/functions (by reference or by value)

quirimmo
  • 9,800
  • 3
  • 30
  • 45
  • Not sure why people are giving negative rating... actually exact problem was with LinkList where I was trying to previous ptr.. i just came up with similar example..but easy to understand... :) – Manish Jun 04 '17 at 01:14
  • No idea, I didnt downvote it so you should ask them :) I think that it is because you are getting a bit confused between variable declarations and passing variables by value or by reference – quirimmo Jun 04 '17 at 01:27
  • I didn't vote it for closing but already 3 people did. Anyway, the most important thing is you understand what's going on with that code – quirimmo Jun 04 '17 at 01:29
  • @quirimmo I voted for close as not reproducible. Since even after hours the OP did not check that the actual output does not match with what they claim in the question. Hence - not reproducible. – zerkms Jun 04 '17 at 08:28