Example Code in "Use an IIFE to Create a Module"

Tell us what’s happening:
I’m struggling understanding a detail in function declaration. The code example given to us here is:

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 two parentheses cause the function to be immediately invoked

I don’t really understand why the difference in syntax betwen the first ( function () {})(); , with a space after function, and the next one with no space: obj.glide = function() {};

I tried the IIFE with both space or no space, and it’s working. Is there a difference between those two, is there one I should use over the other?

Thanks!

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

No difference, use whichever you think is more readable.

1 Like

If you’re asking if there is difference between function (obj) and function(obj)
There is no difference, they’re the same. It is just you preference how you write them.

On other hand if you’re asking whats the difference between IFFY( function () {})(); and regular function declaration function() {}.
IFFY is invoked(called) immediately.
// The two parentheses cause the function to be immediately invoked
And other one is invoked when you call. Ah i didnt gave name that function…ugh… now i cant invoke it… but you would
function funky() {}
funky()

What invokes iffy are those last 2 parentheses
( function () {})()

2 Likes

Thanks both of you.
I understand the concept of IIFE my question was really just about the space in the syntax, I wanted to make sure I wasn’t missing something

It is, however, an inconsistency in formatting that really shouldn’t be there. Now granted it is easy to miss, which is why code formatters like Prettier are used.