On Fri, Jan 7, 2011 at 1:31 PM, Peter van der Zee <[email protected]> wrote:
> 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.
>
>
Ah, yes, I forgot to mention this case; good addition. For this case
indirect eval may help:
"use strict";
(function foo() {
("global", eval)("this").foo = 42;
})();
alert(foo); // 42
http://dmitrysoshnikov.com/ecmascript/es5-chapter-2-strict-mode/#indirect-eval-call
Or in ES3 or non-strict ES5 -- just IIFE with returning `this`.
Dmitry.
--
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]