You can use:
foo()()
or the same with assignment to a variable:
var a = foo();
a();
Note that it will output both value2
and then value1.
How is this working?
In order to make it easier to understand the provided code, consider the following example which makes the same result as your code:
function foo() {
var bar;
quux = 'value2';
console.log(quux);
return function() {
var quux = 'value1';
bar = true;
console.log(quux);
};
}
var a = foo(); // executes code, returns function and stores it in a variable
a(); // executes function, stored in a variable
I guess, it is easier to understand this code now - it executes some code, returns a function, which you call later.
Now, consider another example:
function foo() {
var bar;
quux = 'value2';
console.log(quux);
var zip = function() {
var quux = 'value1';
bar = true;
console.log(quux);
};
return zip;
}
var a = foo(); // executes code, returns function and stores it in a variable
a(); // executes function, stored in a variable
This code does the same, but it stores a function in a variable before returning as result. The code that you have provided in example almost does not differ from the last example, except for function declaration:
These are almost equivalent (why almost?):
var zip = function() {
};
function zip() {
}