The first example tries to assign a property to a global object named my_var by reading a value from an identifier named my_var (OR an empty object). However, the identifier my_var not defined at this point, so it fails.
In the second example, because of how the javascript variable works, the variable my_var already declared when you read it, assigning it to it.
Also look at this example:
a = a;
It will work with the var keyword!
b = b; // succeeds allthough identifier undeclared?! var b = 0;
This is due to the fact that a variable lifting device will turn it into this:
var b;
Daniel Baulig
source share