1

What would the following code do?

I have used JS for years but haveno idea how this construct works?

(function() { /* No implementation yet */ })();

Knowing that there is no implementation - if there were - how would I invoke it? Would the following make an anonymous object?

var temp = (function() {  })();

Which I could use as:

temp.doWhateverDefined();
Aditya Singh
  • 9,512
  • 5
  • 32
  • 55
Alex.Barylski
  • 2,843
  • 4
  • 45
  • 68

6 Answers6

4

You have an immediately invoked function expression (IIFE). It is very common in JavaScript.

Evan Davis
  • 35,493
  • 6
  • 50
  • 57
  • Thats kinda cool it's like inline code that is executed but in a function scope - but without explicitly calling any function??? – Alex.Barylski May 10 '13 at 17:31
  • @Alex.Barylski it explicitly calls the anonymous function wrapping the code. In JavaScript, you invoke a function by adding parens, `()`. This defines a function, and then immediately calls it by appending `()` to the end. Worth noting, the final `()` can be inside OR outside the wrapping parens. `(function() {}())` is just as good as `(function() {})()` – Evan Davis May 10 '13 at 17:34
2
(function() { /* No implementation yet */ })();

This is called an Immediately Invoked Function Expression or shortly IIFE. It is declared, evaluated and called immediately.

The basic idea is:

var x = (function() { return 5;})();
alert(x); //5
flavian
  • 28,161
  • 11
  • 65
  • 105
1

It's being run immediately. It's as if you said:

var f = function() { /* No implementation yet */ }
(f)();

which is the same as

var f = function() { /* No implementation yet */ }
f();

The point of it is to allow a block of code (page initialization, etc.) to use whatever variable/function names it likes, without conflicting with other Javascript code that may use the same names. All the functions/etc. declared in that block are local, and don't harm the outside world.

Paul Roub
  • 36,322
  • 27
  • 84
  • 93
0

That is an anonymous function so you cannot invoke other than in the line where you create it unless you assign it to a name

var temp = (function() {  })();

This piece of code should be changed to

var temp = (function() {  });

That way you actually assign the function to the name. To invoke it just use a parentheses

temp()
aaronman
  • 18,343
  • 7
  • 63
  • 78
0

It is a self-executing function. You do not invoke it, it does it automatically.

var temp = (function() { })(); would work if the function returns something.

gpojd
  • 22,558
  • 8
  • 42
  • 71
0

What you have is a self-invoking function which is already called.

var temp = (function() {  })();

So the function has to returns a function so that temp() or temp.somethin(); could be triggered. See an example below for more information,

var temp = (function() { return {
    doWhateverDefined: function () {
        return 'invoked';}
      };  
    }
)();

alert(temp.doWhateverDefined()); //should alert invoked

DEMO: http://jsfiddle.net/5ch8F/1/

Selvakumar Arumugam
  • 79,297
  • 15
  • 120
  • 134
  • Thanks all for the replies...I kinda figured it was called immediately after being defined but I appreciate the IIFE blog thats awesome :) – Alex.Barylski May 10 '13 at 17:29