Immediately invoked function question

Hey. I just reached the immediately invoked function expression (IIFE) topic and the thing that got me wondering is this:

let motionModule = (function () {
  return {
    glideMixin: function(obj) {
      obj.glide = function() {
        console.log("Gliding on the water");
      };
    },
    flyMixin: function(obj) {
      obj.fly = function() {
        console.log("Flying, wooosh!");
      };
    }
  }
})();

The code snippet from above is an usage example of IIFE, that is shown in the course. My question is, why would i ever want to do that, if i can do this:

let motionModule = {
  glideMixin: function(obj) {
    obj.glide = function() {
      console.log("Gliding on the water");
    };
  },
  flyMixin: function(obj) {
    obj.fly = function() {
      console.log("Flying, wooosh!");
    };
  }
};

It’s less typing and easier to read imo. Is there any meaningful difference between these 2 ways of creating a module?

1 Like