7

I didn't find a post asking for a pattern similar to this one, sorry if I missed it.

Anyway, I'm repeateadly seeing this pattern in many jQuery plugins or scripts:

(function () {
  ...........
}());

What's its purpose? Does it have a name?

Thanks!

Tom
  • 1,217
  • 3
  • 12
  • 15
  • 1
    It's a function that gets executed immediatly – Joren Oct 04 '13 at 13:35
  • 1
    **I** mediately **I** nvoked **F** unction **E** xpression, _IIFE_. Here, `function` is being used as an _operator_, not a _statement_ and then the `()` let it be invoked as soon as it's interpreted. – Paul S. Oct 04 '13 at 13:38
  • IIFE - http://benalman.com/news/2010/11/immediately-invoked-function-expression/ – shem86 Oct 04 '13 at 13:38
  • It's not a self-executing function. Do you see it executing itself anywhere? note: that means the function is calling itself inside itself. This function is just getting invoked anonymously, not by itself. – Joe Simmons Oct 04 '13 at 13:38
  • I view this one like a ninja shuriken – JAL Oct 04 '13 at 13:39
  • Another/better duplicate: http://stackoverflow.com/questions/440739/what-do-parentheses-surrounding-a-javascript-object-function-class-declaration-m – bfavaretto Oct 04 '13 at 13:41
  • I wish I had an animation of the progression/evolution of comments and answers from "self-executing anonymous function" to "immediately invoked function expression." including my own, since-deleted comment). – Michael Paulukonis Oct 04 '13 at 13:46
  • this may be marked as a duplicate, but the accepted answer on the duplicate site is not the best answer in the world. It certainly does not address the "name" issue asked here (of which there are two -- a common misnomer, and another option). – Michael Paulukonis Oct 04 '13 at 13:58
  • @bfavaretto your duplicate-question addresses "what does it do", but not "what is it called?" which is a legitimate question on its own (and as pointed out, fraught with argument). – Michael Paulukonis Oct 04 '13 at 14:00
  • @MichaelPaulukonis True, still I think it's good to mark questions as duplicates whenever possible, and marking them as duplicates of multiple other questions when applicable. This way the OP and future visitors can be directed to other/different explanations for what they're asking. That does not invalidate the answers here -- although the naming issue might be discussable, as the language specification does not mention IIFEs (it surely mentions function expressions, but doesn't discuss immediate invocation). Granted, IIFE is currently the most used term for them. – bfavaretto Oct 04 '13 at 14:24
  • The language specification doesn't need to mention IIFE's, because it's a usage pattern. – Michael Paulukonis Oct 04 '13 at 14:46

4 Answers4

5

It's also known as an Immediately Invoked Function Expression

Ben Alman has a good article covering IIFE usage, and why "self-executing anonymous function" isn't the best terminology:

One of the most advantageous side effects of Immediately-Invoked Function Expressions is that, because this unnamed, or anonymous, function expression is invoked immediately, without using an identifier, a closure can be used without polluting the current scope.

[... T]the term “self-executing” is somewhat misleading, because it’s not the function that’s executing itself, even though the function is being executed. Also, “anonymous” is unnecessarily specific, since an Immediately Invoked Function Expression can be either anonymous or named. And as for my preferring “invoked” over “executed,” it’s a simple matter of alliteration; I think “IIFE” looks and sounds nicer than “IEFE.”

Michael Paulukonis
  • 9,020
  • 5
  • 48
  • 68
3

It is an Immediately-Invoked Function Expression.
It's useful for creating a scope for its contents to enjoy. It's similar to this:

function myFunc() {
    //code
}
myFunc();

But there are 2 differences:

  • The function is named here
  • The above code is a function declaration, different from a function expression. 'myFunc' above gets stored as a variable unless you use it as an expression, like so:

    foo(function myFunc() {
        //code
    });
    

Then it will not get stored as a variable.

p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
Sergio
  • 28,539
  • 11
  • 85
  • 132
3

It's a an immediately invoked function expression (Fiddle):

(function () {
  alert("pee");
}());
Joren
  • 3,068
  • 25
  • 44
1

It's a immediately-invoked function - as soon as you declare it, it gets evoked.

The () syntax in Javascript means "call this function with whatever arguments are between ( and ). Since there isn't anything between the parentheses in your example, no arguments are supplied to the function call; but if you need to supply arguments, you can do something like this:

(function foo(arg1, arg2) {
   alert(arg1 + " " + arg2);
})(3, 5);

Which will immediately call foo() and pass in 3 and 5 as the two arguments.

Bucket
  • 7,415
  • 9
  • 35
  • 45