Use this if you want to repeatedly call a function for known arguments, or in case you just need a IIFE.
If you want a repeatedly called functions, you would typically -
- Call function in a loop
- Use recursion
For e.g. -
function printStr(str) {
console.log(str);
}
printStr("hello");
printStr("people");
/*
hello
people
*/
You can do it in a more concise way like so -
(function(str) {
console.log(str);
return arguments.callee;
// return function for next call
})("hello")("world")("and")("people");
/*
hello
world
and
people
*/
Note that you are not returning a string, but a whole function and calling that function for the given arguments. In the statement, we use arguments to call the function that returns a function, and invoke the returned function on a specific argument thereon.
You may remember arguments from our discussion on arguments object in Javascript and practical application from our curry functions in Javascript post.