This is not due to an anonymous function, because else
with this return
is redundant. You do not need it, since return exits the function, if the if
not true, then return
will be executed by default.
(function(){ document.getElementById("element").onclick = function(){ var r = confirm("Are you sure ?"); if (r){ return true; } return false; } })();
Edit:
As the nebulae said, this can be done even shorter:
(function(){ document.getElementById("element").onclick = function(){ return confirm("Are you sure ?"); } })();
source share