It is not possible to call an anonymous function if it is not assigned to a variable.
In your example:
consomeCsr = (function () {
return alert('Funcção Anonima')
})();
consomeCsr();
consomeCsr
is not a function. This IIFE is run immediately and the variable consomeCsr
receives the value of undefined
, which is what the alert
leaves / returns.
If you want to have a function that runs this alert then you should have:
var consomeCsr = function () {
alert('Funcção Anonima');
};
But if you want a self-executing function ( IIFE ) but also stay in a variable to use later, you have to give it a name to be able to return and reuse like this:
var consomeCsr = (function autoExecutavel() {
alert('Funcção Anonima');
return autoExecutavel;
})();
jsFiddle: link