Why on earth does this work:
FOO = window.FOO || {isFoo: true};
But this doesn't:
FOO = FOO || {isFoo: true};
Since FOO and window.FOO both reference the same thing (both are running in the global scope).
Why on earth does this work:
FOO = window.FOO || {isFoo: true};
But this doesn't:
FOO = FOO || {isFoo: true};
Since FOO and window.FOO both reference the same thing (both are running in the global scope).
Because FOO
is not declared, but window
is. Trying to access an undeclared variable will throw a ReferenceError
, but accessing an undefined property will not.
You can get around it by using typeof
:
FOO = typeof FOO != 'undefined' ? FOO : {isFoo: true};