3

What types of variables change like so?

var int1;
var int2;
var int3;

int1 = 42;
int2 = int3 = int1;
int1 += 3;

int1      // 45
int2      // 42
int3      // 42



arr1 = {};
arr2 = arr3 = arr1;
arr1.ab = 5; 

arr1      // Object {ab: 5}
arr2      // Object {ab: 5}
arr3      // Object {ab: 5}

Like howcome int2 and int3 changed and arr2 and arr3 didn't? What types change like this?

Yharoomanner
  • 145
  • 1
  • 1
  • 5

4 Answers4

3

In javascript Arrays & Objects are passed by reference, so changing them one place affects others.

And primitives like number, string are passed by value, so changing them at one place doesn't affect on others.

Primitives

var a,b;
a=10;
b=a;

So b & a has something like bellow structure

a ---1000----> [10] a is pointing to some location(lets 1000) which have value 10
b ---1004----> [10] b is pointing to some location(lets 1004) which have value 10

Lets we increment a by 1 now the value will be changed at the place 1000.

a ---1000----> [11]
b ---1004----> [10]

And in Arrays and Objects

obj1 = {}; // obj1 has a reference

obj1 has an structure like bellow

------->1000--------->[{}]
obj1 -------1004----->[1000] //this '1000' is a reference which has a `{}` at it's place 

This line

obj2 = obj1;

after this line obj2 & obj share same reference ------->1000--------->[{}] obj1 -------1004----->[1000] obj2 -------1008----->[1000]

obj1.ab = 5; 

this line is adding a field called ab to the reference of obj1

------->1000--------->[{ab:5}]
obj1 -------1004----->[1000]
obj2 -------1008----->[1000]

And because obj1 & obj2 have same reference, you are getting the field ab for both.

obj1      // Object {ab: 5}
obj2      // Object {ab: 5}

Note:- Any improvement in answer is appreciated.

Mritunjay
  • 25,338
  • 7
  • 55
  • 68
  • 3
    That is not correct. Everything is passed by value. In case of objects, that value *is* a reference. Pass by reference is something else though. The biggest difference between objects and primitives is that objects are mutable. – Felix Kling Oct 04 '14 at 06:35
  • 1
    @JhKaiz: This breaks if you mutate `obj1`. Simulating immutability through prototype inheritance only works one way and is more confusing than useful IMO. And again, everything is *pass by value* in JS. Wikipedia has a huge article explaining the different ways of passing values. – Felix Kling Oct 04 '14 at 07:34
  • Thanks @FelixKling, I got the point. Removed my wrong solution. So,the only to pass by value of an Object that value != reference is make it be a function that return a new object, isn't it? – Kai Oct 04 '14 at 23:48
1

Primitive data types (number, string and boolean) do not change if you change a reference of it, whereas composite data types do change.

http://msdn.microsoft.com/en-us/library/ie/7wkd9z69(v=vs.94).aspx

Farce
  • 38
  • 4
  • 1
    This answer is a bit superficial. It doesn't explain what *reference* means in this context. If you are just saying that primitives are immutable and objects are not, that's correct. – Felix Kling Oct 04 '14 at 06:48
0

That is because the arrays share the reference between them while the integers share the value. When a reference is changed, it affects others who have the same reference, but values are not like that since the js compiler allocates separate memory for them and modifying them affects only them..

To visualise reference types,

Reference --------> { ab : 5 } // Changing affects others with same reference
              ^
              | --> arr1
              | --> arr2
              | --> arr3

To visualise value types,

int1 ---> 42 --> +3 ---> 45 // Doesn't affect others
int2 ---> 42 
int3 ---> 42
Amit Joki
  • 58,320
  • 7
  • 77
  • 95
0

arr1 contain a reference for the empty list. Then arr3 receives a reference for the same list, and arr2 a reference for the same list. So when you make arr1.ab = 5, you added a item in that list named 'ab' and gave the value 5. But since arr2 and arr3 point to the same list, you will get the same value.

eri0o
  • 2,285
  • 4
  • 27
  • 43