Javascript has a global object
. In the browser, this is referenced by the name window
.
window.foo
create a property in the global object.
var foo
create a global variable. This is very similar to a property of the global object, but you can't use delete
on it.
Simply declaring foo
without the var
keyword also create a property on the global object. It is a language design choice -- a lousy one -- but a design choice none-the-less.
var foo
and window.foo
both work equally well when you are at the global level. If you are inside of a function -- perhaps a module -- you must use window.foo
to make sure you reach the global object.