Hi,
I'm a bit confused by JavaScript's window object. For starters, consider the following two declarations:
var imglobal = "I'm Global";
window.imglobal = "I'm Global";
As far as I understand it, this would be exactly the same (is it?) It can be accessed as "imglobal" or "window.imglobal" in both cases. I don't understand why, var declares local variables, the following doesn't work:
function imafunc() {
var imavar = "I'm a variable";
window.alert(imafunc.imavar);
}
So why does the following?
var imavar = "I'm a variable";
window.alert(window.imavar);
I stumbled across this when using GWT; it appears one always has to refer to the window object ($wnd there) explicitly there, probably because it's not the "real" window object but some kind of sandbox.
It gets even more confusing with functions, I know three ways to declare them:
var myfunc = function() { window.alert("Hello, World!"); }
window.myfunc = function() { window.alert("Hello, World!"); }
function myfunc() { window.alert("Hello, World!"); }
Is there any technical difference between these three approaches?