Q:

JavaScript Function Reference

Consider this code, which sets a to a function. Then f points to a, but then a gets set to null.
var a = function () { 
    return 5;
};

var f = a; 

a = null;

However, when calling f(), 5 prints out.
console.log(f());

5

Why doesn't calling f() result in a null object reference?
http://jsfiddle.net/deDca/

A:

Here's step by step what is happening:
var a = function () { 
    return 5;
};

Assigns a function reference to the variable a.

var f = a; 

Assigns the same function reference to the variable f.  At this point, both a and f contain a reference to the same function.

a = null;

Clears a so it no longer contains that function reference.  But, f still contains the function reference and has nothing to do with a so you can change the value of a to your heart's content and it will not affect what f has.  After the second step above, each variable had it's own unique reference to the function.  a and f did not point at each other.
It's like if you have two friends and you tell each of them a secret password.  Now, you tell the first friend that the password is now different.  The second friend still has the original password - they are not affected by the fact that you told the first friend something different.

