Mathias, the thing is, in the first case you're assuming the code might be
ran inside another scope. If that is the case, what tells you that the
"window" variable has not been redeclared inside that scope (second case)?
And what tells you that your var name hasn't already been declared inside
that scope (third case)?

In case window has been redeclared, the effect is a new property on that
object (or nothing/error when it's not even an object). Subsequent usages of
the identifier, without "window" prefix, could cause errors.

Then the third case. If the var was already declared, the var statement is
ignored and the already declared variable is used instead. If it wasn't
declared, the var will be created in global scope. So you see that the
behaviour is different.

So if you fear your script will be inlined inside another scope, there's
little you can do to be absolutely safe. I think `window.x` (or `global.x`
if the environment supports it) would be the best way to go, if you want to
make it obvious that you're explicitly defining a global variable.

Any environment that inlines a script like this is threading on thin ice
anyways...

- peter

-- 
To view archived discussions from the original JSMentors Mailman list: 
http://www.mail-archive.com/[email protected]/

To search via a non-Google archive, visit here: 
http://www.mail-archive.com/[email protected]/

To unsubscribe from this group, send email to
[email protected]

Reply via email to