2

I am a javascript newbie and recently came into the following code.

(function($){

    if(!document.defaultView || !document.defaultView.getComputedStyle){
        var oldCurCSS = jQuery.curCSS;
        jQuery.curCSS = function(elem, name, force){
            if(name !== 'backgroundPosition' || !elem.currentStyle || elem.currentStyle[ name ]){
                return oldCurCSS.apply(this, arguments);
            }
            var style = elem.style;
            if ( !force && style && style[ name ] ){
                return style[ name ];
            }
            return oldCurCSS(elem, 'backgroundPositionX', force) +' '+ oldCurCSS(elem, 'backgroundPositionY', force);
        };
    }
})(jQuery);

What is function($) {...} (jQuery)?

PS: I also don't quite get what the code is for... If possible please give a hint on it.

tckmn
  • 57,719
  • 27
  • 114
  • 156
CDT
  • 10,165
  • 18
  • 66
  • 97

6 Answers6

3

It's a self-executing anonoymous function called with jQuery as argument, that gets renamed to $ inside the function signature.

Since you don't know if jQuery.noConflict() has been used in the page (especially if you are writing distributable code like Plugins), this way you can use the shorthand $ inside your function safely.

Who wrote it is actually stupid beacuse he's using jQuery inside the function anyway : )

It also prevents the variables from polluting the global namespace and encapsulates them making them unaccessible from outside the function.

David Fregoli
  • 3,377
  • 1
  • 19
  • 40
2

That's to prevent the code from interfering with a global variable named $, so that you can use other libraries that use the $ symbol too.

Inside the function, $ will only refer to the jQuery object.

Refer http://docs.jquery.com/Plugins/Authoring

Osiris
  • 4,195
  • 2
  • 22
  • 52
1

It is to make sure $ means the same thing as jQuery. Other libraries may change what $ means, so this is needed.

function($) is called with jQuery as an argument, therefore $ gets set to jQuery within the function.

tckmn
  • 57,719
  • 27
  • 114
  • 156
1

That is a self executing function, think of it like document.ready function in jQuery, except that this function will fire once its loaded.

(function(){}) will declare the function, but by adding () this can be fired immediately. You can pass parameter s to this function as well. $ is actually an alias of jQuery, so to make sure that you are using jQuery, this is passed as a parameter and aliased as $ which is the convention.

(function(myAwesomePlugin){})(jQuery);

This is also valid. You can use it like myAwesomePlugin("#id").click() ...

GoodSp33d
  • 6,252
  • 4
  • 35
  • 67
1

basically it’s an anonymous function that lets jQuery play nicely with other javascript libraries that might have $ variable/function. It is executed as soon as the DOM is parsed and is invoked in order of appearance if there are multiple appearances. At this point the document is however not displayed, its just parsed.

1

Apart from what has been said already, this self-executing function encapsulates all variables and other functions, so the main (window) namespace remains unpolluted as all variables and functions are 'local'.

strah
  • 6,702
  • 4
  • 33
  • 45