8

IIFE which need to access non-overridden window object - can be seen as:

something like (jQuery example):

$(function (global) {
  // do something with global
})( window );

But sometimes I also see this (underscore.js):

(function() {
  var global= this;
  // do something with global
}).call(this);

Question 1: is there any difference? If so, when should I use each?

Question 2: this inside IIFE is window. Why is it necessary to "send" window/call(this)? (jQuery isn't using strict mode imho)

NB

It looks like jQuery (since 1.11.0) has also adopted this pattern:

(function (global, factory)
{
   //....

}(typeof window !== "undefined" ? window : this, function (window, noGlobal)
{
   //...
});
Alan
  • 553
  • 1
  • 4
  • 15
Royi Namir
  • 144,742
  • 138
  • 468
  • 792
  • 1
    There might be no `window` variable in some JS environments… while [`this` works even in strict mode](http://stackoverflow.com/q/9642491/1048572) – Bergi Mar 11 '14 at 13:39
  • @Bergi so they could do : `$(function (global){ // do something with global })( this );`....no ? – Royi Namir Mar 11 '14 at 13:43
  • @Bergi Can you please help me find in [The DOCS](http://es5.github.io/) where does it say that context inside IIFE is Global object ? ( I can't find it) – Royi Namir Jul 19 '14 at 07:30
  • Not necessarily inside the IIFE (where it is [determined](http://es5.github.io/#x10.4.3) by the ordinary call logic), but the [global `this`](http://es5.github.io/x10.html#x10.4.1) (which is passed to `.call()` as an argument) is *always* the global object. – Bergi Jul 19 '14 at 07:41
  • @Bergi are you telling me that when I do `f()` it actually runs `f.call` ? – Royi Namir Jul 19 '14 at 07:57
  • No - that's from the underscore snippet you posted. `f()` [runs](http://es5.github.io/#x11.2.3) [\[\[call\]\]](http://es5.github.io/#x13.2.1), `f.call()` calls [call](http://es5.github.io/#x15.3.4.4). – Bergi Jul 19 '14 at 08:09

1 Answers1

4
(function() {
  var win = this;
  // do something with win
  }).call(this);

Underscore is a javascript library not a DOM library,therefore it should not use window anywhere,since javascript is not the DOM and window is a DOM api

Underscore isnt tied to the DOM with this approach. Trying to call window in nodejs or rhino is not going to work and doesnt make any sense.

EDIT :

call sets the context of the function to this(global or window) , so no need to pass anything as an argument.

mpm
  • 20,148
  • 7
  • 50
  • 55
  • So , why it still uses `this` ? – Royi Namir Mar 09 '14 at 13:05
  • well because this is valid javascript to point to a scope.if you type this in the console it will return window. – mpm Mar 09 '14 at 13:06
  • I was asking about the differences between those 2 :-). for example - why did underscore didnt do :`(function (root){ // ... })( this);` instead ? ( in order to set context) – Royi Namir Mar 09 '14 at 13:10
  • $ is a shortcut to jquery, why do you want underscore to use that? – mpm Mar 09 '14 at 13:11
  • I know that call sets the context also apply. but inside IIFE - `this` already referes to the global object. ALSO - it there arny reason for choosing apply/call VS sending `this` ? (this is actually my 2 simple questions) – Royi Namir Mar 09 '14 at 13:15
  • No,this doesnt referes to the global object automatically. the "this" context needs to be set.That's the why of the call. As for the reason they are using one over the other,because you dont need to pass anything to the IIFE to set the context. – mpm Mar 09 '14 at 13:19
  • `this` in IIFE is always referes to the global object. look at this node sample : http://i.stack.imgur.com/fUZ65.jpg. in browser it's window. so the context in IIFE IS(!) the global object. – Royi Namir Mar 09 '14 at 13:24
  • @RoyiNamir: http://stackoverflow.com/questions/9822561/why-is-this-in-an-anonymous-function-undefined-when-using-strict – DCoder Mar 11 '14 at 11:04
  • @DCoder I know this already. I wrote it in the question : _( jQuery isn't using strict mode imho)_ and still in ver 1.11 - they use the same pattern (when window==undefined though) – Royi Namir Mar 11 '14 at 11:06