Why use IIFE for creating a module?

Tell us what’s happening:
Hi, im new to programming and just dont get why i should use IIFE to create a module if
the result is the same without IIFE?
Your code so far


let isCuteMixin = function(obj) {
obj.isCute = function() {
  return true;
};
};
let singMixin = function(obj) {
obj.sing = function() {
  console.log("Singing to an awesome tune");
};
};
let funModule = {
isCuteMixin: function(obj) {
  obj.isCute = function() {
    return true;
  };
},
singMixin: function(obj) {
  obj.sing = function() {
    console.log("Singing to an awesome tune");
  };
}
}
let test = {};
funModule.singMixin(test);
test.sing();
let arr = [];
for(let props in test) {
arr.push(props);
}
console.log(arr)

  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:87.0) Gecko/20100101 Firefox/87.0.

Challenge: Use an IIFE to Create a Module

Link to the challenge:

Yeah IIFEs are weird. To be honest, in 2.5 years as a JS developer, I can count how many times I’ve used them on one finger. Others may have different experiences.

There is some discussion here on the advantages. You can also read here and here and a thousand other places.

1 Like

The second link helped me, thank you for your effort and time. Didnt see that, in this case, it is for privacy reasons, now it makes sense to me.

1 Like

Yeah, “keeping things private” is not something JS has historically handled well. It’s gotten better - IIFEs can help with that, among other things.

1 Like

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