NeilDCruz NeilDCruz - 1 year ago 73
jQuery Question

Why would you wrap a function expression in paranthesis in javascript (Not IIFE)?

I was trying to acquaint myself with some advanced javascript and have been reading some of the jquery code on github when, I came across a anonymous function wrapped in parenthesis here.

Now, I am already aware of the concept of IIFE and the plethora of questions on SO regarding the subject, for instance - IIFE question here. But, this is not an IIFE, at least, is not invoked immediately IMHO.

I'm trying to figure out the use of such code and how it fits into the larger scheme of things. Since, the function is anonymous, I'm wondering how it is called or referenced.

Answer Source

It is an IIFE -- it just gets called with a very long argument.

( function( global, factory ) {

    "use strict";

    // .....

// Pass this if window is not defined yet
} )( typeof window !== "undefined" ? window : this, function( window, noGlobal ) {
     // ...
    return jQuery;
} );

A function is passed as an argument, which gets used in the upper function. But it clearly is a standard IIFE otherwise.