'How does jquery ready function allow it's first parameter to become jquery object?

Today I stumbled upon this:

jQuery(document).ready(function($$){
    console.log($$);
});

Apparently, $$ is considered as the jQuery object itself. Usually to do such a thing, we need to pass the jQuery variable:

(function($$){
    console.log($$);
}(jQuery))

But in case of ready function how does it know $$ is the jQuery object? I think jquery defines the ready function under the hood in a way that passes it's first parameter as jQuery.



Solution 1:[1]

This is described in the docs.

In this situation:

jQuery(document).ready(function($$){
    console.log($$);
});
  • When the jQuery script tag runs, window.jQuery has jQuery assigned to it
  • jQuery(document) references that global identifier
  • jQuery.ready`'s callback, as designed, calls its callback with the jQuery object

This functionality doesn't have much use unless you're using jQuery.noConflict, but for another example of something similar:

theLibrary().ready(function($$){
    console.log($$.libraryProp);
});
<script>
// library example
window.theLibrary = Object.assign(
  // the library is both a function
  () => ({
    ready: (callback) => {
      callback(window.theLibrary);
    }
  }),
  // and an object with properties
  {
    libraryProp: 'libraryProp'
  }
);
</script>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 CertainPerformance