-2

I understand that IIFE is an anonymous function which invokes it self, but I'm pretty curious to know, where this is used. Can any one please explain one quick use case?

(function() {
  console.log('IIFE!');
})();
beingGeek
  • 1
  • 1
  • It's used as a convenient way to create a scope. – zerkms Jun 07 '14 at 07:26
  • 2
    It doesn't "invoke itself" unless it uses recursion. – RobG Jun 07 '14 at 07:43
  • thanks @mpm but I have gone through that post and was not satisfied with explanation there – beingGeek Jun 07 '14 at 07:45
  • @mpm: Yeah, although it covers similar ground, and a couple of the answers *touch* on it, this question is much more targeted on "What is it for?" rather than "What is it?" (Good work, though, trying to find duplicates. The system should encourage that more.) – T.J. Crowder Jun 07 '14 at 07:50

1 Answers1

1

It's a great way to create a container for things; a new, contained scope. I use them around all of my JavaScript in a page, for instance:

(function() {
    var x;

    function foo() {
    }
})();

Without the IIFE, if I just had this at the topmost scope of a normal script:

var x;

function foo() {
}

...then x and foo would be globals, and the global namespace is already way overcrowded. By putting them inside a function, I keep them contained. They can refer to each other, they're common to my code inside that IIFE, but they're not globals.

It doesn't just have to be for all of the code on a page, either; I use this for privacy at multiple levels. For instance, if I want to create something with one external symbol but a number of internal pieces:

var externalFoo = (function() {
    var someVariable;

    function foo() {
    }

    function bar() {
    }

    return foo;
})();

That creates one symbol in the outer context (which may or may not be global, depending on where the codeblock above is), externalFoo. Inside the IIFE, I have a number of private things that presumably help externalFoo do its job, but they're "plumbing" that shouldn't be accessible from outside.

For me, this is the primary use for them, creating privacy for their contents.

Some people use IIFEs to create new scopes for closures in loops, like this:

var i;
for (i = 0; i < 10; ++i) {
    (function(index) {
        setTimeout(function() {
            doSomthingWith(index);
        }, index * 100);
    })(i);
}

That's so that the doSomethingWith sees each value (0, 1, 2, ...). For that use case, though, I prefer not to create and destroy the outer anonymous function (the IIFE) on each loop iteration (instead using a single builder function), and I prefer to be clearer about what I'm doing:

var i;
for (i = 0; i < 10; ++i) {
    setTimeout(buildHandler(i), i * 100);
}

function buildHandler(index) {
    return function() {
        doSomthingWith(index);
    };
}
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • "Some people use them to create new scopes" --- what you explained in the first part of your answer is also a scope. – zerkms Jun 07 '14 at 07:36
  • "I prefer not to create and destroy the anonymous function on each loop iteration" --- your solution does create an anonymous function on every iteration loop though as well. – zerkms Jun 07 '14 at 07:37
  • @zerkms: "Some people use them to create new scopes **for closures**" is the point. – T.J. Crowder Jun 07 '14 at 07:37
  • @zerkms: It creates one. It doesn't destroy one (until it's been used, but we need it until then). The example using IIFE creates **two** and destroys one (eventually, when the other is released). The second one is completely unnecessary. – T.J. Crowder Jun 07 '14 at 07:38
  • okay. I just prefer not to confuse people around with different purposes. It's just a scope. That's it :-) – zerkms Jun 07 '14 at 07:39
  • @zerkms: Well, actually, in the second case it's not just a scope. It's a scope *and* a series of execution contexts. – T.J. Crowder Jun 07 '14 at 07:39
  • I prefer the Occam's razor in this case actually :-) They are just scopes with *something* inside ;-) – zerkms Jun 07 '14 at 07:41
  • thanks for the response! but I think if i declare x in a function the the scope will be still in the function only right, I mean it will not e global anyway. function test() { var a=0; console.log(a); } In this function I think variable a is still local variable and can't access globally, please correct me if I'm wrong – beingGeek Jun 07 '14 at 07:42
  • @beingGeek: yep, but this function will be global. Which isn't the case with IIFEs – zerkms Jun 07 '14 at 07:43
  • @beingGeek: Right. But without the IIFE, if you have `var x;` and `function foo() { }` at the top level of a script, they become globals. The point of the IIFE is to create a scope to wrap around them, so they aren't globals. – T.J. Crowder Jun 07 '14 at 07:44