Manually Invoke Iife
Solution 1:
Try this:
var funcName = (function funcName() {
// rest of the codereturn funcName;
}());
Also see this jsbin.
Solution 2:
The whole idea of the IIFE is that its an anonymous function that is immediately executed. So by definition, no there is no way to re-execute it.
With that said however, you can store the function expression to a global variable, and execute it. For example
window.my_iife = (function() { /* stuff */ });
window.my_iife();
Notice the slight difference in syntax compared to the traditional IIFE: ((function() {})());
By storing the function in window
, you are able to access it later from either the developer console or anywhere else in your code. If you simply store it in a var
, or declare it as function my_iife() { /* ... */ }
somewhere you risk that var
or function declaration itself being wrapped in an IIFE and thus being inaccessible. As an example, that scenario could occur if the file you declared your var
/function
in is part of a Sprockets manifest (such as application.js
in Rails).
Solution 3:
var funcName = null;
(funcName = function funcName() {
// rest of the code
return funcName;
}());
funcName();
This is what worked for me
Post a Comment for "Manually Invoke Iife"