Use an IIFE to Create a Module - Why do I need to use IIFE

Use an IIFE to Create a Module - Why do I need to use IIFE
0

#1

Tell us what’s happening:
I am very confused by this problem as to why I need to use IIFE to create a module.
I want you to take a look at my code. As you can see, I did not use IIFE here but I still passed this challenge, indeed.
Is there any reason it is better to create a module using 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(){},
  singMixin: function(){}
};

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/object-oriented-programming/use-an-iife-to-create-a-module/


#2

I checked the test script and it does not actually validate you used an IIFE, which is why you passed the tests. When called via IIFE, your funModule should return an object containing the isCuteMixin and singMixin functions. Instead, you just created an object named funModule with the applicable functions inside it.

Why use an IIFE? Read @DanCouper’s great reply in the following thread.