Use an IIFE to Create a Module question

Tell us what’s happening:

Since this is an immediately invoked function, shouldn’t it print “He is cute” to the console along with “Hey Jude”?

I’m not sure if you would create an object and then instantiate it to have access to the return object isCuteMixin.any propertites within the isCuteMixin?

I’ve never seen anything like this before. Sorry if I’m not making sense.

Your code so far


let funModule = (function () {
  return {
    isCuteMixin: function (obj) {
      obj.glide = function() {
        console.log("He is cute");
      };
    },
    singMixin: function(obj) {
      obj.fly = function() {
        console.log("Hey Jude");
      };
    }
  }
}) (); 

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 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

Nope. That IIFE is indeed executed immediately, but the only thing it’s doing is returning an object. This object:

{
    isCuteMixin: function (obj) {
      obj.glide = function() {
        console.log("He is cute");
      };
    },
    singMixin: function(obj) {
      obj.fly = function() {
        console.log("Hey Jude");
      };
    }
  }

That object has two properties that reference functions. However, none of those functions were actually called in your code yet. So far all you have is that object, stored in funModule variable. You could go ahead and call one of them, but they take an object as an argument so we’ll need to make one:

const paulMcCartney = { }; // our object
funModule.singMixin(paulMcCartney) // we call singMixin, which gives our object a new property
paulMcCartney.fly() // logs "Hey Jude" to the console.
2 Likes