CodeBeatz
CodeBeatz

Reputation: 1981

Why my object reference is not null?

I can not explain the following behavior:

var persona = {nome:"Mario", indirizzo:{citta:"Venezia"}}
var riferimento = persona.indirizzo;
// riferimento ==> {citta: "Venezia"} <-- It is Ok

persona.indirizzo.citta = "Torino";
// riferimento ==> {citta: "Torino"} <-- It is Ok

persona.indirizzo = null;
// riferimento ==> {citta: "Torino"} <-- Why?

persona.indirizzo = undefined;
// riferimento ==> {citta: "Torino"} <-- Why?

I've tested this on C# and JavaScript and I have the same behavior.

Why my variable riferimento is not null or undefined?

Upvotes: 4

Views: 73

Answers (3)

user8894012
user8894012

Reputation: 11

In C# (not sure for javascript) the following is happening:

var persona = {nome:"Mario", indirizzo:{citta:"Venezia"}}

Creates a new object, however persona.indirizzo is not the object itself but just a reference to it (actual object somewhere in stack).

var riferimento = persona.indirizzo;

this creates a NEW reference to the object that persona.indirizzo was pointing to.

Because of the clearing of the reference in the persona, the newly created reference is not changed.

Upvotes: 0

TimoStaudinger
TimoStaudinger

Reputation: 42490

Both persona.indirizzo and riferimento hold a reference to the same object {citta: "Venezia"}.

If you use the reference to change a property of the object, this change will be visible through both variables:

persona.indirizzo.citta = "Torino";

However, if you delete one of the references, you do not impact the object itself at all, only one of the references to the object. The second reference still points to the same object:

persona.indirizzo = null;

If you delete both references, the object will no longer be accessible and will eventually be garbage collected.

Upvotes: 0

David
David

Reputation: 219037

Because that's what a reference is. It's not the actual object, it's simply a kind of "pointer" to the location in memory where the object lives. Consider these two statements:

  • You can have as many references to the same object as you want.
  • When you set the value of a reference, you're only changing the reference and not the object itself.

So when you do this:

persona.indirizzo = null;

You're not modifying the object to which indirizzo pointed. You're just pointing indirizzo to something else. In this case, null. The object still remains unchanged in memory. And riferimento still points to that object. (Side note: If nothing pointed to that object anymore then it would be out of scope and the system would "clean up" that memory as needed. Different languages/environments/etc. handle that in different ways.)

It's subtle, but the difference is seen in these two statements:

persona.indirizzo.citta = "Torino";
persona.indirizzo = null;

The first line follows the reference and sets the value of something in the object to a new value. The second line changes the reference itself to no longer point to the object at all.

Upvotes: 1

Related Questions