I've wanted to find a better version of 'var dwr = {};' for a while,
mostly because every JS checker complains. I'm not sure why.
I
think they expect a statement and not a declaration in the one-liner. Same
problem we had with Safari and "if ( expr ) function()...".
As
we want "dwr" in the global scope it would do fine without a |var| declaration
though.
It's worse that when used with an 'if' we might like to use a block, but
{ var dwr = {}; } feels like it shouldn't affect the surroundings. (though I
appreciate that it does).
Well, |var|-declared variables in JavaScript register themselves in the
nearest scope and new scopes are created for functions, not for individual
{} blocks.
So doing window['dwr'] felt like a decent fix, however, and this is the
strange bit, on IE:
window['dwr'] = { };
dwr.wibble = 'x';
if
(dwr == null) var dwr = {};
Ends up re-defining dwr because (dwr
== null) is true. I have no idea why, but IE was broken because not all
declarations of DWR did it in the same way.
I've
tried but can't trigger this bug. Anything else I need to
do?
In the end I copied what Dojo does.
Ok,
they have a lot of handling for other environments than the browser. In the
browser this===window. For that reason I think the current |this| code would
work just the same with |window|.
If
the aim is to always register our variables on the global object I think it is
clearer to use |window|.
Best
regards
Mike