I came across this pattern for JavaScript initialization:
foo = foo || {};
alert('no errors!');
But it seems that it does not work unless foo was defined before.
There are two variations of the same pattern that seem to work correctly:
var foo = foo || {};
alert('no errors!');
and
foo = window.foo || {};
alert('no errors!');
From my point of view, I don't understand why the first one does not work, but the two other do.
Why is that? Could you please explain in detail?