This kind of code is typically generated by transpilers (like Babel), in order to convert modern JavaScript -- that uses the most recent additions to the specification -- to a JavaScript version that is more widely supported.
Here is an example where this transpilation pattern occurs:
Let's say we have this original code before transpilation:
import {myfunc} from "mymodule";
myfunc();
To make this ES5-compatible code, you could do this:
"use strict";
var mymodule = require("mymodule");
mymodule.myfunc();
But here we would execute myfunc
with mymodule
as this
value, which is not happening in the original code. And although that might not always be an issue, it is better to make sure the function behaves just like it would in the original version, even if that function would use a this
reference -- how unusual or even useless that use of this
in myfunc
might be (because also in the original version it would be undefined
).
So for instance, if the original code would throw an error because of a this.memberFun()
reference in the function, it will also throw in the transpiled version.
So that is were the comma operator is used to get rid of that difference:
(0, mymodule.myfunc)();
Granted, in code that you write yourself, you would never have a good use case for this pattern, as you would not use this
in myfunc
in the first place.