3
(function() {

  //do stuff

})();

EDIT: I originally thought this construct was called a closure - not that the effect that it caused results (potentially) in a closure - if variables are captured.

This is in no way to do with the behaviour of closures themselves - this I understand fully and was not what was being asked.

James Westgate
  • 11,306
  • 8
  • 61
  • 68
  • 2
    http://stackoverflow.com/questions/3720283/what-is-this-practice-called-in-javascript – user113716 Oct 06 '10 at 12:56
  • Note to closers, the question has been rephrased slightly differently - is it called a closure? - not what is the name of this construct? – James Westgate Oct 06 '10 at 13:03
  • 2
    I think the answer there is pretty much what you need. You should've searched before asking. That's all. – gblazex Oct 06 '10 at 13:07
  • 1
    I think @galambalazs is right. In my opinion, that question really covered the bases of when you do and do not have a closure. I learned a lot from it. If you have a specific scenario in mind where you're confused as to whether or not it created a closure, you may want to post that. – user113716 Oct 06 '10 at 13:12
  • Actually I have always called this a 'closure', and thats what I want to clear up. Search google/SO for closure and you wont find the answer. – James Westgate Oct 06 '10 at 13:14
  • @James - I had as well before the discussions/answers in the link I posted. I think you'll find good information on when a closure is/isn't created. – user113716 Oct 06 '10 at 13:16
  • Edgar and AHM have got it. Shameless self-promotion: I wrote a blog post explaining closures here: http://sleeplessgeek.blogspot.com/2009/12/so-what-are-these-closure-thingys.html. The follow-up post shows a way you might use one to add sequentially-numbered inputs to a form: http://sleeplessgeek.blogspot.com/2010/01/adding-fields-to-web-form-job-for.html – Nathan Long Oct 06 '10 at 13:36
  • IIFE Pattern: http://benalman.com/news/2010/11/immediately-invoked-function-expression/ – HaNdTriX Feb 07 '15 at 15:10

5 Answers5

3

It is an anonymous function (or more accurately a scoped anonymous function) that gets executed immediately.

The use of one is that any variables and functions that are declared in it are scoped to that function and are therefore hidden from any global context (so you gain encapsulation and information hiding).

Oded
  • 489,969
  • 99
  • 883
  • 1,009
2

it's an anonymous function but it's not a closure since you have no references to the outer scope

http://www.jibbering.com/faq/notes/closures/

  • You don't know that.. He is "doing stuff" in there. – Thilo Oct 06 '10 at 13:00
  • 1
    no, he just has a comment ;). BTW if he has got some references on the outer scope then he has a closure –  Oct 06 '10 at 13:03
1

I usually call it something like "the immediate invocation of an anonymous function."

Or, more simply, "function self-invocation."

Matt Ball
  • 354,903
  • 100
  • 647
  • 710
1

No, a closure is rather something along these lines:

function outer()
{
    var variables_local_to_outer;

    function inner()
    {
        // do stuff using variables_local_to_outer
    }

    return inner;
}

var closure = outer();

the closure retains a reference to the variables local to the function that returned it.

Edit: You can of course create a closure using anonymous functions:

var closure = (function(){

    var data_private_to_the_closure;

    return function() {
        // do stuff with data_private_to_the_closure
    }

})();
Edgar Bonet
  • 3,416
  • 15
  • 18
1

Kindof. It's doesn't really close around anything though, and it's called immediately, so it's really just an anonymous function.

Take this code:

function foo() {
    var a = 42;
    return function () {
        return a;
    }
}

var bar = foo();
var zab = bar();
alert(zab);

Here the function returned by foo() is a closure. It closes around the a variable. Even though a would apear to have long gone out of scope, invoking the closure by calling it still returns the value.

AHM
  • 5,145
  • 34
  • 37