3

I was reading a book about object oriented javascript and I found this:

Reference types do not store the object directly into the variable to which it is assigned, so the object variable in this example doesn’t actually contain the object instance. Instead, it holds a pointer (or reference) to the location in memory where the object exists. This is the primary difference between objects and primitive values, as the primitive is stored directly in the variable.

My question is what is the meaning of this ?

"Reference types do not store the object directly into the variable to which it is assigned, so the object variable in this example doesn’t actually contain the object instance." ??

enter image description here

mplungjan
  • 169,008
  • 28
  • 173
  • 236
Aniruddha Chakraborty
  • 1,849
  • 1
  • 20
  • 32

6 Answers6

2

It could be clearer with an example:

var obj1 = { name: "John" };
var obj2 = obj1;

obj1 and obj2 point to the same location in memory. It can be proved by changing name property:

obj2.name = "Bob";
console.log(obj1.name);  // "Bob"

Another behaviour with primitives:

var string1 = 'string';
var string2 = string1;

string1 and string2 point to different locations in memory. So changing string2 won't affect string1

string2 = 'new string';
console.log(string1);  // 'string'
Anton Gusar
  • 513
  • 1
  • 3
  • 14
  • 1
    "string1 and string2 point to different locations in memory." - how do you know? – georg Jan 27 '16 at 13:31
  • 1
    well, "obj2 = new object()" won't affect "obj1" as well. – georg Jan 27 '16 at 13:36
  • Because they are not related. But if we assign your obj2 created with 'new Object()' to obj1 they'll start pointing to the same location in memory. – Anton Gusar Jan 27 '16 at 13:39
  • 1
    Your example doesn't prove that "string1 and string2 point to different locations in memory". Replace strings with objects and the result will be exactly the same. – georg Jan 27 '16 at 13:43
  • @georg, could you please provide an example that will prove or refute a statement that string1 and string2 point to different locations in memory? – Anton Gusar Jan 27 '16 at 13:56
  • 1
    The point is, there's no "reference vs primitive" dichotomy in js, all types are references. – georg Jan 27 '16 at 14:12
2

In the image you provided you can see

var object1 = new Object();
var object2 = object1;

In this case, you have two variables that both store a reference (think of a pointer) to another place in your memory. In this place the object is stored. If you change your object via one of the references, and access it via the other one you will see it has changed.

object1.someVariable = 'asdf';
object2.someVariable = 'newValue';
console.log(object1.someVariable); // will give 'newValue'
console.log(object2.someVariable); // will also give 'newValue'

If you have scalar values, they will not store references, they will store the value itself.

Think of another example:

var primitiveString = 'asdf';
var anotherPrimitiveString = primitiveString;

Since both store the value it self, you can change one of the two strings, but the other one will still contain asdf, since they do not reference something.

anotherPrimitiveString = 'newValue';
console.log(primitiveString); // will give 'asdf'
console.log(anotherPrimitiveString); // will give 'newValue'

Here you have a jsfiddle with the explained example.

bpoiss
  • 13,673
  • 3
  • 35
  • 49
1

You have a house. You = variable. House = value. Now, you have to prove you own that house. You get a paper, stating that you are the owner.

When you go around, you don't have to carry your house. You can just show people the paper.

House = heavy, hard to move. Paper = small, light, easy to move.

That paper does what storing a reference does. It does not actually hold the real object, but it tells the rest of the system where it is.

Mjh
  • 2,904
  • 1
  • 17
  • 16
1

In computer science in general there is two type of variables types, pointers and values. Pointers don't hold anything but an address so the computer can find where the real value is stored, while values store the real data. The power of pointers is you can define one value and have multiple pointers use that same value. This is a huge gain in memory management and bidirectional communication from one section of code and another. This is an example of a pointer in javascript.

var foo = { value : 1};
var goo = foo;
goo.value = 4;
//now both goo.value and foo.value are both 4.

A value is something like this.

var foo = 2;
var goo = foo;
goo = 4;
//now foo is 2 and goo is 4.
Michael Warner
  • 3,879
  • 3
  • 21
  • 45
1

Object1 refers to some memory location (for example 2002). Object2 is also refered to the same memory location but by referring Object1.

rb4bhushan
  • 115
  • 2
  • 11
1

In js it is best to think that variables are pointers to objects & when assign directly to a variable you are not modifying any object , but pointing your variable to an object.

Let us take this example

var a= b ={}

So here a & b are pointer to same object.

Now set a.someProp = 'value'

it sets b.someProp as well since a & b point to same object

Where as storing a value in a variable is called variable initialization

brk
  • 48,835
  • 10
  • 56
  • 78