I am a bit confused by the JavaScript window object. To get started, consider the following two declarations:
var imglobal = "I'm Global"; window.imglobal = "I'm Global";
As I understand it, that would be exactly the same (right?). In both cases, it may be available as "imglobal" or "window.imglobal". I do not understand why var declares local variables, the following does not work:
function imafunc() { var imavar = "I'm a variable"; window.alert(imafunc.imavar); }
So why the following?
var imavar = "I'm a variable"; window.alert(window.imavar);
I came across this while using GWT; it seems that you always need to explicitly refer to the window object ($ wnd there) there, probably because it is not a "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?
source share