HelterSkelter HelterSkelter - 1 month ago 7
Javascript Question

calling a not-yet-existing and an already-gone property

The following code:



var person = {
name: "john doe",
printName: function () {console.log(persona.name); }
};
person.printName();
var persona = person;
persona.printName();





[EDIT: doesn't] works fine, and prints
john doe
twice
.

It will output once if we'll log
person.name
.

Why does the first printing doesn't work, and why it outputs twice when (in the second case)
person
doesn't exist anymore?

Answer

The first one will not work, here is the example of it:

var person = {
name: "john doe",
printName: function () {console.log(persona.name); }
};
person.printName();

Regarding the second case - the variable persona is only a reference to your person variable. You didn't delete it (so it still exists).

Check here:

var person = {
name: "john doe",
printName: function () {console.log(persona.name); }
};
var persona = person;
person.printName();
persona.printName();