0

I found this in the index.js of a module I used recently:

(function() {
  module.exports = require('./lib/somelib');
}).call(this);

These were the entire contents of the file. somelib had the definitions that were actually being exported. What would be the purpose of this and how is this different from simply having:

  module.exports = require('./lib/somelib');

I understand what an IIFE is for (typically for creating a closure with a reference to a "hidden" scoped variable. I'm having trouble seeing any benefit to this above pattern however.

Can anyone enlighten me?

scorpiodawg
  • 5,612
  • 3
  • 42
  • 62
  • Maybe IIFE is added automatically by some build process (i.e. it was not intentionally added) – Felix Kling Nov 02 '17 at 22:37
  • 1
    [Coffeescript produces these](https://stackoverflow.com/q/4542942/1048572) – Bergi Nov 02 '17 at 23:38
  • @Bergi Looks like you're on the money (though it's hard to conclusively prove since the source I looked at didn't have any coffeescript AFAICT). The explanations on that page make sense. Feel free to make this an answer that I'll accept. – scorpiodawg Nov 06 '17 at 19:43

0 Answers0