2

I'm working with a script and have found the following, which I really can't find any info of what it means

(function($) {
    $(document).ready(function(e) {


       ... bla bla bla ...

    });
}) (jQuery);

Is (function($){}) (jQuery); the same as $(function () {}); ? and if so, why would somebody define twice document.ready ?

Alex
  • 7,538
  • 23
  • 84
  • 152
  • 1
    probably trying to work around some OTHER js library redefining `$` away from jquery, by passing the entire jquery object in as a parameter to this "ready" function, so it's guaranteed to be jquery when the internal ready function actually fires. – Marc B Jan 16 '13 at 15:42
  • could be so @MarcB as many other libraries are being loaded. thanks – Alex Jan 16 '13 at 15:43

6 Answers6

3

No, it's not the same. It's an anonymous function which is being passed the jQuery object, to insure that it is available as the local variable $ within the scope of the function, even if the global variable $ is overwritten by another library. It is completely different than $(function () { }) and $(document).ready(function () { }).

This particular pattern is recommended in the jQuery plugin authoring documentation:

[When authoring a plugin] it's a best practice to pass jQuery to an IIFE (Immediately Invoked Function Expression) that maps it to the dollar sign so it can't be overwritten by another library in the scope of its execution.

(function( $ ) {
  $.fn.myPlugin = function() {
 
   // Do your awesome plugin stuff here

 };
})( jQuery );
Community
  • 1
  • 1
user229044
  • 232,980
  • 40
  • 330
  • 338
2

Is (function($){}) (jQuery); the same as $(function () {}); ?

No. The first is immediate invocation of an anonymous function, used primarily for preventing pollution of the global scope. In this case, it's also used to make sure that $ is a reference to jQuery, without worrying about overwriting $ elsewhere.

The second is the shorthand for binding a document ready handler with jQuery.

More reading:

Community
  • 1
  • 1
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
2

No it isn't, (function($){}) (jQuery); is an IIFE(Immediately invoked function expression) passing jQuery as a parameter and using $ to represent it in the function scope so that no conflicts will occur if another library that uses $ is loaded without using jQuery.noConflict.

Musa
  • 96,336
  • 17
  • 118
  • 137
2

Nope

(function(){})();

is executed as soon as the browser encounters that script. .ready() is an event that is triggered after the entire document is parsed

Adrian
  • 190
  • 6
1

No it's not. It is a closure with a document ready handler inside it. It is used to ensure that the $ within the enclosure is reserved for jQuery and does not interfere with any other library.

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
0

A nice clear explanation here;

http://jquery-howto.blogspot.com/2008/12/what-heck-is-function-jquery.html

Oliver Gray
  • 874
  • 6
  • 17
  • 1
    Whilst this may theoretically answer the question, [it would be preferable](http://meta.stackexchange.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – Matt Ball Jan 16 '13 at 15:45
  • Link-only answers are strongly discouraged. Your answer needs to include all the information required to answer the question, which may then be * supplemented* with links. – user229044 Jan 16 '13 at 16:00