2

I have been looking and looking and looking everywhere and I have no idea what this piece of code is called. I pulled it from jQuery

( function( global, factory ) {

"use strict";

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 ) {...})

Can anyone help me out?

RPichioli
  • 3,245
  • 2
  • 25
  • 29
C. Morgan
  • 99
  • 1
  • 10
  • 2
    This is a wrapper for supporting different ways of loading the script. It allows it to be used both with CommonJS-style module systems (such as in NodeJS), which have to use `exports` and `require()`, as well as in browsers by defining a global. The [UMDjs](https://github.com/umdjs/umd) project offers templates for various combinations. – Jonathan Lonowski Sep 03 '16 at 19:36
  • Let's keep comments civil and constructive please. – Jon Clements Sep 03 '16 at 20:37
  • Hmm. And also idk if this is me but every comment is deleted – C. Morgan Sep 03 '16 at 20:37
  • Well, I am kind of pro https://github.com/XtremePlayzCODE – C. Morgan Sep 03 '16 at 20:40
  • Weren't exactly useful comments in clarifying the question... anyway - moving forward... – Jon Clements Sep 03 '16 at 20:41

2 Answers2

2

Could be known as a wrapped/ self invoked function. But really its no different to a function. Just a humble function that gets called on definition - without a name. Hence why, at the end of the declaration, there is a list of arguments being passed straight through.

Caspar Wylie
  • 2,818
  • 3
  • 18
  • 32
  • That I can see. I want to know the name to make my own – C. Morgan Sep 03 '16 at 19:41
  • They do not really have a specific time of name - they are, just functions. – Caspar Wylie Sep 03 '16 at 19:46
  • 1
    @C.Morgan The root of the snippet is an [IIFE](https://stackoverflow.com/questions/8228281/what-is-the-function-construct-in-javascript), or immediately-invoked function expression. As for the use of `factory` and conditionals within that, ["Universal Module Definitions"](https://github.com/umdjs/umd) is at least one name given to the pattern. – Jonathan Lonowski Sep 03 '16 at 19:46
0

If you're asking about the syntax of the code which might look weird to you, this is what called a 'self invoked function'.

Which is a function that is being invoked right away, not declaring its's name. just invoking it.

Kesem David
  • 2,135
  • 3
  • 27
  • 46