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,431
  • 5
  • 31
  • 54
Alex.Barylski
  • 2,601
  • 4
  • 38
  • 61

6 Answers6

4

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

Mathletics
  • 33,673
  • 6
  • 49
  • 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() {})()` – Mathletics 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
  • 27,081
  • 11
  • 59
  • 100
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
  • 35,848
  • 27
  • 79
  • 88
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
  • 17,836
  • 6
  • 58
  • 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
  • 21,634
  • 8
  • 41
  • 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
  • 78,145
  • 14
  • 119
  • 133
  • 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