On Feb 18, 11:32 pm, "Dmitry A. Soshnikov"
<[email protected]> wrote:
> On 18.02.2011 6:00, nathanJsweet wrote:
>
> > So in the new ECMA standard the body decided that when the "this" word
> > is used at the highest hierarchical level (i.e. when it would normally
> > be the window object) would be undefined.
>
> It relates only to a function calls and their contexts. In the global
> context, as before, `this` value is the global object -- regardless the
> strict mode.
>
> alert(this); // global
>
> In function calls as is said, `this` is undefined (and moreover, it
> isn't coerced to an object -- i.e. if it's a primitive, it will be a
> primitive value).
>
> > In using the MooTools
> > library I have noticed that they invoke all of their IIFEs by tying
> > the variables to the "this" word instead of passing the window object
> > to the function and tying the variables into the window object that
> > way (as JQuery does).
>
> If to apply the IIFE in context of the global, then it's OK:
>
> "use strict";
>
> (function () {
> this.b = 10;
>
> }).apply(this);
>
> alert(b); // 10
If the intention is that - this - reference the global object, then it
is clearer (to me) to write:
(function(global) {
global.b = 10;
})(this);
which makes it clear what - this - should reference and doesn't use
apply. Or the function can be called with window, for the same reson.
--
Rob
--
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]