What is the purpose of wrapping a mixin module in an IIFE?

Tell us what’s happening:
The instructions for this exercise would have the code below wrapped in an IIFE where the only difference is that funModule would be ensconced in a function wrapper like:

let funModule = (function() { 
  return {
    // properties of funModule below
    // .....
  }
}) ();

I’m not understanding the purpose of wrapping the object funModule in an IIFE assigned to a variable of the same name. I understand that the anonymous function returning the object with the mixin properties is automatically invoked, but it seems to work just the same (and even passes the tests) without the IIFE wrapper like my code below:

   **Your code so far**

let funModule = {
 isCuteMixin: function(obj) {
   obj.isCute = function() {
     return true;
   };
 },
 singMixin: function(obj) {
   obj.sing = function() {
     console.log("Singing to an awesome tune");
   };
 }
}

function Bird() {
	let hatchedEgg = 10;
	
	this.getHatchedEggCount = function() {
		return hatchedEgg;
	};
}

let ducky = new Bird();

funModule.singMixin(ducky);

console.log(ducky.sing());

   **Your browser information:**

User Agent is: Mozilla/5.0 (X11; Linux x86_64; rv:98.0) Gecko/20100101 Firefox/98.0

Challenge: Use an IIFE to Create a Module

Link to the challenge:

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.