In JS, doing a read for an undeclared variable gives a reference exception.
I tried the following code:
var obj = { };
console.log(obj.v1);
Prints undefined
console.log(v2);
So far this has thrown an exception.
What is the reason for the different behavior? I was expecting exceptions in both cases since v1 and v2 are not declared.
EDIT: More confusing is the fact that if v2 was declared in the global scope, it would become a property of the window object. Doesn't this look like the case when I refer to an undeclared property of a window object? Same as case 1?
source
share