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
My question is: should I be worried?
In non-strict mode -- nope. In strict -- yes, if you expect those
attached to `this` properties will be global props.
I'm tempted
to go through MooTools when I'm finished building with it for a
particular website and replace the cases where the "this" word is used
for window with an invocation variable that is being passed the window
object, like so: (function(w){ var someVar = w.someVar = function()
{dosomething}})(window);. What do you all thing?
As is shown above, just `apply(this)` is enough.
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]