0

In the following code, what does the last line do (the bit within the parenthesis)?

  • Is it related to Javascript closures?
  • Is it returning a variable?
  • Is there a way to write this code without using this short-hand?

Thanks!!

(function (window, $, my, undefined) {
    'use strict';

    if (!$) { throw 'jQuery not found.'; }

    my.someNamespace = {

        bla:null,
        blabla:null,

        init: function(){},
        bind: function(){}

    };
} (window, $, window.my = window.my || {}));
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
Sterling Bourne
  • 3,064
  • 23
  • 22

1 Answers1

5
var x = 5;
(function(y){
   console.log(y); // will print 5
})(x);

It’s an Immediately-Invoked Function Expression, or IIFE for short. It executes immediately after it’s created. x is the passed parameter.

void
  • 36,090
  • 8
  • 62
  • 107