Why does babel rewrite imported function call to (0, fn)(...)?
(0, _b.a)()
ensures that the function _b.a
is called with this
set to the global object (or if strict mode is enabled, to undefined
). If you were to call _b.a()
directly, then _b.a
is called with this
set to _b
.
(0, _b.a)();
is equivalent to
0; // Ignore result
var tmp = _b.a;
tmp();
(the ,
is the comma operator, see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comma_Operator).
Why does babel wrap _sourceMapSupport.install() with (0, ...)()?
The comma is explained in What does a comma do in JavaScript expressions?. basically, it evaluates all the expressions, and returns the value returned by the last one.
Probably, the reason for using that is to be able to call the method as if it weren't a method.
Consider this function:
function f() { return this; }
And let's make it a method:
var o = {f: f}
Then, although f === o.f
, the result will vary depending on how you call it:
o.f(); // o
f(); // global object (in non-strict mode)
f(); // undefined (in strict mode)
So babel uses the comma approach to get a reference to the function, without associating it with the object. This way the method can be called as if it were a global function, without being one.
(0, o.f)(); // global object (in non-strict mode)
(0, o.f)(); // undefined (in strict mode)
What is the purpose of calling (0, func)() in JS?
The sometimes mystifying semantics of JavaScript are probably the reason. The expression
(0, _utilities.validateNextState)
evaluates of course to a reference to that function. However, because it's in that parenthesized subexpression, the function call that's outside that will be made without that _utilities
object being recognized as the context for the call (the this
value). Thus inside the validateNextState
function, this
will be either undefined
or a reference to the global object, depending on the "strict" mode state.
I suspect that Babel does that because in the original source code the call to validateNextState()
is made as if it were a "naked" function, not a method on an object. Babel doesn't know (probably) whether the value of this
matters to that particular function, but it has to make sure it gets invoked safely.
Zero as first argument in IIFE
The 0
is ignored. It is there solely to allow the comma operator to evaluate _classCallCheck3.default
and get the function without this
being _classCallCheck3
.
Related Topics
Pad a Number With Leading Zeros in JavaScript
Angularjs: How to Pass Variables Between Controllers
How to Use Setinterval and Clearinterval
How Does This Object Method Definition Work Without the "Function" Keyword
How to Get the Difference Between Two Dates in JavaScript
How to Get Just Numeric Part of CSS Property with Jquery
Google Seo and Hidden Elements
How to Compare Two Color Values in Jquery/Javascript
Making Facebook Login Work with an Android Webview
Inject a JavaScript Code in Webview iOS
How to Post Urlencoded Form Data With $Http Without Jquery
Is the JavaScript Date Object Always One Day Off
Sleep in JavaScript - Delay Between Actions
How to Use Backslashes (\) in a String
Is "Clear" a Reserved Word in JavaScript
Html5 Video - Percentage Loaded
How to Make Jquery UI Tabs Scroll Horizontally If There Are Too Many Tabs