I'am studying the behaviour of Immediatly Invoked Function Expressions (IIFE) and while doing that I encounterd the following situation.
(function () {
document.write("bar");
})
(function () {
document.write("foo");
}());
I thought that the first is just a grouping operator with a function expression inside without calling it. The second is a grouping operator as well with a function expression but now with the call of that function.
What I find strange is that both are invoked, why is that?
(function () {
document.write("bar");
})
var x = 1;
(function () {
document.write("foo");
}());
When I break the two by inserting a variable declaration in between, it's just writes foo. This is what I expected.
Immediately invoked function expressions can be used to avoid variable hoisting from within blocks, protect against polluting the global environment and simultaneously allow public access to methods while retaining privacy for variables defined within the function.
An Immediate-Invoked Function Expression (IIFE) is a function that is executed instantly after it's defined. This pattern has been used to alias global variables, make variables and functions private and to ensure asynchronous code in loops are executed correctly.
An IIFE (Immediately Invoked Function Expression) is a JavaScript function that runs as soon as it is defined. The name IIFE is promoted by Ben Alman in his blog. (function () { // … })(); (() => { // … })(); (async () => { // …
An IIFE (Immediately Invoked Function Expression) can be used for avoiding the variable hoisting from within the blocks. It allows the public access to methods while retaining the privacy for variables defined in the function. IIFE is a design pattern that is also known as the Self-Executing Anonymous Function.
Because you forgot the semicolon after the first function expression:
(function () {
document.write("bar");
});
Otherwise the second "grouping operator" is interpreted as a function call. So this:
(function a() {
...
})
(function b() {
...
}());
is basically the same as:
function b() {
...
}
(function a() {
...
})(b());
Reordering makes it a bit easier to see. Remember that white space characters have no meaning in JavaScript and are ignored.
As Felix Kling correctly noted: the missing semicolon causes the parentheses around the second IIFE to be interpreted as a function call rather than just grouping the function expression. It becomes much more clear without the newlines:
(function () {
document.write("bar");
})(function () {
document.write("foo");
}());
Or with some realignment:
(function () {
document.write("bar");
})(
function () {
document.write("foo");
}()
);
The first function expression is called with the result of the second function expression as its first and only argument. You should also note that foobar
is written rather than barfoo
, as the second function is called first and its result is passed as an argument to the first function.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With