0

We have that basic jQuery script that use wrapper code, It initialized with the transfered global window parameter. Is it necessary transfering this parameter? window is a global parameter and you can use it from inside function if you transfer it or not.

What is the reason for that?

(function (window, undefined) {
    var jQuery = (function () {

        //Define a local copy of jQuery
        var jQuery = function (selector, context) {
            // The jQuery object is actually just the init constructor 'enhanced'
            return new jQuery.fn.init(selector, context, rootjQuery);
        },  
    //some code
    //...
    //...
    //...

    window.jQuery = window.$ = jQuery;
})(window);
UiUx
  • 967
  • 2
  • 14
  • 25
oleg
  • 131
  • 2
  • 9
  • 1
    Basically, what is said in the (very frequent) duplicates is that having a global variable enables the minification of its name. – Denys Séguret Feb 14 '13 at 14:17

1 Answers1

4

Smaller file size upon minification.

If you use window everywhere, the minifier will leave every reference as window. If you pass it through a closure, the minifier will convert it to something like a, which will save on the number of bytes if window was used more than once.

zzzzBov
  • 174,988
  • 54
  • 320
  • 367
  • 1
    shouldn't the minification just remove the window completely ? I mean if you have `window.somevar = 2` and the minification does `a.somevar=2` (if a becomes a ref to window), doing `somevar=2` without a var is the same thing and smaller – TheBrain Feb 14 '13 at 14:20
  • @TheBrain, No,If the script is evaluated in strict mode it could complain that those global references are undefined if they happen to be undefined. – zzzzBov Feb 14 '13 at 14:42