I'm writing a JavaScript style-guide for my team, so that we can organise and contribute our documents easier. But I've hit a small bump which is where my question applies...
What am I supposed to call an anonymous JavaScript function that's called immediately. I know I could quite simply call it an "anonymous function", but I'd like to stress the fact that it's being called immediately.
Here is an example:
var MyVariable = (function(data){
return "another value"
})("some value");
console.log(MyVariable);
// "another value"
-
1Note you will often see this construct (inaccurately) referred to as a "self-calling function"AakashM– AakashM2015年08月04日 11:04:34 +00:00Commented Aug 4, 2015 at 11:04
1 Answer 1
They already have a term for that in the Javascript world. They are called Immediately Invoked Function Expressions (IIFE).
What it is
IIFE functions are not given a name. Instead, they are executed once as the interpreter comes across them:
var area = function() {
var width = 3;
var height = 2;
return width * height;
}();
The final parentheses after the closing curly brace of the code block tell the interpreter to call the function expression immediately.
If you write a function declaration you must add grouping operators, or parentheses surrounding the function, to tell the interpreter to treat the function as an expression that can be immediately invoked:
var area;
(function() {
var width = 3;
var height = 2;
area = width * height;
}());
When they are used
IIFEs are used for code that only needs to run once within a task, rather than being repeatedly called.
- As an argument when a function is called (to calculate values, etc.)
- To assign the value of a property to an object.
- In event handlers and listeners.
- To prevent conflicts between two scripts that might use the same variable names. They can be used as wrappers to drop code in another script where you're not sure if variable names may be the same.
-
One nitpick: In your example using
var area = ...
, you don't need the "wrapping" parentheses on the function, because it's already a function expression by virtue of being on the right-hand side of the=
. The wrapping parentheses are only needed when the function is written as a function declaration (i.e. without the leadingvar area = ...
).Eric King– Eric King2015年08月03日 23:27:12 +00:00Commented Aug 3, 2015 at 23:27 -
@EricKing did I do it right?Lawrence Aiello– Lawrence Aiello2015年08月03日 23:53:43 +00:00Commented Aug 3, 2015 at 23:53
-
It could be nice to be able to more casually call these 'express functions'strainer– strainer2015年08月04日 00:27:34 +00:00Commented Aug 4, 2015 at 0:27
-
@LawrenceAiello I added an edit to show what I meant. Hopefully it still works for you.Eric King– Eric King2015年08月04日 03:40:17 +00:00Commented Aug 4, 2015 at 3:40
-
As a related question, is there any reason to pass an argument to the IIFE as the question shows? I've only ever seen these like how you demonstrate -- without any arguments (since any argument can be a local variable).Kat– Kat2015年08月05日 14:57:17 +00:00Commented Aug 5, 2015 at 14:57