Considering the following scenario:
We need to use some var or function schrodinger
but do not know if it is available.
So we try:
if(schrodinger){ ... }
=> ReferenceError: schrodinger is not defined
We could use..
if( typeof(schrodinger) !== "undefined"){ ... }
=> No error. Stuff does not run
BUT, we could also use..
if( window.schrodinger){ ... }
.. as (apparently) properties become undefined
instead of null
or something?
This last point I do not understand ("Why does window.schrodinger
not throw an error?")
Is there any preference of using typeof(shrodinger)..
over window.schrodinger
?
"JS dynamically initialises properties" – that's what I thought although I do not yet quite understand it.
Anyway, most people use
typeof
which seems the way to go. It's just that I'm trying to understand howwindow.foo
works..B.t.w.:
if(window.schrodinger) alert('hello')
does not work when[window.]schrodinger
== FALSE or 0. So it still seems valid to usewindow.var
.I'm probably thick but I still do not understand why
window.var
does not throw a reference error... :)