3

I've seen that code in open source projects and I wonder what is its intent:

'use strict';

function invariant(condition, message) {
  if (!condition) {
    throw new Error(message);
  }
}

(0, invariant)(false, 'failed');

Is this done to disable assertions? If it is, then I was unable to figure out how.

John Difool
  • 5,572
  • 5
  • 45
  • 80
  • 2
    You can take a look at this post: [Calling function with window scope explanation (0, function(){})()](http://stackoverflow.com/questions/6577812/calling-function-with-window-scope-explanation-0-function). – ConnorsFan Sep 24 '16 at 19:38
  • Also see [(1,eval)('this') vs eval('this')](http://stackoverflow.com/questions/9107240/1-evalthis-vs-evalthis-in-javascript) – ASDFGerte Sep 24 '16 at 19:41
  • So it seems that the main idea is that "indirect references are guaranteed to execute in global scope." and (0, foo) always yield an indirect ref. It results in `this` being set to global scope. It's a nifty trick, just not plain obvious. – John Difool Sep 24 '16 at 19:51
  • There won't be any difference given that code example than if they just did `invariant(false, 'failed')`. –  Sep 24 '16 at 20:16

0 Answers0