0

I noticed in some transpiled code recently an immediately invoked function expression that took the form of e.g.:

(0, function () { console.log('invoked!'); })();

I am used to writing this simply as:

(function () { console.log('invoked!'); })();

What does the 0, bit do here?

magritte
  • 7,396
  • 10
  • 59
  • 79
  • That isn't a duplicate of this one. That question has nothing to do with IIFE and the reason for doing it there is unrelated. @Amy, et al. –  Jan 08 '18 at 17:06
  • 1
    Wait, the OP closed it as a duplicate too? Now I'm really confused. Are you asking about what that question is asking about or did you actually see this syntax used with an IIFE? –  Jan 08 '18 at 17:08
  • @rockstar yes, that question was also marked as a duplicate of this one, https://stackoverflow.com/questions/32275135/why-does-babel-rewrite-imported-function-call-to-0-fn, which answered my question and I learnt something in the process! (comma operator), cheers! – magritte Jan 09 '18 at 17:12
  • Read my comments more closely. That isn't a duplicate. You're asking about IIFE functions, and there's no effect on an IIFE like there is in the situation described by the duplicate. –  Jan 09 '18 at 17:14
  • @rockstar indeed, the take away i had was that it doesn't do anything in my case, but the transpiler does it that way for that reason – magritte Jan 09 '18 at 17:17
  • I don't know if there isn't any reason, so that shouldn't be a takeaway. Maybe the transpiler does have a reason for it. I just know that the reason given in the non-duplicate doesn't apply here, so I don't know why you closed it. –  Jan 09 '18 at 17:18
  • 1
    @rockstar you need to read the dup-dup which entirely answers magritte's question: https://stackoverflow.com/questions/32275135/why-does-babel-rewrite-imported-function-call-to-0-fn – Jerome Jan 09 '18 at 17:44
  • @Jerome: That technique is only relevant to functions that are referenced from an object. There's no explanation in the dup-dup or the dup as to why a transpiler would do it for an IIFE, since this technique would have no effect in that case. Again, maybe there's a reason but there's really no context given here, so it's hard to tell. –  Jan 09 '18 at 18:35
  • I guess we'll never know but we should keep beating it to death anyway, just to annoy Margritte. – Jerome Jan 10 '18 at 10:06

0 Answers0