Encountered some code that's using IIFEs in an expression rather than just a normal function.
var custom_type = (function() {
return $('#myDiv').attr('custom_type');
})();
Normally I would write this as something like:
var custom_type = function() {
return $('#myDiv').attr('custom_type');
};
What's the reason for the IIFE? The only thing I can think of is that the IIFE might assign the custom_type
variable only once at the beginning whereas the second might continue to check for the updated type every time the variable is referenced.
var custom_type = $('#myDiv').attr('custom_type')
. So both examples have different results (in the first one,custom_type
is a string, and in the second one it's a function) and without context it's impossible to tell why the first one was used. Presumably whoever wrote the code didn't wantcustom_type
to be a function. – Vinna