Self-executing functions
Update: "Immediate functions" sounds like a much better name.
Self-executable functions are simple and powerful.
All you do is add a set of parentheses after the function and this causes it to be executed right there. If it’s an anonymous function you also need to wrap it in parentheses.
(function(){ var a = 1; var b = 2; alert(a + b); })();
You can also pass parameters to the self-executing function if you want.
(function(a, b){ var c = a + b; alert(c); })(1, 2);
So how is this pattern useful?
It’s useful when you have some work to do, some initialization maybe. You need to do it only once and you don’t want to leave any globals lying around after the work is finished. All the temporary variables you need like c in the case above remain local and don't pollute the namespace.
Useful also when your code is kind of a guest to the page. Like a bookmarklet for example, or a widget of some sorts. You don’t want to add your variables and functions to the host page’s naming space and possibly causing a naming conflict with the page's core code.
Tags: bookmarklet, self-executing, widget
July 4th, 2010 at 3:16 pm
awesome bless you for composing this
August 3rd, 2010 at 11:25 am
I didn’t understand the part of:
“…You don’t want to add your variables and functions to the host page’s naming space and possibly causing a naming conflict with the page’s core code.”
How can this scenario happen?
Can you give me an example?
August 5th, 2010 at 8:43 am
@Avi – I think the author is trying to get across that the variables and functions inside a self-executing anon function will not be global and therefore won’t clobber any variables / functions that may already exist in the global namespace, i.e. if you declare a var / function without namespacing it or using a particular function pattern it will be part of the global namespace, i.e. the window object in a browser, hope this helps….
December 28th, 2010 at 5:22 pm
I’m using this method exactly in the bookmarklet I created. Works perfect!
April 26th, 2011 at 4:17 pm
@Avi – There are plenty of examples. How often do you name variables something common?
Perhaps you use a single letter for a quick toggle value or whatnot, so you make a var i, var x, or var o. If these are in the global namespace, it would not be uncommon for another function to use the same name which can cause some interesting bugs. Or what if you name something like “isComplete” or “isReady” or any number of other generic, yet descriptive, names? By putting these in a self-executing function to reduce the likelihood of causing conflicts.
January 3rd, 2012 at 2:04 pm
We really enjoy your site publish. You can find truly lots of procedures we could place it to good use by the use of no work soon enough and economic assets. Thanks a great deal pertaining to supporting make this submit give light to quite a few troubles now we have encountered before now.
January 28th, 2012 at 7:15 pm
[...] (engl. function expression) zugewiesen, der sofort ausgeführt wird. Dies wird auch als "immediate function" bezeichnet. Rückgabewert ist dabei ein weiterer anonymer Funktionsausdruck, der bis auf ein Detail [...]