When it comes to using IIFE in JavaScript and AngularJS, I have come across two common structures:
Structure 1:
//IIFE Immediately Invoked Function Expression
(function () {
}());
However, there is another structure where the IIFE is assigned to a variable as seen below:
Structure 2:
//IIFE Immediately Invoked Function Expression assigned to doStuff variable
var doStuff = (function () {
}());
PLEASE NOTE: This inquiry is not about what an IIFE is, but rather focuses on why one would use a return variable within an IIFE and how it relates to Angular practices.
In Angular, Structure 1 works perfectly fine. However, in many native JavaScript examples, Structure 2 is preferred. The assumption is that anything encapsulated within doStuff
will be accessible and callable through it. Yet, the exact reasoning behind this distinction between the two methods remains unclear. Any help in understanding when to use each method would be greatly appreciated.