> Plugins are supposed to use this: > > (function($) { > // Plugin code > > }) (jQuery) > > I know what it does (allows the use of $ within the script), but how > does it actually work? Is it somehow casting the function object as > the jQuery object? It always seemed odd to me, and I haven't seen this > idiom elsewhere.
Consider this bit of code: // declare a function that accepts an argument var myFn = function(myParam) { // inside here I can use myParam freely }; // call the function and pass an argument myFn(3); Now, change what you call the parameter name and what you pass to the function var myFn = function($) { // inside here I can use $ freely }; // call the function and pass jQuery as the argument myFn(jQuery); Now do it all in one step, as an anonymous function: (function($) { // inside here I can use myParam freely })(jQuery);