Navin Rauniyar
Navin Rauniyar

Reputation: 10535

How jQuery is available outside the closure scope?

Sorry if this type of question asked many times before but I'm little unclear with the jQuery coding here:

From http://code.jquery.com/jquery-latest.js

(function(global,factory){
  //some checks and coding
})(typeof window !== "undefined" ? window : this, function( window, noGlobal ) {
  //coding stuff
})

So, the jQuery is using the coding in that closure but how the jQuery is available outside that scope?

Upvotes: 0

Views: 77

Answers (2)

Bergi
Bergi

Reputation: 665040

In the very end of this IEFE closure you can see the lines

// Expose jQuery and $ identifiers, even in
// AMD (#7102#comment:10, https://github.com/jquery/jquery/pull/557)
// and CommonJS for browser emulators (#13566)
if ( typeof noGlobal === strundefined ) {
    window.jQuery = window.$ = jQuery;
}

where they export the $ and jQuery identifiers to the global window object. This function with the // coding stuff itself is the second argument of the IEFE that you posted, and will - as the factory be called appropriately depending on what module loaders are available (or not).

Upvotes: 1

dfsq
dfsq

Reputation: 193291

Inside of the closure you can find this line of code:

return (window.jQuery = window.$ = jQuery);

So jQuery constructor function is explicetly assigned to window.jQuery. That's why it's available outside.

Upvotes: 1

Related Questions