I've been using IIFE in JavaScript
and in AngularJS
and have been using the following structure:
Method 1:
//IIFE Immediately Invoked Function Expression
(function () {
}());
However, I've seen the following often where a variable is assigned to the IIFE
Method 2:
//IIFE Immediately Invoked Function Expression assigned to doStuff variable
var doStuff = (function () {
}());
NOTE: This question is NOT about what this pattern is or what an IIFE is. This is pertaining specifically to why one would use a return variable on an IIFE and its relation to Angular practices as well.
In Angular Method 1 works fine, but in many of the raw JS examples I see, Method 2 is used. My assumption is that anything encasulated in doStuff
will be avliable via it and callable. However, I'm not 100% sure on the exact reasoning or distinction between the 2 methods and need some help understanding when to use the different methods?