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)
{
//...
});