1

The following code is present in jquery library. As they are quite different from the generally used functions, how is the flow of execution happening here:

/*!
 * jQuery JavaScript Library v1.12.3
 * http://jquery.com/
 *
 * Includes Sizzle.js
 * http://sizzlejs.com/
 *
 * Copyright jQuery Foundation and other contributors
 * Released under the MIT license
 * http://jquery.org/license
 *
 * Date: 2016-04-05T19:16Z
 */

(function( global, factory ) {

    if ( typeof module === "object" && typeof module.exports === "object" ) {
        // For CommonJS and CommonJS-like environments where a proper `window`
        // is present, execute the factory and get jQuery.
        // For environments that do not have a `window` with a `document`
        // (such as Node.js), expose a factory as module.exports.
        // This accentuates the need for the creation of a real `window`.
        // e.g. var jQuery = require("jquery")(window);
        // See ticket #14549 for more info.
        module.exports = global.document ?
            factory( global, true ) :
            function( w ) {
                if ( !w.document ) {
                    throw new Error( "jQuery requires a window with a document" );
                }
                return factory( w );
            };
    } else {
        factory( global );
    }

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

// Support: Firefox 18+
// Can't be in strict mode, several libs including ASP.NET trace
// the stack via arguments.caller.callee and Firefox dies if
// you try to trace through "use strict" call chains. (#13335)
//"use strict";
var deletedIds = [];

I mean the following portion

(function( global, factory ) {...})

with parenthesis at the start and end of the above line, how will this be taken. What are these kinds of coding styles called as and when are they used. Or what purpose do they serve.

ShyamSundar R
  • 455
  • 5
  • 16
  • 1
    it's an IIFE (Immediately Invoked Function Expression) – Jaromanda X Sep 08 '16 at 04:07
  • `var foo = function(){}; foo();` => `var foo = function(){}; (foo)()` => `(function() {})()`. – Felix Kling Sep 08 '16 at 04:08
  • so these kinds of code, will get executed when this file is being added to the page, while rendering the page, and when that line's bytes are reached?? – ShyamSundar R Sep 08 '16 at 04:12
  • If you're just asking what an IIFE is, this is a dup of about a hundred other questions. – jfriend00 Sep 08 '16 at 04:13
  • Many possible dups: [What is the purpose of wrapping whole Javascript files in anonymous functions like “(function(){ … })()”?](http://stackoverflow.com/questions/2421911/what-is-the-purpose-of-wrapping-whole-javascript-files-in-anonymous-functions-li/2421949#2421949) and [What does !function ($) { $(function(){ }) }(window.jQuery) do?](http://stackoverflow.com/questions/10896749/what-does-function-function-window-jquery-do/10896946#10896946) – jfriend00 Sep 08 '16 at 04:16
  • It's just defining and executing a function in a single expression / statement instead of two. – Felix Kling Sep 08 '16 at 05:05

0 Answers0